web.xml 配置介紹

這個不是原創,有點早了,具體從哪裏來的已經記不得了。可是東西是實實在在的。html

一、啓動一個WEB項目的時候,WEB容器會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結點。java

二、緊急着,容建立一個ServletContext(servlet上下文),這個web項目的全部部分都將共享這個上下文。web

三、容器將<context-param>轉換爲鍵值對,並交給servletContext。spring

四、容器建立<listener>中的類實例,建立監聽器。sql

二 Load-on-startup數據庫

Load-on-startup 元素在web應用啓動的時候指定了servlet被加載的順序,它的值必須是一個整數。若是它的值是一個負整數或是這個元素不存在,那麼容器會在該servlet被調用的時候,加載這個servlet 。若是值是正整數或零,容器在配置的時候就加載並初始化這個servlet,容器必須保證值小的先被加載。若是值相等,容器能夠自動選擇先加載誰。apache

在servlet的配置當中,<load-on-startup>5</load-on-startup>的含義是:瀏覽器

標記容器是否在啓動的時候就加載這個servlet。tomcat

當值爲0或者大於0時,表示容器在應用啓動時就加載這個servlet;安全

當是一個負數時或者沒有指定時,則指示容器在該servlet被選擇時才加載。

正數的值越小,啓動該servlet的優先級越高。

三 加載順序

首先能夠確定的是,加載順序與它們在 web.xml 文件中的前後順序無關。即不會由於 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:ServletContext -> listener -> filter -> servlet

同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。咱們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是否是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,所以真正的加載順序爲:context-param -> listener -> filter -> servlet

    對於某類配置節而言,與它們出現的順序是有關的。以 filter 爲例,web.xml 中固然能夠定義多個 filter,與 filter 相關的一個配置節是 filter-mapping,這裏必定要注意,對於擁有相同 filter-name 的 filter 和 filter-mapping 配置節而言,filter-mapping 必須出如今 filter 以後,不然當解析到 filter-mapping 時,它所對應的 filter-name 還未定義。web 容器啓動時初始化每一個 filter  時,是按照 filter 配置節出現的順序來初始化的,當請求資源匹配多個 filter-mapping 時,filter 攔截資源是按照 filter-mapping 配置節出現的順序來依次調用 doFilter() 方法的。

    servlet 同 filter 相似,此處再也不贅述。

   由此,能夠看出,web.xml 的加載順序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同個類型之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的。

四 web.xml文件詳解

我將本身知道的web.xml的元素整理了一下:

web.xml首先是確定要包含它的schema.

複製代碼 1 <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 4 version="2.4"> 5 6 </web-app> 複製代碼 其它的元素都放在<web-app></web-app>之中。

<discription></discription> 是對站臺的描述 <display-name></display-name> 定義站臺的名稱 <distributable/> 是指定該站臺是否可分佈式處理

<context-param></context-param> 用來設定web站臺的環境參數,它包含兩個子元素: <param-name></param-name> 用來指定參數的名稱 <param-value></param-value> 用來設定參數值。好比:

1 <context-param> 2 <param-name>my_param</param-name> 3 <param-value>hello</param-value> 4 </context-param> 在此設定的參數,能夠在servlet中用 getServletContext().getInitParameter("my_param") 來取得

<filter></filter> 是用來聲明filter的相關設定,它包含如下子元素:

  <filter-name></filter-name> 這固然就是指定filter的名字

  <filter-class></filter-class> 這是用來定義filter的類的名稱

<init-param></init-param> 用來定義參數,它有兩個子元素:

  <param-name></param-name> 用來指定參數的名稱

  <param-value></param-value> 用來設定參數值。

好比:

複製代碼 1 <filter> 2 <filter-name>setCharacterEncoding</filter-name> 3 <filter-class>com.myTest.setCharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>GB2312</param-value> 7 </init-param> 8 </filter> 複製代碼 與<filter></filter>同時使用的是<filter-mapping></filter-mapping> 用來定義filter所對應的URL,它有兩個子元素:

  <filter-name></filter-name> 指定filter的名字

  <url-pattern></url-pattern> 指定filter所對應的URL。

好比:

