综述:

  Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central Servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s DispatcherServlet however, does more than just that. It is completely integrated with the Spring IoC container and as such allows you to use every other feature that Spring has.

  The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram. The pattern-savvy reader will recognize that the DispatcherServlet is an expression of the "Front Controller" design pattern (this is a pattern that Spring Web MVC shares with many other leading web frameworks).

  spring的web mvc框架,像其他的web mvc框架一样,请求驱动,围绕着一个中央servlet设计的,该中央servlet分发请求到控制器,并提供其他功能以促进web应用的发展。然而,spring的dispatcherServlet,做的不只这么多。它完全使用了spring ioc容器进行集成,正因如此才允许你使用spring拥有的所有特性

  spring mvc dispatcherservlet的请求处理流程,列举在下面的图表里。pattren-savvy的读者将会识别出DispatcherServlet前置控制器的表达模式(这是一个模式,那些spring web mvc与很多其他领先的web框架共享的)。

The request processing workflow in Spring Web MVC (high level)

The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application. You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping in the same web.xml file. This is standard Java EE Servlet configuration; the following example shows such a DispatcherServlet declaration and mapping:

spring web mvc的请求处理流程:

  DispatcherServlet实际上就是一个servlet,它继承自HttpServlet这个基础类,并且可以在你的web项目的web.xml中声明。你只需要将该控制器处理的请求分发给它即可,在同一个web.xml文件中,通过使用一个url映射。这是一个标准的java ee servlet配置,下面的例子说明了该servlet的声明和映射:

<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 the DispatcherServlet instance named example. In a Servlet 3.0+ environment, you also have the option of configuring the Servlet container programmatically. Below is the code based equivalent of the above web.xml example:

  上面的例子,所有以/example开头的请求都将会被名为example的DispatcherServlet处理。在servlet 3.0以上的环境中,你也可以通过编程方式代替配置该servlet的手段。下面的代码就是同上面web.xml示例相对应的代码。

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
public void onStartup(ServletContext container) {
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
registration.setLoadOnStartup(1);
registration.addMapping("/example/*");
} }
  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 AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply specifying its servlet mapping. See Code-based Servlet container initialization for more details.

  The above is only the first step in setting up Spring Web MVC. You now need to configure the various beans used by the Spring Web MVC framework (over and above the DispatcherServlet itself).

  As detailed in Section 5.15, “Additional Capabilities of the ApplicationContext”, ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.
  WebApplicationInitializer spring mvc提供的一个接口,确保你在编码中的配置能够被发现,并自动被使用以用来初始化servlet3容器。一个抽象的基础类实现该接口,被命名为
AbstractDispatcherServletInitializer使得它更容易去注册该DispatcherServlet通过简单的指定它的servlet映射。看Code-based Servlet container initialization有更多详细信息。
  上面的内容仅仅是设置spring-mvc的第一步,现在,你需要配置spring mvc framework使用的各种beans()。
  就像在5.15中描述的一样,应用上下文添加的能力,spring的应用上下文实例是能够被审查的。在web mvc 框架中,每个DispatcherServlet都有它自己的webApplicationContext,该~继承了在基础WebApplicationContext中定义的所有beans。这些继承的beans能够在servlet-specific scope中重写,你能够定义新的scope-specific beans 给servlet实例。

Figure 17.2. Context hierarchy in Spring Web MVC
Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-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之前,spring mvc在你的应用程序的WEB-INF目录中查找一个文件名servlet-name-servlet.xml,并根据定义的内容创建相应的beans,覆盖全局中同样名称的任何beans。

Consider the following DispatcherServlet Servlet configuration (in the web.xml file):

注意下面的DispatcherServlet配置

<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
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).

用上面的servlet配置替换,你需要一个叫做/WEB-INF/golfing-servlet.xml的文件在你的系统中,这个文件将会包含所有你spring mvc 特定的组件。你可以通过servlet的初始化参数配置,改变这个文件存放的地方。如下:

<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>
The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes (see Section 17.9, “Using themes”), and that it knows which Servlet it is associated with (by having a link to the ServletContext). The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext if you need access to it.

WebApplicationContext是简单ApplicationContext的一个扩展,有一些额外的特性是应用程序所需要的。与一个普通的ApplicationContext不同,在WebApplicationContext中包含了解析主题。Web被绑定在ServletContext中,通过使用RequestContextUtils类的静态方法,当你需要使用它时,你总能找到它。

