spring是目前最流行的框架。創建java web項目時,我們首先會遇到的配置文件就是web.xml,這是javaweb為我們封裝的邏輯,不在今天的研究中。下面我們將簡單講講web.xml中的配置。
一、一個空的web.xml
1 1 <?xml version="1.0" encoding="UTF-8"?> 2 2 <web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 4 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 5 id="WebApp_ID"> 6 6 </web-app>
二、標簽介紹
web.xml中比較常見的標簽以及其加載順序為:
context-param > listener > filter > servlet
1、 <display-name>Archetype Created Web Application</display-name>
display-name 是標識項目的名稱,這個不是很常用,可有可無的,或者說不需要我們去在意的東西。
2、 <context-param>
1 <context-param> 2 <param-name>webAppRootKey</param-name> 3 <param-value>60000</param-value> 4 </context-param>
context-param 是web.xml首先加載的標簽,其下子標簽有param-name和param-value.
此所設定的參數,在JSP網頁中可以使用下列方法來取得:
1 ${initParam.webAppRootKey}
若在Servlet可以使用下列方法來獲得:
1 String param_name=getServletContext().getInitParamter(“webAppRootKey”);
3、listener
1 <listener> 2 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 3 </listener>
listenter在項目開始的時候就注入進來,盡在context-param之后,所以正常我們將spring配置在listener 中,這樣方法spring 初始化相關的bean。
4、filter
1 <filter> 2 <filter-name>CharacterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 <init-param> 9 <param-name>forceEncoding</param-name> 10 <param-value>true</param-value> 11 </init-param> 12 </filter> 13 14 <filter-mapping> 15 <filter-name>CharacterEncodingFilter</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping>
filter起到一個過濾的作用,在servlet執行前后,像上面的配置就是在過濾servlet前將編碼轉換UTF-8,filter-mapping 則是將filter和url路徑進行映射。其中init-param則是將初始化需要的參數傳入到filter-class中從而進行初始化。filter和filter-mapping中的name必須是相同的,才能起到映射的作用,而filter-mapping 中的url-pattern則是匹配請求路徑的。上面‘/*'表示過濾所有請求的servlet,如果寫成‘/lzg',則過濾http://localhost:8080/項目名/lzg這個請求。
5、servlet
1 <servlet> 2 <!-- 配置DispatcherServlet --> 3 <servlet-name>springMvc</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <!-- 指定spring mvc配置文件位置 不指定使用默認情況 --> 6 <init-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:spring/spring-mvc.xml</param-value> 9 </init-param> 10 <!-- 設置啟動順序 --> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 14 <!-- ServLet 匹配映射 --> 15 <servlet-mapping> 16 <servlet-name>springMvc</servlet-name> 17 <url-pattern>*.zxh</url-pattern> 18 </servlet-mapping>
servlet和filter類似,需要先指定servlet對應的class類,然后將這個類和utl路徑請求地址進行映射。這里不多說了。
以上就是web.xml文件中出現最多的幾個標簽。其他的比如:
6、歡迎頁
1 <welcome-file-list> 2 <welcome-file>login.jsp</welcome-file> 3 </welcome-file-list>
7、錯誤頁
1 <!-- 后台程序異常錯誤跳轉頁面 --> 2 <error-page> 3 <exception-type>java.lang.Throwable</exception-type> 4 <location>/views/error.jsp</location> 5 </error-page> 6 7 <!-- 500跳轉頁面--> 8 <error-page> 9 <error-code>500</error-code> 10 <location>/views/500.jsp</location> 11 </error-page> 12 13 <!-- 404跳轉頁面 --> 14 <error-page> 15 <error-code>404</error-code> 16 <location>/views/404.jsp</location> 17 </error-page>