Spring学习手札(四)配置DispatcherServlet

时间:2022-06-09 04:12:29

本文描述了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:

Spring学习手札(四)配置DispatcherServlet

附上模板工程自带的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:codeevoship@gmail.com
转载请注明出处,谢谢。
由于文中在发布后难免会有勘误或补充,推荐到本博客中阅读本文。