17.2 The DispatcherServlet的更多相关文章

  1. spring Transaction Management --官方

    原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12.  ...

  2. Spring4参考手册中文版

    Spring4参考手册中文版 前言 https://github.com/b2gats/stone-docs/blob/master/spring-4-beans.md Part III. 核心技术 ...

  3. SpringMVC(3):DispatcherServlet详解

    原文出处: 张开涛 3.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供spring Web MVC的集中访问点,而且负责职责的分派,而且 ...

  4. SpringMVC源码分析(3)DispatcherServlet的请求处理流程

    <springmvc源码分析(2)dispatcherservlet的初始化>初始化DispatcherServlet的多个组件. 本文继续分析DispatcherServlet解析请求的 ...

  5. Spring5源码解析-论Spring DispatcherServlet的生命周期

    Spring Web框架架构的主要部分是DispatcherServlet.也就是本文中重点介绍的对象. 在本文的第一部分中,我们将看到基于Spring的DispatcherServlet的主要概念: ...

  6. 2016.3.23 集成新版activiti-modeler(5.17+)到项目中

    书:<activiti实战> 博客: http://www.kafeitu.me/activiti/2013/03/10/integrate-activiti-modeler.html h ...

  7. 集成新版(5.17+)Activiti Modeler与Rest服务

    声明: 此教程适合Activiti 5.17+版本. 本博客所涉及的内容均可在kft-activiti-demo中找到. 在线demo可以访问 http://demo.kafeitu.me:8080/ ...

  8. Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

    错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...

  9. 20191128 Spring Boot官方文档学习(9.11-9.17)

    9.11.消息传递 Spring Boot提供了许多包含消息传递的启动器.本部分回答了将消息与Spring Boot一起使用所引起的问题. 9.11.1.禁用事务JMS会话 如果您的JMS代理不支持事 ...

随机推荐

  1. iOS开发--图片处理

    纵观现实社会和移动app市场,这是一个看脸的时代,而好看且漂亮的APP界面就是移动APP的脸.漂亮的外观后面少不了UI设计人员的辛苦,如果不懂的处理,就浪费了UI设计人员的心血. 比如下面这张图片,是 ...

  2. FastSocket学习笔记~制定自已的传输协议~续~制定基于FastSocket的协议

    FastSocket这个东西上次我已经说过,它使用简单,功能强大,扩展灵活,目前在新浪的生产环境中已经被广泛使用,所以它的性能,安全等各方面我们绝对可以信赖,今天我们来说一个话题,和上一讲有关,这次我 ...

  3. (转)RabbitMQ消息队列(九):Publisher的消息确认机制

    在前面的文章中提到了queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consum ...

  4. Eclipse中tomcat之后,tomcat的相关配置会被Eclipse重置

    之前用MyEclipse,在tomcat的conf中修改了配置文件,启动就OK了. 现在改用Eclipse,发现改了,之后发现没有用,Eclipse重启tomcat之后,配置文件就被重置了. 众里寻他 ...

  5. 杭电ACM2096--小明A+B

    http://acm.hdu.edu.cn/showproblem.php?pid=2096 本来就是很简单.但是数据的大小有要求. (a%100+b%100)%100和(a+b)%100本来没有什么 ...

  6. 3月3日(4) Remove Duplicates from Sorted List

    原题 Remove Duplicates from Sorted List 有序单链表去重,delete 只能对指针起作用. /** * Definition for singly-linked li ...

  7. 比较不错的一个ios找茬游戏源码

    找茬游戏源码 ,这个是一款非常不错的ios找茬游戏源码,该游戏的兼容性非常好的,并且还可以支持ipad和iphone,UI界面设计得也很漂亮,游戏源码真的是一款非常完美,而且又很完整的一款休闲类的游戏 ...

  8. [windows phone开发]新生助手的开发过程与体会三

    由于网络原因,新生助手开发介绍的博客近期一直没有更新,请大家见谅.今天向大家介绍一下新生助手中动态磁帖的实现. 在PhoneApplicationPage中添加如下引用 xmlns:toolkit=& ...

  9. 弹性布局-flex

    浅谈display:flex   display:flex 意思是弹性布局 首先flex的出现是为了解决哪些问题呢? 一.页面行排列布局 像此图左右两个div一排显示 可以用浮动的布局方式 html部 ...

  10. 表达式语言之EL表达式

    1.EL的用法EL的起源:起源于JSTL.EL运算符: 算术型:+.-.*./.div.%.mod.其中/和div都表示求除.%和mod表示求余数. 逻辑型:and或&&.or或||. ...