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
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping and
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping .
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 org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter ,
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter , for Spring's
org.springframework.web.HttpRequestHandler and
org.springframework.web.servlet.mvc.Controller interfaces, respectively. A default
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
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
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver ,
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver , and
org.springframework.web.servlet.mvc.support.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
org.springframework.web.servlet.view.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
org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator .
org.springframework.web.multipart.MultipartResolver implementation.
An implementation for standard Servlet multipart processing is included.
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
org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver .
ThemeResolver.
Implementations for a fixed theme and for cookie and session storage are included.
The ThemeResolver bean name is "themeResolver"; default is
org.springframework.web.servlet.theme.FixedThemeResolver .
Theme support is deprecated as of 6.0 with no direct replacement.
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
RequestMappingHandlerMapping and/or RequestMappingHandlerAdapter
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
org., if any, will be shared.
DispatcherServlet may be injected with a web application context,
rather than creating its own internally. This is useful in Servlet 3.0+
environments, which support programmatic registration of servlet instances.
See the DispatcherServlet(WebApplicationContext) javadoc for details.
org.springframework.web.HttpRequestHandler , org.springframework.web.servlet.mvc.Controller , org.springframework.web.context.ContextLoaderListener
| Modifier and Type | Field and Description |
|---|---|
| private boolean | cleanupAfterInclude
Perform cleanup of request attributes after include request?. |
| private static final String | DEFAULT_STRATEGIES_PATH
Name of the class path resource (relative to the DispatcherServlet class) that defines DispatcherServlet's default strategy names. |
| private static final String | DEFAULT_STRATEGIES_PREFIX
Common prefix that DispatcherServlet's default strategy attributes start with. |
| private static Properties | defaultStrategies
Store default strategy implementations. |
| private boolean | detectAllHandlerAdapters
Detect all HandlerAdapters or just expect "handlerAdapter" bean?. |
| private boolean | detectAllHandlerExceptionResolvers
Detect all HandlerExceptionResolvers or just expect "handlerExceptionResolver" bean?. |
| private boolean | detectAllHandlerMappings
Detect all HandlerMappings or just expect "handlerMapping" bean?. |
| private boolean | detectAllViewResolvers
Detect all ViewResolvers or just expect "viewResolver" bean?. |
| public static final String | EXCEPTION_ATTRIBUTE
Name of request attribute that exposes an Exception resolved with a
|
| public static final String | FLASH_MAP_MANAGER_ATTRIBUTE
Name of request attribute that holds the |
| public static final String | FLASH_MAP_MANAGER_BEAN_NAME
Well-known name for the FlashMapManager object in the bean factory for this namespace. |
| private FlashMapManager | flashMapManager
FlashMapManager used by this servlet. |
| public static final String | HANDLER_ADAPTER_BEAN_NAME
Well-known name for the HandlerAdapter object in the bean factory for this namespace. |
| public static final String | HANDLER_EXCEPTION_RESOLVER_BEAN_NAME
Well-known name for the HandlerExceptionResolver object in the bean factory for this namespace. |
| public static final String | HANDLER_MAPPING_BEAN_NAME
Well-known name for the HandlerMapping object in the bean factory for this namespace. |
| private List | handlerAdapters
List of HandlerAdapters used by this servlet. |
| private List | handlerExceptionResolvers
List of HandlerExceptionResolvers used by this servlet. |
| private List | handlerMappings
List of HandlerMappings used by this servlet. |
| public static final String | INPUT_FLASH_MAP_ATTRIBUTE
Name of request attribute that holds a read-only |
| public static final String | LOCALE_RESOLVER_ATTRIBUTE
Request attribute to hold the current LocaleResolver, retrievable by views. |
| public static final String | LOCALE_RESOLVER_BEAN_NAME
Well-known name for the LocaleResolver object in the bean factory for this namespace. |
| private LocaleResolver | localeResolver
LocaleResolver used by this servlet. |
| public static final String | MULTIPART_RESOLVER_BEAN_NAME
Well-known name for the MultipartResolver object in the bean factory for this namespace. |
| private MultipartResolver | multipartResolver
MultipartResolver used by this servlet. |
| public static final String | OUTPUT_FLASH_MAP_ATTRIBUTE
Name of request attribute that holds the "output" |
| public static final String | PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request. |
| protected static final Log | pageNotFoundLogger
Additional logger to use when no mapped handler is found for a request. |
| private boolean | |
| public static final String | REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME
Well-known name for the RequestToViewNameTranslator object in the bean factory for this namespace. |
| public static final String | THEME_RESOLVER_ATTRIBUTE
Deprecated
as of 6.0, with no direct replacement
Request attribute to hold the current ThemeResolver, retrievable by views.
|
| public static final String | THEME_RESOLVER_BEAN_NAME
Deprecated
as of 6.0, with no direct replacement
Well-known name for the ThemeResolver object in the bean factory for this namespace.
|
| public static final String | THEME_SOURCE_ATTRIBUTE
Deprecated
as of 6.0, with no direct replacement
Request attribute to hold the current ThemeSource, retrievable by views.
|
| private ThemeResolver | themeResolver
Deprecated
ThemeResolver used by this servlet.
|
| private boolean | throwExceptionIfNoHandlerFound
Throw a NoHandlerFoundException if no Handler was found to process this request? |
| public static final String | VIEW_RESOLVER_BEAN_NAME
Well-known name for the ViewResolver object in the bean factory for this namespace. |
| private RequestToViewNameTranslator | viewNameTranslator
RequestToViewNameTranslator used by this servlet. |
| private List | viewResolvers
List of ViewResolvers used by this servlet. |
| public static final String | WEB_APPLICATION_CONTEXT_ATTRIBUTE
Request attribute to hold the current web application context. |
| Access | Constructor and Description |
|---|---|
| public | DispatcherServlet()
Create a new |
| public | DispatcherServlet(WebApplicationContext
the context to use webApplicationContext)Create a new |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| protected LocaleContext | Returns: the corresponding LocaleContextcurrent HTTP request request)Overrides org. Build a LocaleContext for the given request, exposing the request's primary locale as current locale. |
| protected HttpServletRequest | Returns: the processed request (multipart wrapper if necessary)current HTTP request request)Convert the request into a multipart request, and make multipart resolver available. |
| protected void | cleanupMultipart(HttpServletRequest
current HTTP request request)Clean up any resources used by the given multipart request (if any). |
| protected Object | Returns: the fully configured strategy instancethe current WebApplicationContext context, Class<?> the strategy implementation class to instantiate clazz)Create a default strategy. |
| protected void | doDispatch(HttpServletRequest
current HTTP request request, HttpServletResponse current HTTP response response)Process the actual dispatching to the handler. |
| protected void | doService(HttpServletRequest
current HTTP request request, HttpServletResponse current HTTP response response)Implements abstract org. Exposes the DispatcherServlet-specific request attributes and delegates to |
| protected <T> List | Returns: the List of corresponding strategy objectsthe current WebApplicationContext context, Class<T> the strategy interface strategyInterface)Create a List of default strategy objects for the given strategy interface. |
| protected <T> T | Returns: the corresponding strategy objectthe current WebApplicationContext context, Class<T> the strategy interface strategyInterface)Return the default strategy object for the given strategy interface. |
| protected String | Returns: the view name (ornull if no default found)current HTTP servlet request request)Translate the supplied request into a default view name. |
| protected HandlerExecutionChain | Returns: the HandlerExecutionChain, ornull if no handler could be foundcurrent HTTP request request)Return the HandlerExecutionChain for this request. |
| protected HandlerAdapter | getHandlerAdapter(Object
the handler object to find an adapter for handler)Return the HandlerAdapter for this handler object. |
| public final List | Returns: an immutable list with the configured mappings, ornull
if not initialized yetReturn the configured |
| public final MultipartResolver | Returns: the MultipartResolver used by this servlet, ornull if none
(indicating that no multipart support is available)Obtain this servlet's MultipartResolver, if any. |
| private static String | |
| public final ThemeSource | Returns: the ThemeSource, if any
Deprecated
Return this servlet's ThemeSource, if any; else return |
| private static boolean | hasMultipartException(HttpServletRequest request)
Check "jakarta.servlet.error.exception" attribute for a multipart exception. |
| private void | initFlashMapManager(ApplicationContext context)
Initialize the |
| private void | |
| private void | initHandlerExceptionResolvers(ApplicationContext context)
Initialize the HandlerExceptionResolver used by this class. |
| private void | |
| private void | |
| private void | initMultipartResolver(ApplicationContext context)
Initialize the MultipartResolver used by this class. |
| private void | initRequestToViewNameTranslator(ApplicationContext context)
Initialize the RequestToViewNameTranslator used by this servlet instance. |
| protected void | |
| private void | initThemeResolver(ApplicationContext context)
Deprecated
Initialize the ThemeResolver used by this class. |
| private void | |
| private void | |
| protected void | noHandlerFound(HttpServletRequest
current HTTP request request, HttpServletResponse current HTTP response response)No handler found → set appropriate HTTP response status. |
| protected void | onRefresh(ApplicationContext
the current WebApplicationContext context)Overrides org. This implementation calls |
| private void | processDispatchResult(HttpServletRequest request, HttpServletResponse response, HandlerExecutionChain mappedHandler, ModelAndView mv, Exception exception)
Handle the result of handler selection and handler invocation, which is either a ModelAndView or an Exception to be resolved to a ModelAndView. |
| protected ModelAndView | Returns: a corresponding ModelAndView to forward tocurrent HTTP request request, HttpServletResponse current HTTP response response, Object the executed handler, or handler, Exception null if none chosen at the time of the exception
(for example, if multipart resolution failed)the exception that got thrown during handler execution ex)Determine an error ModelAndView via the registered HandlerExceptionResolvers. |
| protected void | render(ModelAndView
the ModelAndView to render mv, HttpServletRequest current HTTP servlet request request, HttpServletResponse current HTTP servlet response response)Render the given ModelAndView. |
| protected View | Returns: the View object, ornull if none foundthe name of the view to resolve viewName, Map<String, Object> the model to be passed to the view model, Locale the current locale locale, HttpServletRequest current HTTP servlet request request)Resolve the given view name into a View object (to be rendered). |
| private void | restoreAttributesAfterInclude(HttpServletRequest
current HTTP request request, Map<?, ?> the snapshot of the request attributes before the include attributesSnapshot)Restore the request attributes after an include. |
| public void | setCleanupAfterInclude(boolean cleanupAfterInclude)
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. |
| public void | setDetectAllHandlerAdapters(boolean detectAllHandlerAdapters)
Set whether to detect all HandlerAdapter beans in this servlet's context. |
| public void | setDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers)
Set whether to detect all HandlerExceptionResolver beans in this servlet's context. |
| public void | setDetectAllHandlerMappings(boolean detectAllHandlerMappings)
Set whether to detect all HandlerMapping beans in this servlet's context. |
| public void | setDetectAllViewResolvers(boolean detectAllViewResolvers)
Set whether to detect all ViewResolver beans in this servlet's context. |
| public void | setThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound)
Deprecated
for removal since 6.1. as of 6.1 this property is set to
true by default, and
should not need to be customized; in effect, DispatcherServlet
should always raise NoHandlerFoundException and allow it to be
handled through a HandlerExceptionResolver.
Set whether to throw a NoHandlerFoundException when no Handler was found for this request. |
| private static void | triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, HandlerExecutionChain mappedHandler, Exception ex)
|