一、Spring MVC處理流程
1.Spring MVC將所有請求都交由DispatchServlet進行處理。
2.DispatchServlet獲取HandlerMapping(處理映射器),然后找到對應的HandlerBean處理Controller請求,並返回一個ModelAndView對象。
3.DispatchServlet查詢一個或多個ViewResolver視圖解析器對象, 並把視圖渲染返回給前端。
二、相關配置
首先配置web.xml,我們根據組件啟動順序:全局參數<context-param>、Listener監聽器、Filter過濾器、Servlet、的順序配置。
1 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 4 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5 6 <!-- 配置ContextLoaderListener監聽器,使容器初始化spring配置文件 --> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath*:applicationContext.xml</param-value> 10 </context-param> 11 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 16 <!-- 17 為每個請求過程綁定一個HibernateSession,它將由spring自動管理,無需手動開啟和關閉 18 此session的currentSession由SpringSessionContext管理而不是ThreadLocalSessionContext. 19 --> 20 <filter> 21 <filter-name>openSessionInterceptor</filter-name> 22 <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> 23 </filter> 24 <filter-mapping> 25 <filter-name>openSessionInterceptor</filter-name> 26 <url-pattern>/*</url-pattern> 27 </filter-mapping> 28 29 <!-- 字符集過濾器 --> 30 <filter> 31 <filter-name>encodingFilter</filter-name> 32 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 33 <init-param> 34 <param-name>encoding</param-name> 35 <param-value>UTF-8</param-value> 36 </init-param> 37 </filter> 38 <filter-mapping> 39 <filter-name>encodingFilter</filter-name> 40 <url-pattern>/*</url-pattern> 41 </filter-mapping> 42 43 <!-- spring mvc 配置--> 44 <servlet> 45 <servlet-name>context</servlet-name> 46 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 47 <init-param> 48 <param-name>contextConfigLocation</param-name> 49 <param-value>classpath*:/spring-mvc.xml</param-value> 50 </init-param> 51 </servlet> 52 53 <servlet-mapping> 54 <servlet-name>context</servlet-name> 55 <url-pattern>/</url-pattern> 56 </servlet-mapping> 57 </web-app>
我們首先配置了org.springframework.web.context.ContextLoaderListener的Context監聽器,我們以Tomcat為例:
1、他啟動的時候會去讀取配置文件web.xml,首先讀兩個節點: <listener></listener> 和 <context-param></context-param>,然后創建一個ServletContext,整個web項目將共享這個Context。(意味着,我們這個項目將以spring MVC的context方式啟動)
2.容器將<context-param></context-param>的內容以鍵值對的形式交給Listener,配置中,我們配置了contextConfigLocation,也就是springmvc配置文件的位置,作為啟動springContext的配置文件,這個值在容器啟動的時候監聽器執行contextInitialized方法可以拿到,然后 sc.getInitParameter(CONFIG_LOCATION_PARAM)獲得配置文件路徑,再根據配置文件路徑初始化容器。(Spring源碼如下)
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的核心,他攔截了所有請求,並將請求統一管理。注意:攔截路徑必須寫成<url-pattern>/</url-pattern>,不能寫成<url-pattern>/*</url-pattern>,因為“/*”意為攔截所有請求,只要是請求一律攔截,而“/”意為將DispatcherServlet作為default Servlet(默認是org.apache.catalina.servlets.DefaultServlet),所有其他路徑映射未匹配情況下才會交由它處理。而由於隱式映射的關系,使得 .jsp 擴展名被映射到靜態資源進而被執行。
例如配置成“/*”執行過程中遇到的:
配置成“/*”他將攔截所有請求,當返回視圖路徑資源時,請求資源的請求被當成了servlet給攔截了,進而想要執行對應的Controller,發現沒有對應的視圖,但是根本不存在導致404錯誤。
接下來配置springMVC配置文件
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/tool" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 5 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"> 6 7 <!-- 掃描spring注解包 --> 8 <context:component-scan base-package="controll" use-default-filters="false"> 9 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 10 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RequestMapping"/> 11 </context:component-scan> 12 13 <!-- 配置Spring默認返回路徑前綴和后綴 --> 14 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/pages/"/> 16 <property name="suffix" value=".jsp"/> 17 </bean> 18 19 </beans>
這是spring mvc配置文件,因為我們使用注解方式開發,所以要使用<context:component-scan/>掃描包,掃描注解,其實默認就是掃描注解的,所以可以直接寫成:
1 <context:component-scan base-package="controll" />
第二個是視圖解析器,他將配合控制器使用:
控制器代碼如下:
1 @Controller 2 public class LoginController{ 3 @Resource 4 private UserService userService; 5 6 @RequestMapping("/index.do") 7 public String index(){ 8 return "index"; 9 } 10 }
@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