ssm框架整合入門系列——編寫ssm整合的關鍵配置文件(web.xml)


編寫ssm整合的關鍵配置文件(web.xml)


前言

web.xml,一個Tomcat工程中最重要的配置文件。web.xml沒有其實也可以----只要你確定你的項目里面不需要任何過濾器、監聽器、Servlet等等

在啟動一個WEB項目的時候,WEB容器(比如tomcat)會去讀取它的配置文件web.xml,讀取到不同的節點時,WEB容器就會創建相應的過濾器、監聽器等為這個web項目服務。

如果你之前學過servlet肯定知道web.xml的加載順序為:context-param->listener->filter->servlet。


完整web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xmlns="http://java.sun.com/xml/ns/javaee"
  	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   	id="WebApp_ID" version="3.0">
  <display-name>ssm-crud</display-name>
  <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>
  
  <!-- 啟動Spring容器 -->
  <!-- needed for ContextLoaderListener -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- SpringMVC的前端控制器,攔截所有請求 -->
  <servlet>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!--
	  	<init-param>
	  		<param-name>contextConfigLoction</param-name>
	  		<param-value>location</param-value>
	  	</init-param>
  	 -->
  	 <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map all requests to the DispatcherServlet for.. -->
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 字符編碼過濾器,一定要放在所有過濾器之前 -->
  <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>forceRequestEncoding</param-name>
		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- Rest風格的URI 將頁面普通的post請求轉為指定的delete或者put請求 -->
  <filter>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<servlet-name>/*</servlet-name>
  </filter-mapping>
  
</web-app>

該項目的web.xml內容解析

在ssm-crud項目下打開web.xml

1.contextConfigLocation配置解析

如下代碼:

<!-- 啟動Spring容器 -->
  <!-- needed for ContextLoaderListener -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

作用:該元素用來聲明應用范圍(整個WEB項目)內的上下文初始化參數。
param-name 設定上下文的參數名稱。必須是唯一名稱
param-value 設定的參數名稱的值
contextConfigLocation 參數定義了要裝入的 Spring 配置文件。
與Spring相關的配置文件必須要以"applicationContext-"開頭,要符合約定優於配置的思想

看到有一篇相關的詳解:https://blog.csdn.net/tiantiandjava/article/details/8964912

和contextConfigLocation配合使用的是ContextLoaderListener
在myeclipse中使用快捷鍵 ctrl+shift+t可以直接打開它的源碼文件,如圖:
contextLoaderListener
可以看到,它是一個繼承於ContextLoader和實現了ServletContextListener接口的一個類。如有需要可以它們的源碼,這里不關注。
那么ContextLoaderListener的作用是什么?

ContextLoaderListener的作用就是啟動Web容器時,讀取在contextConfigLocation中定義的xml文件,自動裝配ApplicationContext的配置信息,並產生WebApplicationContext對象,然后將這個對象放置在ServletContext的屬性里,這樣我們只要得到Servlet就可以得到WebApplicationContext對象,並利用這個對象訪問spring容器管理的bean。
簡單來說,就是上面這段配置為項目提供了spring支持,初始化了Ioc容器。

作者:walidake 鏈接:https://www.jianshu.com/p/523bfddf0810 來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。

當然添加了以上配置后,還有在 src/main/resourse目錄下新建一個applicationContext.xml文件(new spring bean Definition)。
applicationContext.xml配置信息會在下一篇文章。

2.dispatcherServlet

<!-- SpringMVC的前端控制器,攔截所有請求 -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 如果不寫這里需要在web-inf文件夾里配置dispatcherServlet-servlet.xml文件
	  	<init-param>
	  		<param-name>contextConfigLoction</param-name>
	  		<param-value>location</param-value>
	  	</init-param>
  	 -->
  	 <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map all requests to the DispatcherServlet for.. -->
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  

配置DispatcherServlet表示,該工程將采用springmvc的方式。啟動時也會默認在/WEB-INF目錄下查找XXX-servlet.xml作為配置文件.

dispatcherServlet是名字名字,該文件中將配置兩項重要的mvc特性:HandlerMapping,負責為DispatcherServlet這個前端控制器的請求查找Controller;

這里有一篇對於dispatcherServlet更詳細的文章:https://juejin.im/post/58eb3c34b123db1ad06796c6

3.CharacterEncodingFilter 字符編碼過濾器

<!-- 字符編碼過濾器,一定要放在所有過濾器之前 -->
  <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>forceRequestEncoding</param-name>
		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

過濾器基礎知識

<filter>指定一個過濾器。
<filter-name>用於為過濾器指定一個名字,該元素的內容不能為空。
<filter-class>元素用於指定過濾器的完整的限定類名。
<init-param>元素用於為過濾器指定初始化參數,它的子元素<param-name>指定參數的名字,<param-value>指定參數的值。
在過濾器中,可以使用FilterConfig接口對象來訪問初始化參數。
<filter-mapping>元素用於設置一個 Filter 所負責攔截的資源。一個Filter攔截的資源可通過兩種方式來指定:Servlet 名稱和資源訪問的請求路徑
<filter-name>子元素用於設置filter的注冊名稱。該值必須是在<filter>元素中聲明過的過濾器的名字
<url-pattern>設置 filter 所攔截的請求路徑(過濾器關聯的URL樣式)

CharacterEncodingFilter類注釋源碼譯文片段:

Servlet過濾器,允許用戶為請求指定字符編碼。
*這很有用,因為當前的瀏覽器通常不設置字符
*編碼,即使在HTML頁面或表單中指定。
*如果請求尚未應用,則此過濾器可以應用其編碼
*指定編碼,或在任何情況下強制執行此過濾器的編碼
*(“forceEncoding”=“true”)。 在后一種情況下,編碼也將是
*作為默認響應編碼應用(雖然這通常會被覆蓋
*通過視圖中設置的完整內容類型)。

一句話總結:使用過濾器設置項目的編碼默認使用utf-8,至於其中的兩個參數forceRequestEncoding,forceResponseEncoding,是該類的邏輯要求,可以參考源碼:CharacterEncodingFilter

一篇關於filter 過濾器的文章:Filter 過濾器

4.HiddenHttpMethodFilter

  • URI (Uniform Resource Identifier)標識、定位任何資源的字符串
  • URL是URI的子集
<!-- Rest風格的URI 將頁面普通的post請求轉為指定的delete或者put請求 -->
  <filter>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<servlet-name>/*</servlet-name>
  </filter-mapping>

HiddenHttpMethodFilter類注釋源碼譯文片段:

  • {@link javax.servlet.Filter}將發布的方法參數轉換為HTTP方法,可通過{@link HttpServletRequest#getMethod()}檢索。由於瀏覽器目前只有 *支持GET和POST,這是一種常見技術 -
  • 例如Prototype庫使用 - 是使用帶有附加隱藏表單字段的普通POST( _method </ code>) 傳遞“真正的”HTTP方法。此過濾器讀取該參數並進行更改 * {@link HttpServletRequestWrapper#getMethod()}相應地返回值。

它的文章:HiddenHttpMethodFilter


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM