DispatchServlet
关键词
-
- spring MVC
- DispatchSevlet
- url mapping两种配置方法:codebased配置;web.xml中配置
- WebApplicationContext
- 它其实就是[servlet-name]-sevlet.xml
- WebApplicationContext相对于普通ApplicationContext的特殊之处(特殊的beans)
1.概述:
-
- The
DispatcherServlet
is an actualServlet
(it inherits from theHttpServlet
base class) - 它用于拦截用户请求,并且将请求交给相应的handler处理(即交给@Controller @RequestMapping类来处理)
- 要想你的DispatchServlet能够拦截到用户请求,你还得在你的project中添加一些配置信息。
- 添加URL mapping 配置信息,DispatchServlet才能够拦截到相应的URL请求
- 在你的project中添加URL mapping配置信息有多种方法,具体介绍请看本文第二小节内容。
- DispatchServlet还可以提供用于搭建网站的基础功能: offers other functionality that facilitates the development of web applications.
- In the Web MVC framework, each
DispatcherServlet
has its ownWebApplicationContext
, - The
WebApplicationContext
is an extension of the plainApplicationContext
that has some extra features necessary for web applications. - WebApplicationContext实际上就是存放在/WEB-INF/文件夹下的[sevlet-name]-sevlet.xml 其中servlet-name是DispatchServlet实例的名字,该文件中定义了web 相关的各个bean。
- WebApplicationContext之类的IOC容器是可以被spring MVC框架自动识别的,并且在初始化DispatchServlet的时候初始化所有WebApplicationContext,并且创建里面所定义的beans。
- 并且每个
DispatcherServlet
的WebApplicationContext都继承了
all the beans already defined in the rootWebApplicationContext
. - The root
WebApplicationContext
should contain all the infrastructure beans that should be shared between your other contexts and Servlet instances.正是这些beans 组成了spring MVC框架的一部分。我们使用spring MVC来开发web project ,实际上也就是使用的root WebApplicationContext中所定义的这些beans. - beans in root WebApplicationContext can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.
- 更多关于WebApplicationContext的信息参见本文第三小节内容
- In the Web MVC framework, each
- The
2.配置DispatchServlet,使得它能够拦截相应的请求 (也即URL mapping)
概述:
有多种方法可以使得DispatcherServlet能够拦截相应的URL请求(即拦截类似“/example/*”之类的请求)
方法一,使用code-based configuration
-
-
-
-
/** * @author chen * @description 此类用于初始化spring web mvc中的DispatchServlet * 使得"/example/*"之类的url请求可以被dispatchServlet拦截 * */ public class InitializeSpringMVC implements WebApplicationInitializer{ @Override public void onStartup(ServletContext container) { ServletRegistration.Dynamic registration = container.addServlet("example", new DispatcherServlet()); registration.setLoadOnStartup(1); registration.addMapping("/example/*"); } }
In the preceding example, all requests starting with
/example
will be handled by theDispatcherServlet
instance namedexample
. -
WebApplicationInitializer
is an interface provided by Spring MVC that ensures your code-based configuration is detected and automatically used to initialize any Servlet 3 container. -
An abstract base class implementation of this interface named
AbstractAnnotationConfigDispatcherServletInitializer
makes it even easier to register theDispatcherServlet
by simply specifying its servlet mapping and listing configuration classes - it’s even the recommended way to set up your Spring MVC application. See Code-based Servlet container initialization for more details.
-
-
-
方法二,使用web.xml来配置URL mapping
-
-
-
-
<web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example/*</url-pattern> </servlet-mapping> </web-app>
In the preceding example, all requests starting with
/example
will be handled by theDispatcherServlet
instance namedexample
. -
备注:在web.xml中生成DispatchServlet实例,并且配置URL mapping的时候,还可以定义DispatchServlet的一些初始化参数init-param ,DispatchServlet的init-param有三种,分别为contextClass、contextConfigLocation、namespace
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
Table 22.2. DispatcherServlet initialization parameters
Parameter Explanation contextClass
Class that implements
WebApplicationContext
, which instantiates the context used by this Servlet. By default, theXmlWebApplicationContext
is used.contextConfigLocation
String that is passed to the context instance (specified by
contextClass
) to indicate where context(s) can be found. The string consists potentially of multiple strings (using a comma as a delimiter) to support multiple contexts. In case of multiple context locations with beans that are defined twice, the latest location takes precedence.namespace
Namespace of the
WebApplicationContext
. Defaults to[servlet-name]-servlet
.
-
-
-
3.每个DispatchServlet实例都有他自己的IOC容器——WebApplicationContext
概述:
-
- In the Web MVC framework, each
DispatcherServlet
has its ownWebApplicationContext
, - The
WebApplicationContext
is an extension of the plainApplicationContext
that has some extra features necessary for web applications. -
WebApplicationContext实质上也是一个IOC容器,和Spring framework中的ApplicationContext一样,都是用于管理相应的beans的,只不过
WebApplicationContext和普通的ApplicationContext相比,它有一些鲜明的特色,如:
首先,xml-based configuration中,WebApplicationContext必须是[servlet-name]-servlet.xml命名的。其中,[sevlet-name]是DispatchServlet实例的名称。
其次,WebApplicationContext这样的IOC容器会被系统直接识别并且初始化,创建里面所定义的beans.因为Upon initialization of a
DispatcherServlet
, Spring MVC looks for a file named [servlet-name]-servlet.xml in theWEB-INF
directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.- 并且每个
DispatcherServlet
的WebApplicationContext都继承了
all the beans already defined in the rootWebApplicationContext
.也就是说所有的对应于单个DispatcherServlet实例的WebApplicationContext,都继承自rootWebApplicationContext
- root
WebApplicationContext提供了许多beans,这些beans 是用于搭建网站的基础bean,有了这些基础bean,开发者再去搭建网站时就会少了很多工作量
-
Table 22.1. Special bean types in the WebApplicationContext
Bean type Explanation Maps incoming requests to handlers and a list of pre- and post-processors (handler interceptors) based on some criteria the details of which vary by
HandlerMapping
implementation. The most popular implementation supports annotated controllers but other implementations exists as well.HandlerAdapter
Helps the
DispatcherServlet
to invoke a handler mapped to a request regardless of the handler is actually invoked. For example, invoking an annotated controller requires resolving various annotations. Thus the main purpose of aHandlerAdapter
is to shield theDispatcherServlet
from such details.Maps exceptions to views also allowing for more complex exception handling code.
Resolves logical String-based view names to actual
View
types.Resolves the locale a client is using and possibly their time zone, in order to be able to offer internationalized views
Resolves themes your web application can use, for example, to offer personalized layouts
Parses multi-part requests for example to support processing file uploads from HTML forms.
Stores and retrieves the "input" and the "output"
FlashMap
that can be used to pass attributes from one request to another, usually across a redirect. - 如上所述,都是root WebApplicationContext中定义的beans,它们构成了spring MVC框架的一些功能,如提供theme等等。这些beans可以被DispatchServlet实例对应的WebApplicationContext中定义的bean覆盖掉。
- 实际的web project中,
- (类型一)可以只有一个DispatchServlet,只有一个The
WebApplicationContext
, - (类型二)也可以有多个DispatchServlet实例,且每个DispatchServlet实例都有各自的WebApplicationContext,且所有WebApplicationContext都继承自同一个root WebApplicationContext
- In the Web MVC framework, each
类型一,实际web project中,只有一个DispatchServlet对象和一个The WebApplicationContext
-
-
- It is also possible to have just one root context for single DispatcherServlet scenario,这种web project中的ApplicationContext的继承关系如下图:
-
- 实际编程思路:
-
概述,要实现只有一个DispatchServlet实例和一个WebApplicationContext的web project,有多种方法
- 方法一,xml-based configuration
- step1,在web.xml中创建DispatchServlet实例,并且配置URL mapping
-
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
如上配置,
-
-
唯一一个DispatchServlet实例dispatcher,唯一一个WebApplicationContext,还有root WebApplicationContext。
-
所有/*请求都会被DispatchServlet实例dispatcher拦截。
-
并且名为dispatcher的DispatchServlet实例对应的WebApplicationContext为 /WEB-INF/root-context.xml
-
-
- step2,编写DispatchServlet实例对应的WebApplicationContext
- 方法二,java-based configuration
-
public class GolfingWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { // GolfingAppConfig defines beans that would be in root-context.xml return new Class[] { GolfingAppConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { // GolfingWebConfig defines beans that would be in golfing-servlet.xml return new Class[] { GolfingWebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/golfing/*" }; } }
-
-
- It is also possible to have just one root context for single DispatcherServlet scenario,这种web project中的ApplicationContext的继承关系如下图:
-
类型二,实际web project中,有多个DispatchServlet对象和多个The WebApplicationContext
-
-
- WebApplicationContext的继承关系,如下图:
-
-
实际编程:
-
step1,为这种类型的web project配置URL mapping
-
<web-app> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>/golfing/*</url-pattern> </servlet-mapping> </web-app>
所有/golfing/*的请求都会被一个名称为golfing的DispatchServlet实例所拦截,并且根据request mapping分发到相应的handler(controller)
-
-
step2,上面已经在web.xml中创建了DispatchServlet实例,并且为该实例配置了URL mapping,下面讲述如何为该DispatchServlet实例添加它自己的WebApplicationContext
-
-
With the above Servlet configuration in place, you will need to have a file called
/WEB-INF/golfing-servlet.xml
in your application; this file will contain all of your Spring Web MVC-specific components (beans). -
You can change the exact location of this configuration file through a Servlet initialization parameter (see below for details).
-
-
-
- WebApplicationContext的继承关系,如下图:
-