1. 程式人生 > >spring框架中web.xml的配置詳解

spring框架中web.xml的配置詳解

1 定義頭和根元素

  部署描述符檔案就像所有XML檔案一樣,必須以一個XML頭開始。這個頭宣告可以使用的XML版本並給出檔案的字元編碼。
DOCYTPE宣告必須立即出現在此頭之後。這個宣告告訴伺服器適用的servlet規範的版本(如2.2或2.3)並指定管理此檔案其餘部分內容的語法的DTD(Document Type Definition,文件型別定義)。
所有部署描述符檔案的頂層(根)元素為web-app。請注意,XML元素不像HTML,他們是大小寫敏感的。因此,web-App和WEB-APP都是不合法的,web-app必須用小寫。

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

</web-app>

2. display-name元素提供GUI工具可能會用來標記這個特定的Web應用的一個名稱。

<display-name>Archetype Created Web Application</display-name>

3.context-param元素宣告應用範圍內的初始化引數

<context-param>

    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml,classpath:spring/quartz.xml</param-value>

  </context-param>

4.listener servlet API的版本2.3增加了對事件監聽程式的支援,事件監聽程式在建立、修改和刪除會話或servlet環境時得到通知。Listener元素指出事件監聽程式類。

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>
            org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

5.filter 過濾器元素將一個名字與一個實現javax.servlet.Filter介面的類相關聯。filter-mapping 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。

 <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>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>sitemeshFilter</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sitemeshFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

6.servlet 在向servlet或JSP頁面制定初始化引數或定製URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。
lservlet-mapping 伺服器一般為servlet提供一個預設的URL:http://host/webAppPrefix/servlet/ServletName。但是,常常會更改這個URL,以便servlet可以訪問初始化引數或更容易地處理相對URL。在更改預設URL時,使用servlet-mapping元素。

 <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
    <servlet>
        <servlet-name>cacheSysConfig</servlet-name>
        <servlet-class>配置快取</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.ico</url-pattern>
    <url-pattern>*.swf</url-pattern>
    <url-pattern>*.zip</url-pattern>
    <url-pattern>*.xml</url-pattern>
    <url-pattern>*.txt</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>uploadFile</servlet-name>
    <servlet-class>自定義的上傳檔案Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>uploadFile</servlet-name>
    <url-pattern>/servlet/upload</url-pattern>
  </servlet-mapping>

7.匯入自定義的標籤

 <jsp-config>
    <taglib>
      <taglib-uri>/WEB-INF/pagebar.tld</taglib-uri>
      <taglib-location>/WEB-INF/pagebar.tld</taglib-location>
    </taglib>
    <taglib>
      <taglib-uri>/WEB-INF/util.tld</taglib-uri>
      <taglib-location>/WEB-INF/util.tld</taglib-location>
    </taglib>
  </jsp-config>

8.定義預設錯誤的跳轉頁面

<error-page>
  <error-code>404</error-code>
     <location>/error.jsp</location>
    </error-page>
    <error-page>
    <error-code>500</error-code>
    <location>/error.jsp</location>
    </error-page>