ssm框架中,Java web專案的啟動流程及web.xml配置檔案
阿新 • • 發佈:2019-01-01
一、web.xml配置檔案
專案啟動與web.xml配置檔案密不可分,web.xml配置檔案用於初始化配置資訊,包括welcome、context-param、listener、filter、filter-mapping、servlet、servlet-mapping、其他。如下
<?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_2_5.xsd" id="WebApp_ID" version="2.5"> <!--啟動spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--springmvc的前端控制器,攔截所有請求 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要載入的配置檔案 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 匹配所有請求,此處也可以配置成 *.do 形式 --> <url-pattern>/</url-pattern> </servlet-mapping> <!--編碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--使用rest風格的url,將普通的post請求轉為指定的delete或put請求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--使ajax put請求有效 --> <filter> <filter-name>HttpPutFormContentFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpPutFormContentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
二、專案啟動流程
概況圖解
補充:
- ContextLoaderListener初始化的spring容器中載入的bean共享於整個應用程式,如DAO層、Service層。
- DispatcherServlet初始化的MVC上下文中載入的bean只對Spring Web MVC有效,如Controller層。
1.載入spring容器
(1)Tomcat啟動一個web專案時,web容器會讀取專案的配置檔案web.xml,讀取到<context-param>、<listener>兩個標籤。緊接著,web容器建立一個ServletContext物件(一個全域性的servlet上下文,共享於整個專案)
(2)web容器將<context-param>標籤中的內容轉換為鍵值對,封裝在ServletContext物件中。
(3)web容器建立<listener>中的類例項,建立監聽器,如ContextLoaderListener監聽器。
(4)ContextLoaderListener監聽器在啟動Web容器時,監聽ServletContext物件的變化,獲取ServletContext物件的<context-param>,按照applicationContext.xml的配置資訊載入spring容器,自動裝配元件(bean)。
<!--啟動spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
補充:
- 在ContextLoaderListener 中關聯了ContextLoader 這個類,所以整個載入配置過程由ContextLoader 來完成。
- 監聽器隨web應用的啟動而載入,只初始化一次,隨web應用的停止而銷燬。
2.載入字元編碼過濾器
<!--編碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.載入springMVC的核心控制器
<!--springmvc的前端控制器,攔截所有請求 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要載入的配置檔案 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 匹配所有請求,此處也可以配置成 *.do 形式 --> <url-pattern>/</url-pattern> </servlet-mapping>
補充:
- DispatcherServlet是前端控制器設計模式的實現,提供Spring Web MVC的集中訪問點(也就是把前端請求分發到目標controller),而且與Spring IoC容器無縫整合,從而可以獲得Spring的所有好處。
- DispatcherServlet主要用作職責排程工作,本身主要用於控制流程
- ssm專案執行流程:
(1)前端傳送請求
(2)核心控制器DispatcherServlet呼叫請求解析器HandlerMapping對請求進行解析,通過對映關係匹配到Controller層的handler方法
(3)控制層呼叫其他層程式碼(如Service、DAO層),並將處理結果存在邏輯檢視中
(4)核心控制器呼叫檢視解析器解析邏輯檢視,匹配到相應的頁面檔案,返回給前端。
4.其他
略