The Workflow
A high-level overview of the request processing workflow
A more complete view of the request processing workflow
Prepare a Request
Before the DispatcherServlet will start dispatching and handling the request, it first does some
preparation and preprocessing of the request. The servlet starts by determining and exposing the
current java.util.Locale for the current request using the org.springframework.web.servlet
.LocaleResolver. Next, it prepares and exposes the current request in org.springframework.web
.context.request.RequestContextHolder. This gives the framework code easy access to the current
request, instead of passing it around.
Next, the servlet will construct a so-called org.springframework.web.servlet.FlashMap. It does this
by calling the org.springframework.web.servlet.FlashMapManager, which will try to resolve the input
FlashMap. This map contains attributes that were explicitly stored in the previous request. In general, this is used when a redirect is made to go to the next page.
Next, the incoming request is checked for whether it is a multipart HTTP request (this is used when
doing file uploads). If so, the request is wrapped in an org.springframework.web.multipart
.MultipartHttpServletRequest by passing it through an org.springframework.web.multipart
.MultipartResolver. After this, the request is ready to be dispatched to the correct handler.
Determine the HandlerExecutionChain
A couple of components are involved in dispatching the request . When a request is ready
for dispatching, the DispatcherServlet will consult one or more org.springframework.web.servlet
.HandlerMapping implementations to determine which handler can handle the request. If no handler is
found, an HTTP 404 response is send back to the client. The HandlerMapping returns an
org.springframework.web.servlet.HandlerExecutionChain (you will learn more about this in the next
section). When the handler has been determined, the servlet will attempt to find an org.springframework.web.servlet.HandlerAdapter to actually execute the found handler. If no suitable HandlerAdapter can be found, a javax.servlet.ServletException is thrown.
Execute the HandlerExecutionChain
To handle the request, the DispatcherServlet uses the HandlerExecutionChain to determine what to
execute. This class holds a reference to the actual handler that needs to be invoked; however, it also
(optionally) references org.springframework.web.servlet.HandlerInterceptor implementations that are
executed before (preHandle method) and after (postHandle method) the execution of the handler. These interceptors can be used to apply crosscutting functionality. If the code executes successfully, the interceptors are called again; and finally, when needed, the view is rendered.
The execution of the handler is delegated to the selected HandlerAdapter that was determined in the
previous step. It knows how to execute the selected handler and how to translate the response into an
org.springframework.web.servlet.ModelAndView. If there is no view in the returned ModelAndView, an
org.springframework.web.servlet.RequestToViewNameTranslator is consulted to generate a view name
based on the incoming request.
Handle Exceptions
When an exception is thrown during the handling of the request, the DispatcherServlet will consult the
configured org.springframework.web.servlet.HandlerExceptionResolvers to handle the thrown
exception. The resolver can translate the exception to a view to show the user. For instance, if there is an exception related to database errors, you could show a page indicating the database is down. If the
exception isn’t resolved, it is rethrown and handled by the servlet container, which generally results in
an HTTP 500 response code (internal server error).
Render a View
If a view has been selected during the request processing workflow, the DispatcherServlet first checks
whether it is a view reference (this is the case if the view is a java.lang.String). If so, the configured
org.springframework.web.servlet.ViewResolvers are consulted to resolve the view reference to an
actual org.springframework.web.servlet.View implementation. If there is no view and one cannot be
resolved, a javax.servlet.ServletException is thrown.
Finish the Processing
Each incoming request passes through this step of the request processing flow, regardless of whether
there are exceptions. If an HandlerExecutionChain is available, then the afterCompletion method of the
interceptors is called. Only the interceptors where the preHandle method was successfully invoked will
have their afterCompletion method called. Next, these interceptors are executed in the reverse order
that their preHandle method was called. This mimics the behavior seen in servlet filters, where the first
filter called is also the last one to be called.
Finally, the DispatcherServlet uses the event mechanism in the Spring Framework to fire an
org.springframework.web.context.support.RequestHandledEvent. You could create and
configure an org.springframework.context.ApplicationListener to receive and log these events.
The Request Processing Summary
The DispatcherServlet is a key component in processing requests with Spring MVC. It is also highly
flexible and configurable. This flexibility comes from the fact that the servlet uses a lot of different
components to fulfill its role, and these components are expressed as interfaces.
The DispatcherServlet Components Used in Request Processing Workflow
相关推荐
Spring MVC 是一个基于Java的轻量级Web应用框架,它为开发者提供了模型-视图-控制器(MVC)架构,使开发人员能够更好地组织和分离应用程序的业务逻辑、数据处理和用户界面。Spring MVC是Spring框架的一个核心组件,...
四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、spring mvc 如何访问到静态的文件,如jpg,js,css? 八、spring mvc 请求如何映射到具体的...
四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、spring mvc 如何访问到静态的文件,如jpg,js,css? 八、spring mvc 请求如何映射到具体的...
二、Spring MVC核心类与接口:Spring MVC架构中包含许多核心组件,如DispatcherServlet、HandlerMapping、Controller、ViewResolver等。这些组件协同工作,处理用户的请求并返回相应的响应。 三、Spring MVC核心...
Spring MVC通过DispatcherServlet作为入口点,接收HTTP请求,然后根据请求映射信息分发到相应的处理器。 在Spring MVC 4.2.3中,主要包含了以下关键特性: 1. **类型安全的路径变量**:这个版本引入了类型安全的...
首先,Spring MVC的基础架构包括DispatcherServlet(前端控制器)、Model、View和Controller。DispatcherServlet是整个流程的入口,负责接收请求并分发到相应的Controller。Controller是业务逻辑处理的核心,Model...
14. **性能优化**:Spring MVC 4.0在内部做了许多性能优化,包括更快的DispatcherServlet初始化、更高效的HTTP方法映射等。 15. **兼容性与兼容性测试**:Spring MVC 4.0确保与各种Web容器的兼容性,如Tomcat、...
在Spring MVC中,每个请求都会经过DispatcherServlet,这是一个前端控制器,它会根据请求的URL和配置的映射规则将请求分发到合适的处理器。处理器可以是自定义的Controller类,Controller通过方法注解(如@...
总的来说,"Mastering Spring MVC 4(2015.09)源码"提供了深入学习Spring MVC的机会,你可以通过阅读和分析源码来了解如何配置DispatcherServlet、怎样编写控制器、如何进行数据绑定与验证,以及如何利用拦截器等特性...
14. **Asynchronous Request Processing**: Spring MVC支持异步请求处理,可以通过`@Async`注解实现后台任务的并发执行。 15. **Internationalization (i18n) and Localization (l10n)**: Spring MVC提供对国际化和...
通过DispatcherServlet作为前端控制器,Spring MVC能够灵活地调度请求到相应的处理器,并且支持多种视图技术如JSP、Thymeleaf等。 **2. Mybatis** Mybatis是一个轻量级的持久层框架,它简化了Java应用与数据库之间...
- 配置Spring MVC:在Spring的配置文件中,我们需要定义DispatcherServlet、视图解析器和处理器映射器等。 - 集成MyBatis:引入MyBatis的依赖,配置SqlSessionFactoryBean,创建MapperScannerConfigurer扫描Mapper...
DispatcherServlet 是 Spring MVC 框架的核心组件,它负责转发每一个 Request 请求给相应的 Handler,Handler 处理以后再返回相应的视图(View)和模型(Model)。DispatcherServlet 是继承自 HttpServlet 的,既然 ...
3. `org.springframework.web.servlet-3.0.2.RELEASE.jar`:这是 Spring MVC 的核心模块,提供了控制器(Controller)、模型视图(ModelAndView)以及调度器Servlet(DispatcherServlet)等关键组件。...
Spring MVC通过DispatcherServlet作为前端控制器,接收请求并分发给相应的处理器(Controller)。处理器执行业务逻辑后,将结果返回给ModelAndView对象,再由视图解析器渲染视图。此外,Spring MVC还支持注解驱动,...
标题中的"开发Spring MVC应用程序补充—程序源码下载.rar_spring_spring mvc_spring mvc 源码_sp"表明这是一个关于Spring MVC框架的开发教程,其中包含了源代码供学习者参考。Spring MVC是Spring框架的一个核心组件...
Spring MVC的核心概念包括DispatcherServlet、Controller、Model、View和ViewModel。以下将详细阐述这些关键知识点: 1. **DispatcherServlet**:这是Spring MVC的前端控制器,负责接收HTTP请求,解析请求参数,...
在理解Spring MVC的工作原理时,我们需要知道DispatcherServlet的角色。它是Spring MVC的前端控制器,接收所有HTTP请求,然后根据请求信息选择合适的HandlerMapping找到对应处理器(Controller方法)。处理器执行后...