1. 程式人生 > >SpringMVC-DispatcherServlet工作流程及web.xml配置

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

工作流程:

  1. Web中,無非是請求和響應;
  2. 在SpringMVC中,請求的第一站是DispatcherServlet,充當前端控制器角色;
  3. DispatcherServlet會查詢一個或多個處理器對映(handler mapping)並根據請求所攜帶的URL資訊進行決策,將請求傳送給哪個SpringMVC控制器(controller);
  4. 控制器做兩件事:一是將資料打包,二是定義邏輯檢視名,然後返回給DispatcherServlet;
  5. DispatcherServlet通過檢視解析器(view resolver)來將邏輯檢視名匹配為一個特定的檢視實現,它可能是也可能不是JSP;
  6. 交付資料模型,以檢視形式響應給客戶,整個請求流程完成。

web.xml

  1. <welcome-file-list>[歡迎頁面,可定義多個,會依次查詢可用檢視]
  2. <listener>
    1. <listener-class>基本配置包含Log4jConfigListener和ContextLoaderListener,且log4j監聽器在前,目前已廢除log4j監聽器,原因還在努力追問
  3. <context-param>指定上下文配置檔案路徑,基本配置包含log4j和Spring配置檔案
    1. <param-name>指定上下文名稱,一般為:名稱+ConfigLocation字尾,如:contextConfigLocation,不可隨意定義,否則指定的配置檔案無法載入成功,實際上它是org.springframework.web.servlet.FrameworkServlet中的一個成員變數,而FrameworkServlet是DispatcherServlet的父類,log4jConfigLocation目前不得而知
    2. <param-value>指定上下文路徑,如:classpath:applicationContext.xml
  4. <servlet>
    1. <servlet-name>Servlet名稱,可以自定義,但是需要遵守規則:比如指定為Spring,那麼最好在classpath路徑中配置Spring-servlet.xml,否則需要在子元素<init-param>特別指出
    2. <servlet-class>因為要配置MVC,所以指定為:org.springframework.web.servlet.DispatcherServlet
    3. <init-param>[定義容器啟動時初始化的配置檔案,作用主要是指定自定義配置檔案的路徑,貌似可以指定多個]
      1. <param-name>[contextConfigLocation,不可更改,原因見3.1]
      2. <param-value>[可以自定義,如:classpath:spring-servlet.xml,如果不定義,那麼預設為:classpath:${servlet-name}-servlet.xml,見4.1]
      3. <load-on-startup>[定義為1,表示啟動等級,參考文章]
  5. <servlet-mapping>
    1. <servlet-name>與4.1保持一致
    2. <url-pattern>一般定義為“/”,表示所有請求都通過DispatcherServlet來處理
  6. <filter>[以字符集為例]
    1. <filter-name>[自行指定]
    2. <filter-class>[org.springframework.web.filter.CharacterEncodingFilter]
    3. <init-param>
      1. <param-name>[encoding,不可更改,它是CharacterEncodingFilter中定義的一個成員變數]
      2. <param-value>[UTF-8]
  7. <filter-mapping>
    1. <filter-name>[與6.1保持一致]
    2. <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>
複製程式碼

 


God, Grant me the SERENITY, to accept the things I cannot change, COURAGE to change the things I can, and the WISDOM to know the difference.

工作流程:

  1. Web中,無非是請求和響應;
  2. 在SpringMVC中,請求的第一站是DispatcherServlet,充當前端控制器角色;
  3. DispatcherServlet會查詢一個或多個處理器對映(handler mapping)並根據請求所攜帶的URL資訊進行決策,將請求傳送給哪個SpringMVC控制器(controller);
  4. 控制器做兩件事:一是將資料打包,二是定義邏輯檢視名,然後返回給DispatcherServlet;
  5. DispatcherServlet通過檢視解析器(view resolver)來將邏輯檢視名匹配為一個特定的檢視實現,它可能是也可能不是JSP;
  6. 交付資料模型,以檢視形式響應給客戶,整個請求流程完成。

web.xml

  1. <welcome-file-list>[歡迎頁面,可定義多個,會依次查詢可用檢視]
  2. <listener>
    1. <listener-class>基本配置包含Log4jConfigListener和ContextLoaderListener,且log4j監聽器在前,目前已廢除log4j監聽器,原因還在努力追問
  3. <context-param>指定上下文配置檔案路徑,基本配置包含log4j和Spring配置檔案
    1. <param-name>指定上下文名稱,一般為:名稱+ConfigLocation字尾,如:contextConfigLocation,不可隨意定義,否則指定的配置檔案無法載入成功,實際上它是org.springframework.web.servlet.FrameworkServlet中的一個成員變數,而FrameworkServlet是DispatcherServlet的父類,log4jConfigLocation目前不得而知
    2. <param-value>指定上下文路徑,如:classpath:applicationContext.xml
  4. <servlet>
    1. <servlet-name>Servlet名稱,可以自定義,但是需要遵守規則:比如指定為Spring,那麼最好在classpath路徑中配置Spring-servlet.xml,否則需要在子元素<init-param>特別指出
    2. <servlet-class>因為要配置MVC,所以指定為:org.springframework.web.servlet.DispatcherServlet
    3. <init-param>[定義容器啟動時初始化的配置檔案,作用主要是指定自定義配置檔案的路徑,貌似可以指定多個]
      1. <param-name>[contextConfigLocation,不可更改,原因見3.1]
      2. <param-value>[可以自定義,如:classpath:spring-servlet.xml,如果不定義,那麼預設為:classpath:${servlet-name}-servlet.xml,見4.1]
      3. <load-on-startup>[定義為1,表示啟動等級,參考文章]
  5. <servlet-mapping>
    1. <servlet-name>與4.1保持一致
    2. <url-pattern>一般定義為“/”,表示所有請求都通過DispatcherServlet來處理
  6. <filter>[以字符集為例]
    1. <filter-name>[自行指定]
    2. <filter-class>[org.springframework.web.filter.CharacterEncodingFilter]
    3. <init-param>
      1. <param-name>[encoding,不可更改,它是CharacterEncodingFilter中定義的一個成員變數]
      2. <param-value>[UTF-8]
  7. <filter-mapping>
    1. <filter-name>[與6.1保持一致]
    2. <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>
複製程式碼