Spring學習手札(四)配置DispatcherServlet

本文描述了web.xml最基本配置方式。

Spring MVC的核心是DispatcherServlet,作爲Spring MVC的前端控制器;

和任何Servlet一樣,我們需要在web.xml文件中配置DispatcherServlet;

下面的描述以這個web.xml爲例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/iSystem-beans.xml,
		</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>iSystem</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/iSystem-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>iSystem</servlet-name>
		<url-pattern>/</url-pattern>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

</web-app>
在其中的<servlet>標籤下,定義了DispatcherServlet。

重要:在DispatcherServlet載入後,它將從XML文件中載入Spring的應用上下文,這個XML文件的名字取決於Servlet的名字,或者由init-param來指定。

如果我們不設置init-param,形如:

	<servlet>
		<servlet-name>iSystem</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

這時,DispatcherServlet載入後將默認去加載  /WEB-INF/iSystem-servlet.xml文件,iSystem就是上面定義的servlet-name。

如果沒有這個文件,將會報錯:

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/iSystem-servlet.xml]

因此我們如果省略init-param的話,需要按下圖放置該xml:


附上模板工程自帶的servlet.xml文件內容:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="com.codeevoship.app" />
</beans:beans>

接下來再爲DispatcherServlet指定需要處理的URL:

	<servlet-mapping>
		<servlet-name>iSystem</servlet-name>
		<url-pattern>/</url-pattern>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>
當然這裏還可以加上*.png、*.gif等等等等……

到這裏DispatcherServlet的配置就完成了,這裏再說說其他內容。

雖然我們已經加載了iSystem-servlet.xml,雖然可以把全項目所有的bean全都定義在這一個xml中……

推薦分層、分模塊分別定義,比如iSystem-security.xml/iSystem-data.xml/iSystem-service.xml……

這就用到了上下文載入器,最常用的ContextLoaderListener:

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
這個載入器可以載入除DispatcherServlet載入的配置文件之外的其他上下文配置文件,通過下述方式:

        <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/iSystem-security.xml,
			/WEB-INF/iSystem-data.xml,
		</param-value>
	</context-param>
param-value中就是需要加載的文件,以逗號分隔。

以上就是web.xml的最基本配置。

參考:《Spring in Action》

Author:Pirate Leo
blog:http://blog.csdn.net/pirateleo
email:[email protected]
轉載請註明出處,謝謝。
由於文中在發佈後難免會有勘誤或補充,推薦到本博客中閱讀本文。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章