您的位置:首页 > 编程语言 > Java开发

SpringMVC-DispatcherServlet工作流程及web.xml配置

2016-11-21 14:39 344 查看
工作流程:

Web中,无非是请求和响应;

在SpringMVC中,请求的第一站是DispatcherServlet,充当前端控制器角色;

DispatcherServlet会查询一个或多个处理器映射(handler mapping)并根据请求所携带的URL信息进行决策,将请求发送给哪个SpringMVC控制器(controller);

控制器做两件事:一是将数据打包,二是定义逻辑视图名,然后返回给DispatcherServlet;

DispatcherServlet通过视图解析器(view resolver)来将逻辑视图名匹配为一个特定的视图实现,它可能是也可能不是JSP;

交付数据模型,以视图形式响应给客户,整个请求流程完成。

web.xml

<welcome-file-list>[欢迎页面,可定义多个,会依次查找可用视图]

<listener>
<listener-class>基本配置包含Log4jConfigListener和ContextLoaderListener,且log4j监听器在前,目前已废除log4j监听器,原因还在努力追问

<context-param>指定上下文配置文件路径,基本配置包含log4j和Spring配置文件
<param-name>指定上下文名称,一般为:名称+ConfigLocation后缀,如:contextConfigLocation,不可随意定义,否则指定的配置文件无法加载成功,实际上它是org.springframework.web.servlet.FrameworkServlet中的一个成员变量,而FrameworkServlet是DispatcherServlet的父类,log4jConfigLocation目前不得而知

<param-value>指定上下文路径,如:classpath:applicationContext.xml

<servlet>
<servlet-name>Servlet名称,可以自定义,但是需要遵守规则:比如指定为Spring,那么最好在classpath路径中配置Spring-servlet.xml,否则需要在子元素<init-param>特别指出

<servlet-class>因为要配置MVC,所以指定为:org.springframework.web.servlet.DispatcherServlet

<init-param>[定义容器启动时初始化的配置文件,作用主要是指定自定义配置文件的路径,貌似可以指定多个]
<param-name>[contextConfigLocation,不可更改,原因见3.1]

<param-value>[可以自定义,如:classpath:spring-servlet.xml,如果不定义,那么默认为:classpath:${servlet-name}-servlet.xml,见4.1]

<load-on-startup>[定义为1,表示启动等级,参考文章]

<servlet-mapping>

<servlet-name>与4.1保持一致

<url-pattern>一般定义为“/”,表示所有请求都通过DispatcherServlet来处理

<filter>[以字符集为例]

<filter-name>[自行指定]

<filter-class>[org.springframework.web.filter.CharacterEncodingFilter]

<init-param>
<param-name>[encoding,不可更改,它是CharacterEncodingFilter中定义的一个成员变量]

<param-value>[UTF-8]

<filter-mapping>

<filter-name>[与6.1保持一致]

<url-pattern>[/*表示所有请求都经过此过滤器过滤]

示例:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.0"
3     xmlns="http://java.sun.com/xml/ns/javaee"
4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7   <display-name></display-name>
8   <welcome-file-list>
9     <welcome-file>/WEB-INF/views/home.jsp</welcome-file>
10   </welcome-file-list>
11 <!-- 加载指定位置的上下文配置文件 -->
12   <context-param>
13       <param-name>contextConfigLocation</param-name>
14       <param-value>classpath:applicationContext.xml</param-value>
15   </context-param>
16     <context-param>
17    <param-name>log4jConfigLocation</param-name>
18    <param-value>classpath:log4j.properties</param-value>
19 </context-param>
20   <!-- 定义LOG4J监听器 -->
21 <listener>
22    <listener-class>
23         org.springframework.web.util.Log4jConfigListener
24    </listener-class>
25 </listener>
26 <listener>
27      <listener-class>
28           org.springframework.web.context.ContextLoaderListener
29      </listener-class>
30 </listener>
31 <servlet>
32       <servlet-name>Spring</servlet-name>
33       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34       <!-- 表示启动容器时初始化该servlet -->
35       <init-param>
36           <param-name>contextConfigLocation</param-name>
37           <param-value>classpath:Spring-servlet.xml</param-value>
38       </init-param>
39       <load-on-startup>1</load-on-startup>
40       </servlet>
41       <servlet-mapping>
42           <servlet-name>Spring</servlet-name>
43           <!-- 表示哪些请求需要交给Spring Web MVC处理,/是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求 -->
44           <url-pattern>/</url-pattern>
45       </servlet-mapping>
46     <!-- 使用spring解决中文乱码 -->
47         <filter>
48         <filter-name>CharacterEncodingFilter</filter-name>
49         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
50         <init-param>
51             <param-name>encoding</param-name>
52             <param-value>utf-8</param-value>
53         </init-param>
54       </filter>
55
56       <filter-mapping>
57         <filter-name>CharacterEncodingFilter</filter-name>
58         <url-pattern>/*</url-pattern>
59       </filter-mapping>
60 </web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: