java.lang.Object | |||||
↳ | javax.servlet.GenericServlet | ||||
↳ | javax.servlet.http.HttpServlet | ||||
↳ | org.springframework.web.servlet.HttpServletBean | ||||
↳ | org.springframework.web.servlet.FrameworkServlet | ||||
↳ | org.springframework.web.servlet.DispatcherServlet |
Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception handling facilities.
This servlet is very flexible: It can be used with just about any workflow, with the installation of the appropriate adapter classes. It offers the following functionality that distinguishes it from other request-driven web MVC frameworks:
HandlerMapping
implementation - pre-built or provided as part of an application - to
control the routing of requests to handler objects. Default is BeanNameUrlHandlerMapping
and DefaultAnnotationHandlerMapping
. HandlerMapping objects
can be defined as beans in the servlet's application context, implementing the HandlerMapping interface, overriding
the default HandlerMapping if present. HandlerMappings can be given any bean name (they are tested by type).
HandlerAdapter
; this allows for using any handler interface. Default adapters are HttpRequestHandlerAdapter
, SimpleControllerHandlerAdapter
,
for Spring's HttpRequestHandler
and Controller
interfaces, respectively. A default AnnotationMethodHandlerAdapter
will be registered as well. HandlerAdapter objects can be added as beans in the application context, overriding the
default HandlerAdapters. Like HandlerMappings, HandlerAdapters can be given any bean name (they are tested by type).
HandlerExceptionResolver
, for
example mapping certain exceptions to error pages. Default are
AnnotationMethodHandlerExceptionResolver
,
ResponseStatusExceptionResolver
, and
DefaultHandlerExceptionResolver
. These HandlerExceptionResolvers can be overridden
through the application context. HandlerExceptionResolver can be given any bean name (they are tested by type).
ViewResolver
implementation, resolving symbolic view
names into View objects. Default is InternalResourceViewResolver
.
ViewResolver objects can be added as beans in the application context, overriding the default ViewResolver.
ViewResolvers can be given any bean name (they are tested by type).
View
or view name is not supplied by the user, then the configured RequestToViewNameTranslator
will translate the current request into a view name. The corresponding bean name is
"viewNameTranslator"; the default is DefaultRequestToViewNameTranslator
.
MultipartResolver
implementation. Implementations for Jakarta Commons FileUpload
and Jason Hunter's COS are included; the typical choise is CommonsMultipartResolver
.
The MultipartResolver bean name is "multipartResolver"; default is none.
LocaleResolver
. Out-of-the-box implementations work via
HTTP accept header, cookie, or session. The LocaleResolver bean name is "localeResolver"; default is AcceptHeaderLocaleResolver
.
ThemeResolver
. Implementations for a fixed theme and for
cookie and session storage are included. The ThemeResolver bean name is "themeResolver"; default is FixedThemeResolver
. NOTE: The @RequestMapping
annotation will only be processed if a corresponding
HandlerMapping
(for type level annotations) and/or HandlerAdapter
(for method level
annotations) is present in the dispatcher. This is the case by default. However, if you are defining custom
HandlerMappings
or HandlerAdapters
, then you need to make sure that a corresponding custom
DefaultAnnotationHandlerMapping
and/or AnnotationMethodHandlerAdapter
is defined as well -
provided that you intend to use @RequestMapping
.
A web application can define any number of DispatcherServlets. Each servlet will operate in its own
namespace, loading its own application context with mappings, handlers, etc. Only the root application context as
loaded by ContextLoaderListener
, if any, will be shared.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | HANDLER_ADAPTER_BEAN_NAME | Well-known name for the HandlerAdapter object in the bean factory for this namespace. | |||||||||
String | HANDLER_EXCEPTION_RESOLVER_BEAN_NAME | Well-known name for the HandlerExceptionResolver object in the bean factory for this namespace. | |||||||||
String | HANDLER_MAPPING_BEAN_NAME | Well-known name for the HandlerMapping object in the bean factory for this namespace. | |||||||||
String | LOCALE_RESOLVER_BEAN_NAME | Well-known name for the LocaleResolver object in the bean factory for this namespace. | |||||||||
String | MULTIPART_RESOLVER_BEAN_NAME | Well-known name for the MultipartResolver object in the bean factory for this namespace. | |||||||||
String | PAGE_NOT_FOUND_LOG_CATEGORY | Log category to use when no mapped handler is found for a request. | |||||||||
String | REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME | Well-known name for the RequestToViewNameTranslator object in the bean factory for this namespace. | |||||||||
String | THEME_RESOLVER_BEAN_NAME | Well-known name for the ThemeResolver object in the bean factory for this namespace. | |||||||||
String | VIEW_RESOLVER_BEAN_NAME | Well-known name for the ViewResolver object in the bean factory for this namespace. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.springframework.web.servlet.FrameworkServlet
|
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
LOCALE_RESOLVER_ATTRIBUTE | Request attribute to hold the current LocaleResolver, retrievable by views. | ||||||||||
THEME_RESOLVER_ATTRIBUTE | Request attribute to hold the current ThemeResolver, retrievable by views. | ||||||||||
THEME_SOURCE_ATTRIBUTE | Request attribute to hold the current ThemeSource, retrievable by views. | ||||||||||
WEB_APPLICATION_CONTEXT_ATTRIBUTE | Request attribute to hold the current web application context. | ||||||||||
pageNotFoundLogger | Additional logger to use when no mapped handler is found for a request. |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.springframework.web.servlet.FrameworkServlet
| |||||||||||
From class
org.springframework.web.servlet.HttpServletBean
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Obtain this servlet's MultipartResolver, if any.
| |||||||||||
Return this servlet's ThemeSource, if any; else return
null . | |||||||||||
Set whether to perform cleanup of request attributes after an include request, that is,
whether to reset the original state of all request attributes after the DispatcherServlet
has processed within an include request.
| |||||||||||
Set whether to detect all HandlerAdapter beans in this servlet's context.
| |||||||||||
Set whether to detect all HandlerExceptionResolver beans in this servlet's context.
| |||||||||||
Set whether to detect all HandlerMapping beans in this servlet's context.
| |||||||||||
Set whether to detect all ViewResolver beans in this servlet's context.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Build a LocaleContext for the given request, exposing the request's primary locale as current locale.
| |||||||||||
Convert the request into a multipart request, and make multipart resolver available.
| |||||||||||
Clean up any resources used by the given multipart request (if any).
| |||||||||||
Create a default strategy.
| |||||||||||
Process the actual dispatching to the handler.
| |||||||||||
Exposes the DispatcherServlet-specific request attributes and delegates to
doDispatch(HttpServletRequest, HttpServletResponse)
for the actual dispatching. | |||||||||||
Create a List of default strategy objects for the given strategy interface.
| |||||||||||
Return the default strategy object for the given strategy interface.
| |||||||||||
Translate the supplied request into a default view name.
| |||||||||||
This method is deprecated.
as of Spring 3.0.4, in favor of
getHandler(javax.servlet.http.HttpServletRequest) ,
with this method's cache attribute now effectively getting ignored
| |||||||||||
Return the HandlerExecutionChain for this request.
| |||||||||||
Return the HandlerAdapter for this handler object.
| |||||||||||
Initialize the strategy objects that this servlet uses.
| |||||||||||
No handler found -> set appropriate HTTP response status.
| |||||||||||
This implementation calls
initStrategies(ApplicationContext) . | |||||||||||
Determine an error ModelAndView via the registered HandlerExceptionResolvers.
| |||||||||||
Render the given ModelAndView.
| |||||||||||
Resolve the given view name into a View object (to be rendered).
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.springframework.web.servlet.FrameworkServlet
| |||||||||||
From class
org.springframework.web.servlet.HttpServletBean
| |||||||||||
From class
javax.servlet.http.HttpServlet
| |||||||||||
From class
javax.servlet.GenericServlet
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
javax.servlet.Servlet
| |||||||||||
From interface
javax.servlet.ServletConfig
| |||||||||||
From interface
org.springframework.context.EnvironmentAware
|
Well-known name for the HandlerAdapter object in the bean factory for this namespace. Only used when "detectAllHandlerAdapters" is turned off.
Well-known name for the HandlerExceptionResolver object in the bean factory for this namespace. Only used when "detectAllHandlerExceptionResolvers" is turned off.
Well-known name for the HandlerMapping object in the bean factory for this namespace. Only used when "detectAllHandlerMappings" is turned off.
Well-known name for the LocaleResolver object in the bean factory for this namespace.
Well-known name for the MultipartResolver object in the bean factory for this namespace.
Log category to use when no mapped handler is found for a request.
Well-known name for the RequestToViewNameTranslator object in the bean factory for this namespace.
Well-known name for the ThemeResolver object in the bean factory for this namespace.
Well-known name for the ViewResolver object in the bean factory for this namespace. Only used when "detectAllViewResolvers" is turned off.
Request attribute to hold the current LocaleResolver, retrievable by views.
Request attribute to hold the current ThemeResolver, retrievable by views.
Request attribute to hold the current ThemeSource, retrievable by views.
Request attribute to hold the current web application context. Otherwise only the global web app context is obtainable by tags etc.
Additional logger to use when no mapped handler is found for a request.
Obtain this servlet's MultipartResolver, if any.
null
if none
(indicating that no multipart support is available)
Return this servlet's ThemeSource, if any; else return null
.
Default is to return the WebApplicationContext as ThemeSource, provided that it implements the ThemeSource interface.
Set whether to perform cleanup of request attributes after an include request, that is, whether to reset the original state of all request attributes after the DispatcherServlet has processed within an include request. Otherwise, just the DispatcherServlet's own request attributes will be reset, but not model attributes for JSPs or special attributes set by views (for example, JSTL's).
Default is "true", which is strongly recommended. Views should not rely on request attributes having been set by (dynamic) includes. This allows JSP views rendered by an included controller to use any model attributes, even with the same names as in the main JSP, without causing side effects. Only turn this off for special needs, for example to deliberately allow main JSPs to access attributes from JSP views rendered by an included controller.
Set whether to detect all HandlerAdapter beans in this servlet's context. Otherwise, just a single bean with name "handlerAdapter" will be expected.
Default is "true". Turn this off if you want this servlet to use a single HandlerAdapter, despite multiple HandlerAdapter beans being defined in the context.
Set whether to detect all HandlerExceptionResolver beans in this servlet's context. Otherwise, just a single bean with name "handlerExceptionResolver" will be expected.
Default is "true". Turn this off if you want this servlet to use a single HandlerExceptionResolver, despite multiple HandlerExceptionResolver beans being defined in the context.
Set whether to detect all HandlerMapping beans in this servlet's context. Otherwise, just a single bean with name "handlerMapping" will be expected.
Default is "true". Turn this off if you want this servlet to use a single HandlerMapping, despite multiple HandlerMapping beans being defined in the context.
Set whether to detect all ViewResolver beans in this servlet's context. Otherwise, just a single bean with name "viewResolver" will be expected.
Default is "true". Turn this off if you want this servlet to use a single ViewResolver, despite multiple ViewResolver beans being defined in the context.
Build a LocaleContext for the given request, exposing the request's primary locale as current locale.
The default implementation uses the dispatcher's LocaleResolver to obtain the current locale, which might change during a request.
request | current HTTP request |
---|
Convert the request into a multipart request, and make multipart resolver available.
If no multipart resolver is set, simply use the existing request.
request | current HTTP request |
---|
MultipartException |
---|
Clean up any resources used by the given multipart request (if any).
request | current HTTP request |
---|
Create a default strategy.
The default implementation uses createBean(Class, int, boolean)
.
context | the current WebApplicationContext |
---|---|
clazz | the strategy implementation class to instantiate |
Process the actual dispatching to the handler.
The handler will be obtained by applying the servlet's HandlerMappings in order. The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters to find the first that supports the handler class.
All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers themselves to decide which methods are acceptable.
request | current HTTP request |
---|---|
response | current HTTP response |
Exception | in case of any kind of processing failure |
---|
Exposes the DispatcherServlet-specific request attributes and delegates to doDispatch(HttpServletRequest, HttpServletResponse)
for the actual dispatching.
request | current HTTP request |
---|---|
response | current HTTP response |
Exception |
---|
Create a List of default strategy objects for the given strategy interface.
The default implementation uses the "DispatcherServlet.properties" file (in the same package as the DispatcherServlet class) to determine the class names. It instantiates the strategy objects through the context's BeanFactory.
context | the current WebApplicationContext |
---|---|
strategyInterface | the strategy interface |
Return the default strategy object for the given strategy interface.
The default implementation delegates to getDefaultStrategies(ApplicationContext, Class
,
expecting a single object in the list.
context | the current WebApplicationContext |
---|---|
strategyInterface | the strategy interface |
Translate the supplied request into a default view name.
request | current HTTP servlet request |
---|
null
if no default found)Exception | if view name translation failed |
---|
This method is deprecated.
as of Spring 3.0.4, in favor of getHandler(javax.servlet.http.HttpServletRequest)
,
with this method's cache attribute now effectively getting ignored
Return the HandlerExecutionChain for this request. Try all handler mappings in order.
request | current HTTP request |
---|---|
cache | whether to cache the HandlerExecutionChain in a request attribute |
null
if no handler could be foundException |
---|
Return the HandlerExecutionChain for this request.
Tries all handler mappings in order.
request | current HTTP request |
---|
null
if no handler could be found
Exception |
---|
Return the HandlerAdapter for this handler object.
handler | the handler object to find an adapter for |
---|
ServletException | if no HandlerAdapter can be found for the handler. This is a fatal error. |
---|
Initialize the strategy objects that this servlet uses.
May be overridden in subclasses in order to initialize further strategy objects.
No handler found -> set appropriate HTTP response status.
request | current HTTP request |
---|---|
response | current HTTP response |
Exception | if preparing the response failed |
---|
This implementation calls initStrategies(ApplicationContext)
.
context | the current WebApplicationContext |
---|
Determine an error ModelAndView via the registered HandlerExceptionResolvers.
request | current HTTP request |
---|---|
response | current HTTP response |
handler | the executed handler, or null if none chosen at the time of the exception
(for example, if multipart resolution failed) |
ex | the exception that got thrown during handler execution |
Exception | if no error ModelAndView found |
---|
Render the given ModelAndView.
This is the last stage in handling a request. It may involve resolving the view by name.
mv | the ModelAndView to render |
---|---|
request | current HTTP servlet request |
response | current HTTP servlet response |
ServletException | if view is missing or cannot be resolved |
---|---|
Exception | if there's a problem rendering the view |
Resolve the given view name into a View object (to be rendered).
The default implementations asks all ViewResolvers of this dispatcher. Can be overridden for custom resolution strategies, potentially based on specific model attributes or request parameters.
viewName | the name of the view to resolve |
---|---|
model | the model to be passed to the view |
locale | the current locale |
request | current HTTP servlet request |
null
if none foundException | if the view cannot be resolved (typically in case of problems creating an actual View object) |
---|