1. 程式人生 > >【SSM】【2】web.xml配置

【SSM】【2】web.xml配置

web.xml就是一個釋出容器,裡邊包括了相關的專案配置,當一個web專案啟動後,Tomcat容器就會先讀取web.xml配置檔案中的配置,正確後項目才會被正常啟動

  1. 首先啟動web.xml中的<listener></listener>和<context-param></context-param>兩個節點
  2. 緊接著建立一個servletContext(application),這個web專案都將共享這個上下文
  3. 容器以<context></context>name作為鍵,value作為值,將其轉化為鍵值對,存入servletContext容器中,這就是是上下文配置
  4. 容器建立監聽器<listener></listener>的例項,容器會讀取過濾器<filter></filter>
  5. 容器讀取<servlet></servlet>配置
  6. 總的來說web.xml的載入順序就是<context-param>-><listener>-><filter>-><servlet>
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="3.0">

    <!-- session超時定義,單位為分鐘 -->
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>

    <!-- 預設首頁定義 -->
    <welcome-file-list>
        <welcome-file>pages/login.html</welcome-file>
    </welcome-file-list>


    <!-- Spring ApplicationContext配置檔案的路徑,可使用萬用字元"*",多個路徑用","號分隔 此引數用於後面的Spring-Contextloader -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/*.xml</param-value>
    </context-param>
    <!--Spring ApplicationContext 載入 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 使Spring支援request與session的scope,如:<bean id="loginAction" class="com.foo.LoginAction" scope="request"/> -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!-- Spring 重新整理Introspector防止記憶體洩露 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- Character Encoding filter -->
    <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>
            <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>

    <!-- LoggerUtil,存放在MDC中的資料,log4j可以直接引用並作為日誌資訊打印出來. -->
    <filter>
        <filter-name>LoggerUtil</filter-name>
        <filter-class>com.edhic.ecif.utils.LoggerUtil</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoggerUtil</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 01 Cas 登出 Filter -->
    <!-- <filter>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->

    <!-- 02 Cas Auth Filter -->
    <!-- <filter>
        <filter-name>CAS Authentication Filter</filter-name>
        <filter-class>com.edhic.ecif.filter.CasFilter</filter-class>
        <init-param>
            <param-name>casServerLoginUrl</param-name> -->
            <!-- 填寫CAS伺服器訪問地址/埠 -->
            <!-- <param-value>
                http://sso.sinosafe.com.cn/sso
            </param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <param-value>http://ecif.sinosafe.com.cn</param-value>
        </init-param> -->
        <!-- <init-param> <param-name>service</param-name> <param-value>http://localhost:8090/TestCasClient2/</param-value>
            </init-param> -->
    <!-- </filter>
    <filter-mapping>
        <filter-name>CAS Authentication Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->

    <!-- 03 Cas Validation Filter -->
    <!-- <filter>

        <filter-name>CAS Validation Filter</filter-name>
        <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
        <init-param>
            <param-name>casServerUrlPrefix</param-name> -->
            <!-- 填寫CAS伺服器訪問地址/埠 -->
            <!-- <param-value>http://sso.sinosafe.com.cn/sso</param-value>
        </init-param> -->
       <!--  <init-param>
            <param-name>serverName</param-name>
            <param-value>http://ecif.sinosafe.com.cn</param-value>
        </init-param> -->
        <!-- <init-param> <param-name>service</param-name> <param-value>http://localhost:8090/TestCasClient2/</param-value>
            </init-param> -->
    <!-- </filter>
    <filter-mapping>
        <filter-name>CAS Validation Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->

    <!-- 04 Cas Wrapper Filter -->
   <!--  <filter>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->

    <!-- 05 Cas Assertion Filter -->
    <!-- <filter>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 -->


    <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>enabled</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>loginFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <init-param>
            <!-- 允許清空統計資料 -->
            <param-name>resetEnable</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <!-- 使用者名稱 -->
            <param-name>loginUsername</param-name>
            <param-value>taocong</param-value>
        </init-param>
        <init-param>
            <!-- 密碼 -->
            <param-name>loginPassword</param-name>
            <param-value>taocong123456</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>druidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>/pages/*,*.js,*.css,/druid*,*.jsp,*.swf</param-value>
        </init-param>
        <init-param>
            <param-name>principalSessionName</param-name>
            <param-value>sessionInfo</param-value>
        </init-param>
        <init-param>
            <param-name>profileEnable</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>druidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- spring mvc -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>