Spring MVC配置详解

一、Spring MVC处理流程

  1.Spring MVC将所有请求都交由DispatchServlet进行处理。

  2.DispatchServlet获取HandlerMapping(处理映射器),然后找到对应的HandlerBean处理Controller请求,并返回一个ModelAndView对象。

  3.DispatchServlet查询一个或多个ViewResolver视图解析器对象, 并把视图渲染返回给前端。

二、相关配置

  首先配置web.xml,我们根据组件启动顺序:全局参数、Listener监听器、Filter过滤器、Servlet、的顺序配置。

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>

contextConfigLocation
classpath*:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

<!–
为每个请求过程绑定一个HibernateSession,它将由spring自动管理,无需手动开启和关闭
此session的currentSession由SpringSessionContext管理而不是ThreadLocalSessionContext.
–>

openSessionInterceptor
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

openSessionInterceptor
/*

encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8

encodingFilter
/*

context
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath*:/spring-mvc.xml

context
/

  我们首先配置了org.springframework.web.context.ContextLoaderListener的Context监听器,我们以Tomcat为例:

  1、他启动的时候会去读取配置文件web.xml,首先读两个节点: 和 ,然后创建一个ServletContext,整个web项目将共享这个Context。(意味着,我们这个项目将以spring MVC的context方式启动)

  2.容器将的内容以键值对的形式交给Listener,配置中,我们配置了contextConfigLocation,也就是springmvc配置文件的位置,作为启动springContext的配置文件,这个值在容器启动的时候监听器执行contextInitialized方法可以拿到,然后

1 String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
2         if (configLocationParam != null) {
3             wac.setConfigLocation(configLocationParam);
4         }

  其中,ConfigLocationPraram是常量

1 public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

  3.接下来是两个过滤器,分别是openSessionInterceptor为了给每个请求绑定HibernateSession的,在GetCurrentSession的时候可以由Spring进行统一管理,无需手动干扰,另外一个是字符集过滤器,将请求的编码统一。

  4.最后是DispatchServlet调度器,他是SpringMVC的核心,他拦截了所有请求,并将请求统一管理。注意:拦截路径必须写成,不能写成,。

  例如配置成“/*”执行过程中遇到的:

  配置成“/*”他将拦截所有请求,当返回视图路径资源时,请求资源的请求被当成了servlet给拦截了,进而想要执行对应的Controller,发现没有对应的视图,但是根本不存在导致404错误。 

  接下来配置springMVC配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=”http://www.springframework.org/schema/context” xmlns:mvc=”http://www.springframework.org/schema/tool”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd”>

  这是spring mvc配置文件,因为我们使用注解方式开发,所以要使用扫描包,扫描注解,其实默认就是扫描注解的,所以可以直接写成:


  第二个是视图解析器,他将配合控制器使用:

  控制器代码如下:

@Controller
public class LoginController{
@Resource
private UserService userService;

@RequestMapping(“/index.do”)
public String index(){
return “index”;
}
}

  @Controller注解说明这是一个控制器Bean,他会被HandlerMapping管理到,

   @RequestMapping注解,声明了servlet的访问路径,里面有多个属性:

value:指定请求的实际地址
method: 指定请求方式,GET、POST、PUT、DELETE等;(默认Get请求)

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

params: 指定request中必须包含某些参数值��,才让该方法处理。

headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

  最后返回的“index字符串将会和视图解析器的前缀和后缀进行拼接形成新的路径”:即/WEB-INF/pages/index.jsp;

  请求的时候只要http://地址:端口/项目名/实际的RequestMapping名称即可,如:http:localhost:8080/springDemo/index.do

SpringMVC注解之@ResponseBody和@RequestBody http://www.linuxidc.com/Linux/2017-06/145173.htm

关于SpringMVC之认识Validation http://www.linuxidc.com/Linux/2017-07/145340.htm

关于SpringMVC前台日期作为实体类对象参数类型转换错误解决 http://www.linuxidc.com/Linux/2017-06/145204.htm

SpringMVC4 注解配置实例 http://www.linuxidc.com/Linux/2017-01/139642.htm

:http://www.linuxidc.com/Linux/2017-10/148056.htm