1 <filter-mapping> 2 <filter-name>setCharacterEncoding</filter-name> 3 <url-pattern>/*</url-pattern> 4 </filter-mapping> <listener></listener> 用來設定Listener接口,它的主要子元素爲

  <listener-class></listener-class> 定義Listener的類名稱

好比:

1 <listener> 2 <listener-class>com.myTest.ContextListener</listener-class> 3 </listener> <servlet></servlet> 用來聲明一個servlet的數據,主要有如下子元素:

  <servlet-name></servlet-name> 指定servlet的名稱

  <servlet-class></servlet-class> 指定servlet的類名稱

  <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑

  <init-param></init-param> 用來定義參數,和前面的<init-param>差很少

一樣,與<servlet></servlet>一塊兒使用的是<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素:

  <servlet-name></servlet-name> 指定servlet的名稱

  <url-pattern></url-pattern> 指定servlet所對應的URL

好比:

複製代碼 1 <servlet> 2 <servlet-name>ShoppingServlet</servlet-name> 3 <servlet-class>com.myTest.ShoppingServlet</servlet-class> 4 </servlet> 5
6 <servlet-mapping> 7 <servlet-name>ShoppingServlet</servlet-name> 8 <url-pattern>/shop/ShoppingServlet</url-pattern> 9 </servlet-mapping> 複製代碼 <session-config></session-config> 用來定義web站臺中的session參數,包含一個子元素:

  <session-timeout></session-timeout> 用來定義這個web站臺全部session的有效期限,單位爲分鐘

<mime-mapping></mime-mapping> 定義某一個擴展名和某一個MIME Type作對映,包含兩個子元素:

  <extension></extension> 擴展名的名稱

  <mime-type></mime-type> MIME格式

好比:

複製代碼 1 <mime-mapping> 2 <extension>doc</extension> 3 <mime-type>application/vnd.ms-word</mime-type> 4 </mime-mapping> 5 <mime-mapping> 6 <extension>xls</extension> 7 <mime-type>application/vnd.ms-excel</mime-type> 8 </mime-mapping> 複製代碼 <welcome-file-list></welcom-file-list> 用來定義首頁的列單,包含一個子元素:

  <welcome-file></welcome-file> 指定首頁的文件名稱

好比:

1 <welcome-file-list> 2 <welcome-file>index.jsp</welcome-file> 3 <welcome-file>index.html</welcome-file> 4 </welcom-file-list> <error-page></error-page> 用來處理錯誤代碼或異常的頁面,有三個子元素:

  <error-code></error-code> 指定錯誤代碼

  <exception-type></exception-type> 指定一個JAVA異常類型

  <location></location> 指定在web站臺內的相關資源路徑

好比:

複製代碼 1 <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> 複製代碼 <taglib></taglib> 用來設定JSP網頁所用到的Tag Library路徑,有兩個子元素:

  <taglib-uri></taglib-uri> 定義TLD文件的URI,在JSP網頁中用taglib指令即可取得該URI的TLD文件

  <taglib-location></taglib-location> 指定TLD文件相對於web站臺的存放位置

好比:

1 <taglib> 2 <taglib-uri>myTaglib</taglib-uri> 3 <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location> 4 </taglib> <resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源,有五個子元素:

  <description></description> 資源說明

  <rec-ref-name></rec-ref-name> 資源名稱

  <res-type></res-type> 資源種類

  <res-auth></res-auth> 資源經由Application或Container來許可

  <res-sharing-scope></res-sharing-scope> 資源是否能夠共享,有Shareable和Unshareable兩個值,默認爲Shareable

好比,配置數據庫鏈接池就可在此配置:

複製代碼 1 <resource-ref> 2 <description>JNDI JDBC DataSource of shop</description> 3 <res-ref-name>jdbc/sample_db</res-ref-name> 4 <res-type>javax.sql.DataSource</res-type> 5 <res-auth>Container</res-auth> 6 </resource-ref> 複製代碼 <jsp-config> 包括<taglib> 和<jsp-property-group> 兩個子元素。

其中<taglib>元素在JSP 1.2時就已經存在;而<jsp-property-group>是JSP 2.0 新增的元素。

<jsp-property-group>元素主要有八個子元素,它們分別爲:

1.<description>:設定的說明;

2.<display-name>:設定名稱;

3.<url-pattern>:設定值所影響的範圍,如:/CH2 或 /*.jsp;

4.<el-ignored>:若爲true,表示不支持EL 語法;

5.<scripting-invalid>:若爲true,表示不支持<% scripting %>語法;

6.<page-encoding>:設定JSP 網頁的編碼;

7.<include-prelude>:設置JSP 網頁的擡頭,擴展名爲.jspf;

8.<include-coda>:設置JSP 網頁的結尾,擴展名爲.jspf。

一個簡單的<jsp-config>元素完整配置:

複製代碼 1 <jsp-config> 2 <taglib> 3 <taglib-uri>Taglib</taglib-uri> 4 <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location> 5 </taglib> 6 <jsp-property-group> 7 <description>Special property group for JSP Configuration JSP example.</description> 8 <display-name>JSPConfiguration</display-name> 9 <url-pattern>/jsp/* </url-pattern> 10 <el-ignored>true</el-ignored> 11 <page-encoding>GB2312</page-encoding> 12 <scripting-invalid>true</scripting-invalid> 13 <include-prelude>/include/prelude.jspf</include-prelude> 14 <include-coda>/include/coda.jspf</include-coda> 15 </jsp-property-group> 16 </jsp-config>
複製代碼 配置web.xml來限制對某些servlet的請求

有時咱們只但願經過認證的用戶才能請求某些servlet的話,就能夠在web.xml中來進行相應的配置,來達到此目的。

這就要用到<security-constraint></security-constraint>元素。 對於tomcat,中web.xml使用security-constraint元素須要在位於<Tomcat-installation-directory>/conf/tomcat-users.xml的XML文件中建立用戶名和密碼。好比下面的這個tomcat-users.xml文件:

複製代碼 1 <?xml version='1.0' encoding='utf-8'?> 2 <tomcat-users> 3 <role rolename="tomcat"/> 4 <role rolename="manager"/> 5 <role rolename="admin"/> 6 <user username="tomcat" password="tomcat" roles="tomcat"/> 7 <user username="both" password="tomcat" roles="tomcat,manager"/> 8 <user username="admin" password="admin" roles="admin"/> 9 </tomcat-users> 複製代碼 此XML片斷包括一個tomcat-users根元素,它包含一個或多個role和user元素。

而後在Web應用程序的web.xml中建立security-constraint、login-config和security-role元素。

複製代碼 1 <security-constraint> 2 <web-resource-collection> 3 <web-resource-name>HelloServlet</web-resource-name> 4 <url-pattern>/HelloServlet</url-pattern> 5 <http-method>GET</http-method> 6 <http-method>POST</http-method> 7 </web-resource-collection> 8 <auth-constraint> 9 <description>This applies only to the "tomcat" security role</description> 10 <role-name>admin</role-name> 11 </auth-constraint> 12 <user-data-constraint> 13 <transport-guarantee>NONE</transport-guarantee> 14 </user-data-constraint> 15 </security-constraint> 16
17 <login-config> 18 <auth-method>BASIC</auth-method> 19 </login-config> 20 <security-role> 21 <role-name>admin</role-name> 22 </security-role> 複製代碼 其中security-constraint元素包含一個或多個web-resource-collection元素,它是描述Web應用程序中的哪些web資源受到指定安全限制的保護。http-method元素指定安全限制覆蓋的HTTP方法。上面的例子中,當咱們對/HelloServlet的GET或POST請求時將觸發配置的安全機制。 auth-constraint元素用於描述容許訪問Web組件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有看成爲admin角色的用戶才能夠訪問HelloServlet。

Web應用程序經過login-config元素來認證用戶,並確認該用戶是否爲正確的角色。 longin-config包含的transport-guarantee子元素用來指定認證方法,BASIC是一種常見的Web認證方式,瀏覽器給用戶提示一個對話框,要求輸入用戶名和密碼,隨後Tomcat將給出的用戶名和密碼與tomcat-users.xml中的用戶名和密碼進行比較,而後使用前面的security-constraint配置來肯定用戶是否可訪問受保護的servlet。

(除BASIC外,還能夠是FORM、CLIENT-CERT、DIGEST等)

其實這種認證方法實際上有兩個步驟: 一、檢查提供的用戶名和密碼是否正確。 二、判斷用戶是否映射到特定的安全角色。例如,用戶可能提供了正確的用戶名和密碼,但沒有映射到特定的安全角色,也將被禁止訪問特定的Web資源。

五 相應元素配置
一、Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標

1 <icon>
2 <small-icon>/images/app_small.gif</small-icon>
3 <large-icon>/images/app_large.gif</large-icon>
4 </icon>
二、Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱
< display-name>Tomcat Example</display-name>
三、Web 應用描述: 給出於此相關的說明性文本
< disciption>Tomcat Example servlets and JSP pages.</disciption>
四、上下文參數:聲明應用範圍內的初始化參數。

1 <context-param>
2 <param-name>ContextParameter</para-name>
3 <param-value>test</param-value>
4 <description>It is a test parameter.</description>
5 </context-param> 在servlet裏面能夠經過getServletContext().getInitParameter("context/param")獲得

五、過濾器配置:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯。

複製代碼 1 <filter>
2 <filter-name>setCharacterEncoding</filter-name>
3 <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
4 <init-param>
5 <param-name>encoding</param-name>
6 <param-value>GB2312</param-value>
7 </init-param>
8 </filter>
9 <filter-mapping>
10 <filter-name>setCharacterEncoding</filter-name>
11 <url-pattern>/*</url-pattern>
12 </filter-mapping>
複製代碼 六、監聽器配置

1 <listener>
2 <listerner-class>listener.SessionListener</listener-class>
3 </listener> 七、Servlet配置

基本配置

複製代碼 1 <servlet>
2 <servlet-name>snoop</servlet-name>
3 <servlet-class>SnoopServlet</servlet-class>
4 </servlet>
5 <servlet-mapping>
6 <servlet-name>snoop</servlet-name>
7 <url-pattern>/snoop</url-pattern>
8 </servlet-mapping> 複製代碼 高級配置

複製代碼 1 <servlet>
2 <servlet-name>snoop</servlet-name>
3 <servlet-class>SnoopServlet</servlet-class>
4 <init-param>
5 <param-name>foo</param-name>
6 <param-value>bar</param-value>
7 </init-param>
8 <run-as>
9 <description>Security role for anonymous access</description>
10 <role-name>tomcat</role-name>
11 </run-as>
12 </servlet>
13 <servlet-mapping>
14 <servlet-name>snoop</servlet-name>
15 <url-pattern>/snoop</url-pattern>
16 </servlet-mapping> 複製代碼 元素說明
<servlet></servlet> 用來聲明一個servlet的數據,主要有如下子元素:
<servlet-name></servlet-name> 指定servlet的名稱
<servlet-class></servlet-class> 指定servlet的類名稱
<jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑
<init-param></init-param> 用來定義參數,可有多個init-param。在servlet類中經過getInitParamenter(String name)方法訪問初始化參數
<load-on-startup></load-on-startup>指定當Web應用啓動時,裝載Servlet的次序。
當值爲正數或零時:Servlet容器先加載數值小的servlet,再依次加載其餘數值大的servlet.
當值爲負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它
<servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素
<servlet-name></servlet-name> 指定servlet的名稱
<url-pattern></url-pattern> 指定servlet所對應的URL
八、會話超時配置(單位爲分鐘)

1 <session-config>
2 <session-timeout>120</session-timeout>
3 </session-config>
九、MIME類型配置

1 <mime-mapping>
2 <extension>htm</extension>
3 <mime-type>text/html</mime-type>
4 </mime-mapping> 十、指定歡迎文件頁配置

1 <welcome-file-list>
2 <welcome-file>index.jsp</welcome-file>
3 <welcome-file>index.html</welcome-file>
4 <welcome-file>index.htm</welcome-file>
5 </welcome-file-list>
十一、配置錯誤頁面
1、 經過錯誤碼來配置error-page

1 <error-page>
2 <error-code>404</error-code>
3 <location>/NotFound.jsp</location>
4 </error-page> 上面配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp。
2、經過異常的類型配置error-page

1 <error-page>
2 <exception-type>java.lang.NullException</exception-type>
3 <location>/error.jsp</location>
4 </error-page> 上面配置了當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面error.jsp
十二、TLD配置

1 <taglib>
2 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
3 <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
4 </taglib>
若是MyEclipse一直在報錯,應該把<taglib> 放到 <jsp-config>中

複製代碼 1 <jsp-config>
2 <taglib>
3 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
4 <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
5 </taglib>
6 </jsp-config> 複製代碼 1三、資源管理對象配置

1 <resource-env-ref>
2 <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
3 </resource-env-ref> 1四、資源工廠配置

1 <resource-ref>
2 <res-ref-name>mail/Session</res-ref-name>
3 <res-type>javax.mail.Session</res-type>
4 <res-auth>Container</res-auth>
5 </resource-ref> 配置數據庫鏈接池就可在此配置:

複製代碼 1 <resource-ref>
2 <description>JNDI JDBC DataSource of shop</description>
3 <res-ref-name>jdbc/sample_db</res-ref-name>
4 <res-type>javax.sql.DataSource</res-type>
5 <res-auth>Container</res-auth>
6 </resource-ref> 複製代碼 1五、安全限制配置

複製代碼 1 <security-constraint>
2 <display-name>Example Security Constraint</display-name>
3 <web-resource-collection>
4 <web-resource-name>Protected Area</web-resource-name>
5 <url-pattern>/jsp/security/protected/*</url-pattern>
6 <http-method>DELETE</http-method>
7 <http-method>GET</http-method>
8 <http-method>POST</http-method>
9 <http-method>PUT</http-method>
10 </web-resource-collection>
11 <auth-constraint>
12 <role-name>tomcat</role-name>
13 <role-name>role1</role-name>
14 </auth-constraint>
15 </security-constraint> 複製代碼 1六、登錄驗證配置

複製代碼 1 <login-config>
2 <auth-method>FORM</auth-method>
3 <realm-name>Example-Based Authentiation Area</realm-name>
4 <form-login-config>
5 <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
6 <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
7 </form-login-config>
8 </login-config> 複製代碼 1七、安全角色:security-role元素給出安全角色的一個列表,這些角色將出如今servlet元素內的security-role-ref元素的role-name子元素中。分別地聲明角色可以使高級IDE處理安全信息更爲容易。

1 <security-role>
2 <role-name>tomcat</role-name>
3 </security-role> 1八、Web環境參數:env-entry元素聲明Web應用的環境項

1 <env-entry>
2 <env-entry-name>minExemptions</env-entry-name>
3 <env-entry-value>1</env-entry-value>
4 <env-entry-type>java.lang.Integer</env-entry-type>
5 </env-entry> 1九、EJB 聲明

複製代碼 1 <ejb-ref>
2 <description>Example EJB reference</decription>
3 <ejb-ref-name>ejb/Account</ejb-ref-name>
4 <ejb-ref-type>Entity</ejb-ref-type>
5 <home>com.mycompany.mypackage.AccountHome</home>
6 <remote>com.mycompany.mypackage.Account</remote>
7 </ejb-ref> 複製代碼 20、本地EJB聲明

複製代碼 1 <ejb-local-ref>
2 <description>Example Loacal EJB reference</decription>
3 <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
4 <ejb-ref-type>Session</ejb-ref-type>
5 <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
6 <local>com.mycompany.mypackage.ProcessOrder</local>
7 </ejb-local-ref> 複製代碼 2一、配置DWR

複製代碼 1 <servlet>
2 <servlet-name>dwr-invoker</servlet-name>
3 <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
4 </servlet>
5 <servlet-mapping>
6 <servlet-name>dwr-invoker</servlet-name>
7 <url-pattern>/dwr/*</url-pattern>
8 </servlet-mapping> 複製代碼 2二、配置Struts

複製代碼 1 <display-name>Struts Blank Application</display-name>
2 <servlet>
3 <servlet-name>action</servlet-name>
4 <servlet-class>
5 org.apache.struts.action.ActionServlet
6 </servlet-class>
7 <init-param>
8 <param-name>detail</param-name>
9 <param-value>2</param-value>
10 </init-param>
11 <init-param>
12 <param-name>debug</param-name>
13 <param-value>2</param-value>
14 </init-param>
15 <init-param>
16 <param-name>config</param-name>
17 <param-value>/WEB-INF/struts-config.xml</param-value>
18 </init-param>
19 <init-param>
20 <param-name>application</param-name>
21 <param-value>ApplicationResources</param-value>
22 </init-param>
23 <load-on-startup>2</load-on-startup>
24 </servlet>
25 <servlet-mapping>
26 <servlet-name>action</servlet-name>
27 <url-pattern>*.do</url-pattern>
28 </servlet-mapping>
29 <welcome-file-list>
30 <welcome-file>index.jsp</welcome-file>
31 </welcome-file-list>
32 33 <!-- Struts Tag Library Descriptors -->
34 <taglib>
35 <taglib-uri>struts-bean</taglib-uri>
36 <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
37 </taglib>
38 <taglib>
39 <taglib-uri>struts-html</taglib-uri>
40 <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
41 </taglib>
42 <taglib>
43 <taglib-uri>struts-nested</taglib-uri>
44 <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
45 </taglib>
46 <taglib>
47 <taglib-uri>struts-logic</taglib-uri>
48 <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
49 </taglib>
50 <taglib>
51 <taglib-uri>struts-tiles</taglib-uri>
52 <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
53 </taglib> 複製代碼 2三、配置Spring(基本上都是在Struts中配置的)

複製代碼 1 <!-- 指定spring配置文件位置 -->
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>
5 <!--加載多個spring配置文件 -->
6 /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
7 </param-value>
8 </context-param>
9 10 <!-- 定義SPRING監聽器,加載spring -->
11 12 <listener>
13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
14 </listener>
15 16 <listener>
17 <listener-class>
18 org.springframework.web.context.request.RequestContextListener
19 </listener-class>
20 </listener>
複製代碼 其餘說明:

web.xml 文件中通常包括 servlet, spring, filter, listenr的配置。那麼他們是按照一個什麼順序加載呢?加載順序會影響對spring bean 的調用。 好比filter 須要用到 bean ,可是加載順序是 先加載filter 後加載spring,則filter中初始化操做中的bean爲null; 首先能夠確定 加載順序與他們在web.xml 文件中的前後順序無關。 web.xml 中 listener 和 serverlet 的加載順序爲 先 listener 後serverlet 最終得出結果:先 listener >> filter >> servlet >> spring 因此,若是過濾器中要使用到 bean,能夠將spring 的加載 改爲 Listener的方式 1 <listener> 2 <listener-class> 3 org.springframework.web.context.ContextLoaderListener 4 </listener-class> 5 </listener> 關於他們的內部執行順序,也須要注意,以下面文章中遇到的問題

web.xml的filter執行順序致使的亂碼,切記! 發現引發bug的緣由是web.xml的下面幾行: 複製代碼 1 <filter-mapping> 2 <filter-name>SecurityFilter</filter-name> 3 <url-pattern>.do</url-pattern> 4 </filter-mapping> 5 6 <filter-mapping> 7 <filter-name>CharacterEncoding</filter-name> 8 <url-pattern>.do</url-pattern> 9 </filter-mapping> 10 11 <filter-mapping> 12 <filter-name>CharacterEncoding</filter-name> 13 <url-pattern>.jsp</url-pattern> 14 </filter-mapping> 複製代碼 根據servlet2.3規範filter執行是按照web.xml配置的filter-mapping前後順序進行執行,因此上面的配置會致使碰見.do的url請求,先進行SecurityFilter的過濾器處理,這時候沒有作編碼處理,已是亂碼,到下面的filter處理時已經時亂碼,再作編碼處理已經沒有用處。

修正方式,調整filter-mapping順序,以下: 複製代碼 1 <filter-mapping> 2 <filter-name>CharacterEncoding</filter-name> 3 <url-pattern>.do</url-pattern> 4 </filter-mapping> 5 6 <filter-mapping> 7 <filter-name>CharacterEncoding</filter-name> 8 <url-pattern>.jsp</url-pattern> 9 </filter-mapping> 10 11 <filter-mapping> 12 <filter-name>SecurityFilter</filter-name> 13 <url-pattern>*.do</url-pattern> 14 </filter-mapping> 複製代碼

相關文章
相關標籤/搜索