接着前面一片文章SpringMVC---IDEA 搭建SpringMVC工程,繼續解析各配置文件是用來幹嗎的。只有弄懂每個配置項的意義,才能更好的掌握springMVC.前端
一、web.xml文件做用是什麼?
web.xml文件的做用是配置web工程啓動,對於一個web工程來講,web.xml能夠有也能夠沒有,若是存在web.xml文件;web工程在啓動的時候,web容器(tomcat容器)會去加載web.xml文件,而後按照必定規則配置web.xml文件中的組件。
二、web容器加載web.xml文件的規則是怎樣的?
web容器加載順序:ServletContext -> context-param -> listener -> filter ->servlet ;不會因在web.xml中的書寫順序改變:
a、web容器啓動後,會去加載web.xml文件,讀取listener和context-param兩個節點
b、建立一個ServletContext(Servlet上下文)這個上下文供全部部分共享
c、容器將context-param轉換成鍵值對,交給ServletContext
d、接着按照上述順序繼續執行java
一、配置代碼及流程圖示例:web
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--Spring MVC 配置 並添加監聽-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/*</param-value>
</context-param>
<!-- 字符過濾器 傳值亂碼-->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器 進行請求分發 DispatcherServlet本質也是一個Servlet -->
<servlet>
<!--名字能夠自定義-->
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!--標記容器啓動的時候就啓動這個servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!--攔截全部-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
複製代碼
二、加載順序
a、首先加載Spring容器加載器:ContextLoaderListenerspring
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
複製代碼
查看ContextLoaderListener源碼能夠發現其實現了ServletContextListener並繼承了ContextLoader類,ServletContextListener主要用於監聽web容器的啓動和銷燬,ContextLoader用於web容器啓動後加載springApplicationContext上下文。
ServletContextListener的兩個方法爲contextInitialized,contextDestroyed,主要用來監聽Web應用的生命週期,當web應用初始化或者結束時會觸發ServletContextEvent,並由監聽器監聽觸發其餘操做。
ContextLoader主要用於加載上下文:當web服務器開啓時候,觸發ServletContextEvent並被ContextLoaderListener監聽到,此時執行,ContextLoaderListener中的contextInitialized方法,此方法爲ContextLoader中的方法,查看源碼能夠發現其建立了WebApplicationContext,並將springApplicationContext中的bean註冊到容器中供項目使用。tomcat
b、加載過濾器Filter(此處以編碼過濾器CharacterEncodingFilter爲例)此過濾器的做用就是設置請求參數的編碼爲UTF-8.bash
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
複製代碼
c、加載Servlet:初始化DispatcherServlet,在SpringMVC架構中,DispatchServlet負責請求分發,起到控制器的做用服務器
<!--配置前端控制器 進行請求分發 DispatcherServlet本質也是一個Servlet -->
<servlet>
<!--名字能夠自定義-->
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!--標記容器啓動的時候就啓動這個servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!--攔截全部-->
<url-pattern>/</url-pattern>
</servlet-mapping>
複製代碼