Springmvc中web.xml的配置詳解__java

來源:互聯網
上載者:User

首先,web.xml中需要配置的哪些東西。

1.配置監聽器<listener>

         它有兩個監聽器:

          1).

           <!--設定檔載入監聽器-->

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

          ContextLoaderListener它的作用是啟動web容器,(載入設定檔)自動裝配applicationContext.xml配置資訊(詳細配置見下面)。

          2).

                 <!--spring  log4j日誌監聽器配置-->

 <listener>
       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <context-param>
      <param-name>Log4jConfigListener</param-name>
      <!-- 設定檔的位置 -->
      <param-value>classpath:log4j.properties</param-value>
 </context-param>
 <context-param>
     <param-name>Log4jConfigListener</param-name>
     <!-- spring重新整理log4j檔案的時間間隔 -->
     <param-value>10000</param-value>
 </context-param>

2.部署applicationContext.xml檔案

        如果不寫任何參數配置,預設的是在/WEB-INF/applicationContext.xml

        如果想要自訂檔案名稱,需要在web.xml中加入contextConfigLocation這個context參數

    <!-- 配置applicationContext.xml -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
 </context-param>

3.配置前端控制器DispatcherServlet:它是攔截請

     <!-- 配置DispatchServlet 的核心控制器 -->

DispatchServlet是HTTP請求的中央調度處理器,它將web請求轉寄給controller層處理,它提供了敏捷的映射和異常處理機制。
 <servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

或者:

applicationContext.xml設定檔還可以使用<init-param>標籤在servlet標籤中進行配置

<servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 初始化applicationContext.xml -->
     <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
     </init-param>

 </servlet>
 <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>


4.<!-- 錯誤跳轉頁面 -->
 <error-page>
  <!-- 路徑不正確 -->
  <error-code>404</error-code>
  <location>/WEB-INF/errorpage/404.jsp</location>
 </error-page>
 <error-page>
  <!-- 沒有存取權限,訪問被禁止 -->
  <error-code>405</error-code>
  <location>/WEB-INF/errorpage/405.jsp</location>
 </error-page>
 <error-page>
  <!-- 內部錯誤 -->
  <error-code>500</error-code>
  <location>/WEB-INF/errorpage/500.jsp</location>
 </error-page>

5.配置字元集編碼

<filter>

<!-- 用來對瀏覽器的每一次請求進行過濾,加上了父類沒有的功能,就是設定字元集編碼

,一般只用來配置字元集 -->
   <filter-name>CharacterEncodingFilter</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>
   <!-- forceEncoding用來設定是否理會 request.getCharacterEncoding的方法 -->
      <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>

以上就是我們經常會用到的東西。

《*****************************下面貼上一個完整的樣本:********************************》

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name>Web Application</display-name>

 <!-- 讀取spring設定檔 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:config/spring/applicationContext*.xml</param-value>
 </context-param>


 <!-- Spring字元集過濾器 -->
 <filter>
  <filter-name>SpringEncodingFilter</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>SpringEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <filter>
  <filter-name>springSessionRepositoryFilter</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>springSessionRepositoryFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>


 <!-- spring log4j監聽器配置 日誌記錄 -->
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:log4j.properties</param-value>
 </context-param>
 <context-param>
  <param-name>log4jRefreshInterval</param-name>
  <param-value>60000</param-value>
 </context-param>

 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>

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


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


 <!-- springMVC核心配置 -->
 <servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <!-- 註解式控制器 -->
   <param-name>dispatchOptionsRequest</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>dispatchTraceRequest</param-name>
   <param-value>true</param-value>
  </init-param>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:config/spring/applicationContext-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <!-- 錯誤跳轉頁面 -->
 <error-page>
  <!-- 路徑不正確 -->
  <error-code>404</error-code>
  <location>/WEB-INF/errorpage/404.jsp</location>
 </error-page>
 <error-page>
  <!-- 沒有存取權限,訪問被禁止 -->
  <error-code>405</error-code>
  <location>/WEB-INF/errorpage/405.jsp</location>
 </error-page>
 <error-page>
  <!-- 內部錯誤 -->
  <error-code>500</error-code>
  <location>/WEB-INF/errorpage/500.jsp</location>
 </error-page>
</web-app>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.