整理一下web.xml中的一些配置
首先要了解wel.xml包含了哪裏東西,簡單的項目來說 就是spring的配置,springMvc(servlet)的一些配置,現在很多的項目都是ssm開發,所以servlet一般都是springMvc框架,然後就是一些listener監聽器,攔截器filter,
還有一些其他的包括歡迎頁面,session時間,error-page頁面等
首先我們先要知道web.xml裏面的一些加載順序
1、啓動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結點。
2、緊急着,容創建一個ServletContext(servlet上下文),這個web項目的所有部分都將共享這個上下文。
3、容器將<context-param>轉換爲鍵值對,並交給servletContext。
4、容器創建<listener>中的類實例,創建監聽器。
加載順序與它們在 web.xml 文件中的先後順序無關。即不會因爲 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:ServletContext -> listener -> filter -> servlet
首先要講一下load-on-startup這個標籤
Load-on-startup 元素在web應用啓動的時候指定了servlet被加載的順序,它的值必須是一個整數。如果它的值是一個負整數或是這個元素不存在,那麼容器會在該servlet被調用的時候,加載這個servlet 。如果值是正整數或零,容器在配置的時候就加載並初始化這個servlet,容器必須保證值小的先被加載。如果值相等,容器可以自動選擇先加載誰。
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/applicationContext.xml</param-value>
- </context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
看上面代碼就知道了跟標籤的順序是沒有關聯的
這裏我們將sping放到listener標籤中,讓它更優先加載,因爲spring其實是最後加載的是排在在servlet之後的,如果那樣的話,有一些filter 是需要調用配置一些bean的,這樣的話那些bean就會bean實體就變成null了
接下來就是日誌
- <listener>
- <listener-class>
- org.springframework.web.util.Log4jConfigListener
- </listener-class>
- </listener>
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- </context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
filter過濾器是一個重點
因爲在平時的項目中亂碼一直都是經常存在並且讓很多人很頭疼的一些問題
現在的這個編碼filter就可以解決post提交的一些亂碼
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>eccoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>eccoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在springMvc請求的時候,有時候我們會用到ResquestMethod=PUT或者是DELETE等方法,但瀏覽器一些只識別get,post請求,所以我們配置一個filter就ok
- <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>
<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>
springMvc 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:springmvc/springmvc.xml</param-value>
- </init-param>
- <!-- 可以自定義servlet.xml配置文件的位置和名稱,默認爲WEB-INF目錄下,名稱爲[<servlet-name>]-servlet.xml,如spring-servlet.xml
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring-servlet.xml</param-value> 默認
- </init-param>
- -->
- <load-on-startup>1</load-on-startup>
- </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:springmvc/springmvc.xml</param-value>
</init-param>
<!-- 可以自定義servlet.xml配置文件的位置和名稱,默認爲WEB-INF目錄下,名稱爲[<servlet-name>]-servlet.xml,如spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> 默認
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
最後加載servlet
最後就是一些Session 跟一些歡迎,或者跳轉的頁面
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <session-config>
- <session-timeout>30</session-timeout>
- </session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
錯誤頁面
<error-page> 2 <error-code>404</error-code> 3 <location>/error404.jsp</location> 4 </error-page> 5 <error-page> 6 <exception-type>java.lang.Exception</exception-type> 7 <location>/exception.jsp</location> 8 </error-page>