Processing
-
加载必要的上下文元素,包括 WebApplicationContext 和解析器(语言环境、主题、多部分文件)。
-
搜索并执行适当的处理器,并准备一个模型进行渲染。
-
处理异常,使用 HandlerExceptionResolver 进行自定义解析。
-
提供 HTTP 缓存支持,允许处理器控制缓存行为。
-
通过 Servlet 初始化参数来自定义 DispatcherServlet 实例,包括上下文配置、命名空间和异常处理行为。
DispatcherServlet
处理请求的步骤如下:
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 theDispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE
key. -
The locale resolver is bound to the request to let elements in the process 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 the locale resolver.
-
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 run to prepare a model for rendering. Alternatively, for annotated controllers, the response can be rendered (within the
HandlerAdapter
) instead of returning a view. -
If a model is returned, the view is rendered. If no model is returned (maybe 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.
在`WebApplicationContext`中声明的`HandlerExceptionResolver`Bean 用于解析请求处理期间引发的异常。那些异常解析器允许自定义处理异常的逻辑。有关更多详细信息,请参阅Exceptions。
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.
为了支持 HTTP 缓存,处理器可以使用 WebRequest
的 checkNotModified
方法,以及 HTTP Caching for Controllers 中所述的带注释控制器的其他选项。
For HTTP caching support, handlers can use the checkNotModified
methods of WebRequest
,
along with further options for annotated controllers as described in
HTTP Caching for Controllers.
你可以通过向 web.xml
文件中的 Servlet 声明添加 Servlet 初始化参数(init-param
元素)来自定义各个 DispatcherServlet
实例。下表列出了受支持的参数:
You can customize individual DispatcherServlet
instances by adding Servlet
initialization parameters (init-param
elements) to the Servlet declaration in the
web.xml
file. The following table lists the supported parameters:
Parameter | Explanation |
---|---|
|
Class that implements |
|
String that is passed to the context instance (specified by |
|
Namespace of the |
|
Whether to throw a As of 6.1, this property is set to Note that, if default servlet handling is also configured, unresolved requests are always forwarded to the default servlet and a 404 is never raised. |