MVC框架所做的事情
- 将url映射到java类/java类的方法
- 封装用户提交的数据
- 处理请求,调用相关的业务处理,封装响应数据
- 渲染响应数据,例如jsp,html,freemarker等
spring mvc框架
- 是一个轻量级,基于请求响应的mvc框架
- 特点:
- 性能优于struts2
- 简单,便捷
- 和spring无缝集成
- 约定优于配置(按约定编程),更加简单灵活
- 能够进行简单junit测试
- 支持Restful风格
- 异常处理
- 支持数据验证,类型转换
- 拥有拦截器
spring mvc简单示例
- 导入jar包
配置分发器
DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>a01springmvc_hello</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
在WEB-INF下添加springmvc配置文件
- 文件名:
[servlet-name]-servlet.xml
- 文件名:
编写
Controller
类public class HelloController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView mv = new ModelAndView(); //封装显示到视图中的数据 mv.addObject("msg", "hello spring mvc"); //视图名 mv.setViewName("hello"); return mv; } }
编写springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置handler mapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 配置handler adapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 配置渲染器 --> <bean id="jspviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 配置结果视图的前缀及后缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置请求和处理器 --> <bean name="/hello.do" class="com.eric.controller.HelloController"></bean> </beans>
使用注解
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>a01springmvc_hello</display-name> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
- 需要配置初始化参数
[servlet-name]-servlet.xml
位置
- 需要配置初始化参数
mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置渲染器 --> <bean id="jspviewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 配置结果视图的前缀及后缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置注解扫描 --> <context:component-scan base-package="com.eric.controller"/> </beans>
- 需要配置注解扫描
Controller
@Controller public class HelloController { @RequestMapping("/hello") public ModelAndView hello(HttpServletRequest req, HttpServletResponse resp) { ModelAndView mv = new ModelAndView(); //封装显示到视图中的数据 mv.addObject("msg", "hello spring mvc annotation"); //视图名 mv.setViewName("hello"); return mv; } }
Controller配置方式
按照第一个示例,将请求的URL和Bean名字映射或者通过handlerAdapter自动映射(适合小型的项目)
<!-- 配置请求和处理器 --> <bean name="/hello.do" class="com.eric.controller.HelloController"></bean> <!-- 配置handler adapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
- 为URL分配bean
- web.xml配置不变
- mvc.xml配置
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!-- key表示url请求名 --> <!-- value表示对应处理器的id --> <prop key="/hello.do">helloController</prop> </props> </property> </bean> <!-- 配置处理器 --> <bean id="helloController" class="com.eric.controller.HelloController"></bean>
URL匹配bean(配置失败,暂时找不到原因)
- 使用注解的方式进行配置(参第二个示例),通常使用此方式进行配置
- mvc.xml中需要添加
<context:component-scan base-package="com.eric.controller"/>
- Controller中,通过
@Controller
和@RequestMapping("/path")
进行配置
- mvc.xml中需要添加
Spring MVC入门
最新推荐文章于 2021-02-13 18:31:30 发布