没有合适的资源?快使用搜索试试~ 我知道了~
首页Spring Web MVC外文翻译
资源详情
资源评论
资源推荐
1
Spring Web MVC
1.1. Introduction
Spring Web MVC is the original web framework built on the Servlet API and
included in the Spring Framework from the very beginning. The formal name "Spring
Web MVC" comes from the name of its source module spring-webmvc but it is more
commonly known as "Spring MVC".
Parallel to Spring Web MVC, Spring Framework 5.0 introduced a reactive stack,
web framework whose name Spring WebFlux is also based on its source module
spring-webflux. This section covers Spring Web MVC.
1.2. DispatcherServlet
Spring MVC, like many other web frameworks, is designed around the front
controller pattern where a central Servlet, the DispatcherServlet, provides a shared
algorithm for request processing while actual work is performed by configurable,
delegate components. This model is flexible and supports diverse workflows.
The DispatcherServlet, as any Servlet, needs to be declared and mapped
according to the Servlet specification using Java configuration or in web.xml. In turn
the DispatcherServlet uses Spring configuration to discover the delegate components
it needs for request mapping, view resolution, exception handling, and more.
1.2.1. Context Hierarchy
DispatcherServlet expects a WebApplicationContext, an extension of a plain
ApplicationContext, for its own configuration. WebApplicationContext has a link to
the ServletContext and Servlet it is associated with. It is also bound to the
ServletContext such that applications can use static methods on RequestContextUtils
to look up the WebApplicationContext if they need access to it.
For many applications having a single WebApplicationContext is simple and
sufficient. It is also possible to have a context hierarchy where one root
WebApplicationContext is shared across multiple DispatcherServlet (or other Servlet)
instances, each with its own child WebApplicationContext configuration. See
Additional Capabilities of the ApplicationContext for more on the context hierarchy
feature.
1.2.2. Special Bean Types
The DispatcherServlet delegates to special beans to process requests and render
the appropriate responses. By "special beans" we mean Spring-managed, Object
instances that implement WebFlux framework contracts. Those usually come with
built-in contracts but you can customize their properties, extend or replace them.
1.2.3. Web MVC Config
2
Applications can declare the infrastructure beans listed in Special Bean Types
that are required to process requests. The DispatcherServlet checks the
WebApplicationContext for each special bean. If there are no matching bean types, it
falls back on the default types listed in DispatcherServlet.properties.
In most cases the MVC Config is the best starting point. It declares the required
beans in either Java or XML, and provides a higher level configuration callback API
to customize it.
1.2.4. Servlet Config
WebApplicationInitializer is an interface provided by Spring MVC that ensures
your implementation is detected and automatically used to initialize any Servlet 3
container. An abstract base class implementation of WebApplicationInitializer named
AbstractDispatcherServletInitializer makes it even easier to register the
DispatcherServlet by simply overriding methods to specify the servlet mapping and
the location of the DispatcherServlet configuration.
Each filter is added with a default name based on its concrete type and
automatically mapped to the DispatcherServlet.
The isAsyncSupported protected method of AbstractDispatcherServletInitializer
provides a single place to enable async support on the DispatcherServlet and all filters
mapped to it. By default this flag is set to true.
Finally, if you need to further customize the DispatcherServlet itself, you can
override the createDispatcherServlet method.
1.2.5. Processing
Same in Spring WebFlux
The DispatcherServlet processes requests as follows:
The WebApplicationContext is searched for and bound in the request as an
attribute that the controller and other elements in the process can use. It is bound by
default under the key.
The locale resolver is bound to the request to enable elements in the process to
resolve the locale to use when processing the request (rendering the view, preparing
data, and so on). If you do not need locale resolving, you do not need it.
The theme resolver is bound to the request to let elements such as views
determine which theme to use. If you do not use themes, you can ignore it.
If you specify a multipart file resolver, the request is inspected for multiparts; if
multiparts are found, the request is wrapped in a MultipartHttpServletRequest for
further processing by other elements in the process. See Multipart resolver for further
information about multipart handling.
An appropriate handler is searched for. If a handler is found, the execution chain
associated with the handler (preprocessors, postprocessors, and controllers) is
3
executed in order to prepare a model or rendering. Or alternatively for annotated
controllers, the response may be rendered (within the HandlerAdapter) instead of
returning a view.
If a model is returned, the view is rendered. If no model is returned, (may be due
to a preprocessor or postprocessor intercepting the request, perhaps for security
reasons), no view is rendered, because the request could already have been fulfilled.
The HandlerExceptionResolver beans declared in the WebApplicationContext
are used to resolve exceptions thrown during request processing. Those exception
resolvers allow customizing the logic to address exceptions. See Exceptions for more
details.
The Spring DispatcherServlet also supports the return of the last-modification-
date, as specified by the Servlet API. The process of determining the last modification
date for a specific request is straightforward: the DispatcherServlet looks up an
appropriate handler mapping and tests whether the handler that is found implements
the LastModified interface. If so, the value of the long getLastModified(request)
method of the LastModified interface is returned to the client.
You can customize individual DispatcherServlet instances by adding Servlet
initialization parameters ( init-param elements) to the Servlet declaration in the
web.xml file. See the following table for the list of supported parameters.
1.2.6. Interception
All HandlerMapping implementations supports handler interceptors that are
useful when you want to apply specific functionality to certain requests, for example,
checking for a principal. Interceptors must implement HandlerInterceptor from the
org.springframework.web.servlet package with three methods that should provide
enough flexibility to do all kinds of pre-processing and post-processing:
preHandle(..) — before the actual handler is executed
postHandle(..) — after the handler is executed
afterCompletion(..) — after the complete request has finished
The preHandle(..) method returns a boolean value. You can use this method to
break or continue the processing of the execution chain. When this method returns
true, the handler execution chain will continue; when it returns false, the
DispatcherServlet assumes the interceptor itself has taken care of requests (and, for
example, rendered an appropriate view) and does not continue executing the other
interceptors and the actual handler in the execution chain.
See Interceptors in the section on MVC configuration for examples of how to
configure interceptors. You can also register them directly via setters on individual
HandlerMapping implementations.
Note that postHandle is less useful with @ResponseBody and ResponseEntity
methods for which the response is written and committed within the HandlerAdapter
4
and before postHandle. That means its too late to make any changes to the response
such as adding an extra header. For such scenarios you can implement
ResponseBodyAdvice and either declare it as an Controller Advice bean or configure
it directly on RequestMappingHandlerAdapter.
1.2.7. Exceptions
@Controller
DispatcherServlet
HandlerExceptionResolver
Chain of resolvers
HandlerExceptionResolver
order
HandlerExceptionResolver
!
"#$
%"#$
&
"$''(
"$')*
)%+,
'
HandlerExceptionResolver
-./0
+ "1
2
3%**4*
5
5*1-6760
8
)'
94:
;<=$*
"$'$*$
$*
$
*
$*
+9>
*$*
9>*8
*$*
$*
"$''
#>$*
($'+ "1
*
redirect:
UrlBasedViewResolver-(0,
5*1
?
RedirectView
#
redirect:/myapp/some/resource
剩余26页未读,继续阅读
旧巷子里的猫
- 粉丝: 8
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3