一、web.xml配置文件
1.welcome-file-list和welcome-file元素
是用來指定歡迎頁面的,welecome-file-list元素可以包含多個welcome-file元素,每個welcome-file指定一個歡迎頁面。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
2.filter和filter-mapping 元素
filter元素用于聲明一個過濾器,使用該元素可以同時攔截多個請求的url,filter-mapping用來指定與過濾器關(guān)聯(lián)的URL
<!-- 配置Struts2框架核心控制器 -->
<filter>
<filter-name>struts</filter-name>
<!-- 這里使用快捷鍵command+shift+T打開open type,搜索 -->
<!-- 指定filter的實現(xiàn)類,此處使用的是Struts2提供的過濾器類 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 定義filter所攔截的url地址 -->
<filter-mapping>
<!-- filter的名字,該名字必須是filter元素中已聲明過的過濾器的名字 -->
<filter-name>struts</filter-name>
<!-- 定義filter負責(zé)攔截的url地址 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
3.error-page元素
error-page元素用來指定錯誤處理頁面,可以通過配置錯誤碼元素error-code以避免用戶直接看到原始錯誤信息,還可以配置異常類型元素exception-type來指定java中的異常類。
<!-- 配置異常頁 -->
<error-page>
<error-code>404</error-code> <!-- 指定錯誤代碼 -->
<location>/error.jsp</location> <!-- 如果發(fā)生HTTP404錯誤,則返回location子元素中的指定文件 -->
</error-page>
<!-- 配置error-page元素用于捕獲java異常 -->
<error-page>
<exception-type>java.lang.Exception</exception-type><!-- 指定異常類 -->
<location>/error.jsp</location>
</error-page>
4.listener元素
該元素用來注冊監(jiān)聽器類,并使用子元素listener-class指定監(jiān)聽程序的完整限定類名,一般用于初始化Spring 框架
<!-- 監(jiān)聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextloaderListener</listener-class>
</listener>
5.session-config元素
該元素用來指定回話過期時間,session對象里面存放的值會自動失效
<!-- 回話時間配置 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
6.init-param元素
該元素用來定義參數(shù),在web.xml中可以有多個init-param元素
<init-param>
<param-name>strust.il8n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>