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內容解析
在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
可以直接開啟它的原始碼檔案,如圖:
可以看到,它是一個繼承於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()}相應地返回值。
完整web.xml
在ssm-crud的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>
END