SpringMVC配置

使用SpringMVC開發框架,最重要也是最核心的部分就是各種配置文件。配置文件稍有錯誤,極有可能造成整個項目啓動失敗或無法訪問。而SpringMVC又是包含着一個大的配置文件生態系統,比如使用日誌時的日誌配置、使用freemarker時的模板配置、使用數據源時的jdbc配置等等。

本文記錄一些重要的配置文件,並逐一完善各個配置文件,使項目能夠處理更加複雜的業務。

一、web.xml

web.xml存放在WEB-INF.xml目錄下,這裏有關於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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<display-name>Archetype Created Web Application</display-name>

	<!-- 
		1:指明配置文件位置,默認位置爲WEB-INF,默認文件名爲applicationContext.xml
		
		Q.	web.xml中classpath:和classpath*:  有什麼區別?
		A. 	classpath:只會到你的class路徑中查找找文件;   
			classpath*:不僅包含class路徑,還包括jar文件中(class路徑)進行查找.
		
		Q.	位於不同目錄下的配置文件,該如何定義?
		A.  src的情況,需要在web.xml中定義如下:  
			<context-param>  
				<param-name>contextConfigLocation</param-name>  
				<param-value>classpath:applicationContext.xml</param-value>  
			< /context-param>
			
			WEB-INF的情況,需要在web.xml中定義如下:  
			<context-param>  
				<param-name>contextConfigLocation</param-name>  
				<param-value>WEB-INF/applicationContext*.xml</param-value>  
			< /context-param>
		
		Q.	如何添加多個配置文件?
		A.	<context-param>   
				<param-name>contextConfigLocation</param-name>   
				<param-value>   
					classpath*:conf/applicationContext_core*.xml,   
					classpath*:conf/applicationContext_bean*.xml,   
					classpath*:conf/applicationContext_jdbc*.xml,   
					classpath*:conf/applicationContext_log*.xml   
				</param-value>   
			</context-param>
	-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
	        classpath*:applicationContext*.xml
	    </param-value>
	</context-param>

	<!-- 
		2:初始化配置監聽器
		
		ContextLoaderListener的作用在於,容器啓動時自動裝配applicationContext.xml(默認情況,可設置,參上)的配置信息
	 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 
		3:字符編碼攔截器,解決中文亂碼問題
		
		中文編碼亂碼問題,是所有中國程序員最常見的問題之一。在使用SpringMVC時,尤其做ajax請求,很容易出現亂碼問題。
		很多時候我們會很奇怪,明明在tomcat的server.xml中配置了<Connector URIEncoding="UTF-8" ...>,結果還是出現了亂碼。
		其實,這裏的配置只是對url進行了編碼處理,只要是get請求,參數都是通過url傳遞,是不會有亂碼問題的。但post請求時,
		因爲參數都在請求體裏,這裏的編碼設置並不能影響請求體編碼,所以就容易出現亂碼。
		
		如果是firefox瀏覽器,post請求會自動帶上請求頭信息:content-type = application/x-www-form-urlencoded; charset=UTF-8
		所以firefox下SpringMVC可能會表現良好,沒有亂碼問題;
		但如果是chrome瀏覽器,它不會設置關於編碼的請求頭信息:content-type = application/x-www-form-urlencoded ,
		而SpringMVC默認又是採用的ISO-8859-1解析參數,就會出現亂碼
		
		解決方案:
		a.	配置請求映射時:@RequestMapping(value = "saveUserByJson", produces = { "text/json;charset=UTF-8" })
		b.	全局過濾,即以下配置,無需自己寫過濾器,spring已經提供CharacterEncodingFilter
	-->
	<filter>
		<filter-name>forceEncoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>

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

	<!-- 
		4:攔截處理所有請求
		
		DispatcherServlet主要用作職責調度工作,本身主要用於控制流程,主要職責如下:
		a、文件上傳解析,如果請求類型是multipart將通過MultipartResolver進行文件上傳解析;
		b、通過HandlerMapping,將請求映射到處理器(返回一個HandlerExecutionChain,它包括一個處理器、多個HandlerInterceptor攔截器);
		c、通過HandlerAdapter支持多種類型的處理器(HandlerExecutionChain中的處理器);
		d、通過ViewResolver解析邏輯視圖名到具體視圖實現;
		e、本地化解析;
		f、渲染具體的視圖等;
		g、如果執行過程中遇到異常將交給HandlerExceptionResolver來解析。
		
		可以看出,使用SpringMVC時,幾乎大部分功能都是通過該servlet請求轉發的。
		
		DispatcherServlet默認是以WebApplicationContext作爲上下文的,默認的配置文件爲[servlet-name]-servlet.xml,
		比如這裏的servlet-name是spring,所以默認的配置文件應該是spring-servlet.xml,且默認情況下,這個文件應放在WEB-INF目錄下。
		
		也可以通過初始化參數來設置上下文的配置文件,方式如下。
	-->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet-config.xml</param-value>
		</init-param>
	</servlet>

	<!-- 
		5:映射攔截請求路徑
		
		url-pattern配置爲/,不帶文件後綴,會造成其它靜態文件(js,css等)不能訪問。如配爲*.do,則不影響靜態文件的訪問
	-->
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

	<!-- 
		6:指定系統歡迎頁
		
		該設置指定,當用戶訪問到目錄時,系統依次嘗試訪問以下頁面,直到發現可用的頁面
	 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
	</welcome-file-list>
	
	<!-- 
		7:錯誤頁面
		
		error-page配置用於在用戶請求資源時發生錯誤的情況下,提供默認的頁面以提示用戶。比如用戶請求了不存在的資源,
		如果不做設置,用戶就會看到簡單粗暴的404錯誤頁面;再比如系統實現中存在漏洞,在各種條件下觸發空指針異常,用戶就會
		看到一大段的異常堆棧。這些都是不友好的用戶體驗,而error-page則幫我們實現了在指定錯誤下跳轉到指定頁面的功能。
		
		error-code指出在給定http錯誤碼返回時,要展示給用戶的頁面;而exception-type則表示在系統出現某種異常時,要展示給
		用戶的頁面。
	 -->
	<error-page>  
		<error-code>404</error-code>  
		<location>/WEB-INF/common/404.html</location>  
	</error-page>
	
	<error-page>  
		<error-code>500</error-code>  
		<location>/WEB-INF/common/500.html</location>  
	</error-page>
	
	<error-page>
		<exception-type>java.lang.NullPointerException</exception-type>  
		<location>/WEB-INF/common/nullpointer.html</location>  
	 </error-page>
	 
	  
	<!-- 
		8:設置session過期時長
		
		當session查出session-timeout指定的時長時,整個session就會過期。
		session-timeout的單位是分鐘,所以下面的配置session會在用戶3小時無操作時過期
	-->
	<session-config>
		<session-timeout>180</session-timeout>
	</session-config>

</web-app>

二、spring-servlet.xml

spring-servlet.xml並不一定是spring-servlet.xml,它的文件名對應於DispatcherServlet配置的servlet-name屬性,其中spring就是servlet-name的取值。如果沒有對該servlet進行初始化參數的配置,這個配置文件應該存放在WEB-INF目錄下。

在SpringMVC中,applicationContext是mvc context的父容器,mvc context可以引用到applicationContext中的bean,反之則不行。mvc context主要用於配置控制器、攔截器以及適配器相關的設置,它是針對web層的;而applicationContext則用於對整個應用進行配置,比如dao層、service層等等,它不關注表現層。

spring在查找bean時,如果能在當前上下文中找到,則返回,否則就會到父容器中查找。








發佈了40 篇原創文章 · 獲贊 6 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章