1. 程式人生 > >Spring學習手札(四)配置DispatcherServlet

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》