| org.springframework.web.servlet.HttpServletBean org.springframework.web.servlet.FrameworkServlet org.springframework.web.servlet.DispatcherServlet
DispatcherServlet | public class DispatcherServlet extends FrameworkServlet (Code) | | 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:
- It is based around a JavaBeans configuration mechanism.
- It can use any
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 .
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).
- It can use any
HandlerAdapter ; this allows to use any handler interface.
Default adapters are
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter ,
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter and
org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter ,
for Spring's
org.springframework.web.HttpRequestHandler ,
org.springframework.web.servlet.mvc.Controller and
org.springframework.web.servlet.mvc.throwaway.ThrowawayController interfaces,
respectively. 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).
- The dispatcher's exception resolution strategy can be specified via a
HandlerExceptionResolver , for example mapping certain exceptions to
error pages. Default is none. Additional HandlerExceptionResolvers can be added
through the application context. HandlerExceptionResolver can be given any
bean name (they are tested by type).
- Its view resolution strategy can be specified via a
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).
- If a
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 .
- The dispatcher's strategy for resolving multipart requests is determined by
a
org.springframework.web.multipart.MultipartResolver implementation.
Implementations for Jakarta Commons FileUpload and Jason Hunter's COS are
included; the typical choise is
org.springframework.web.multipart.commons.CommonsMultipartResolver .
The MultipartResolver bean name is "multipartResolver"; default is none.
- Its locale resolution strategy is determined by a
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 .
- Its theme resolution strategy is determined by a
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 .
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.springframework.web.context.ContextLoaderListener ,
if any, will be shared.
author: Rod Johnson author: Juergen Hoeller author: Rob Harrop See Also: org.springframework.web.HttpRequestHandler See Also: org.springframework.web.servlet.mvc.Controller See Also: org.springframework.web.context.ContextLoaderListener |
Method Summary | |
protected LocaleContext | buildLocaleContext(HttpServletRequest request) Build a LocaleContext for the given request, exposing the request's
primary locale as current locale. | protected HttpServletRequest | checkMultipart(HttpServletRequest request) Convert the request into a multipart request, and make multipart resolver available. | protected void | cleanupMultipart(HttpServletRequest request) Clean up any resources used by the given multipart request (if any). | protected Object | createDefaultStrategy(ApplicationContext context, Class clazz) Create a default strategy. | protected void | doDispatch(HttpServletRequest request, HttpServletResponse response) 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. | protected void | doService(HttpServletRequest request, HttpServletResponse response) Exposes the DispatcherServlet-specific request attributes and
delegates to
DispatcherServlet.doDispatch for the actual dispatching. | protected List | getDefaultStrategies(ApplicationContext context, Class strategyInterface) Create a List of default strategy objects for the given strategy interface. | protected Object | getDefaultStrategy(ApplicationContext context, Class strategyInterface) Return the default strategy object for the given strategy interface. | protected String | getDefaultViewName(HttpServletRequest request) Translate the supplied request into a default view name. | protected HandlerExecutionChain | getHandler(HttpServletRequest request, boolean cache) Return the HandlerExecutionChain for this request. | protected HandlerAdapter | getHandlerAdapter(Object handler) Return the HandlerAdapter for this handler object. | protected long | getLastModified(HttpServletRequest request) Override HttpServlet's getLastModified method to evaluate
the Last-Modified value of the mapped handler. | public MultipartResolver | getMultipartResolver() Obtain this servlet's MultipartResolver, if any. | public ThemeSource | getThemeSource() Return this servlet's ThemeSource, if any; else return null . | protected void | initStrategies(ApplicationContext context) Initialize the strategy objects that this servlet uses. | protected void | noHandlerFound(HttpServletRequest request, HttpServletResponse response) No handler found -> set appropriate HTTP response status. | protected void | onRefresh(ApplicationContext context) This implementation calls
DispatcherServlet.initStrategies . | protected ModelAndView | processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) Determine an error ModelAndView via the registered HandlerExceptionResolvers. | protected void | render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) Render the given ModelAndView. | protected View | resolveViewName(String viewName, Map model, Locale locale, HttpServletRequest request) Resolve the given view name into a View object (to be rendered). | 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.
Else, just a single bean with name "handlerAdapter" will be expected.
Default is "true". | public void | setDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers) Set whether to detect all HandlerExceptionResolver beans in this servlet's context.
Else, just a single bean with name "handlerExceptionResolver" will be expected.
Default is "true". | public void | setDetectAllHandlerMappings(boolean detectAllHandlerMappings) Set whether to detect all HandlerMapping beans in this servlet's context.
Else, just a single bean with name "handlerMapping" will be expected.
Default is "true". | public void | setDetectAllViewResolvers(boolean detectAllViewResolvers) Set whether to detect all ViewResolver beans in this servlet's context.
Else, just a single bean with name "viewResolver" will be expected.
Default is "true". | public void | setThreadContextInheritable(boolean threadContextInheritable) Set whether to expose the LocaleContext and RequestAttributes as inheritable
for child threads (using an
java.lang.InheritableThreadLocal ).
Default is "false", to avoid side effects on spawned background threads.
Switch this to "true" to enable inheritance for custom child threads which
are spawned during request processing and only used for this request
(that is, ending after their initial task, without reuse of the thread).
WARNING: Do not use inheritance for child threads if you are
accessing a thread pool which is configured to potentially add new threads
on demand (e.g. |
HANDLER_EXECUTION_CHAIN_ATTRIBUTE | final public static String HANDLER_EXECUTION_CHAIN_ATTRIBUTE(Code) | | Request attribute to hold the currently chosen HandlerExecutionChain.
Only used for internal optimizations.
|
LOCALE_RESOLVER_BEAN_NAME | final public static String LOCALE_RESOLVER_BEAN_NAME(Code) | | Well-known name for the LocaleResolver object in the bean factory for this namespace.
|
MULTIPART_RESOLVER_BEAN_NAME | final public static String MULTIPART_RESOLVER_BEAN_NAME(Code) | | Well-known name for the MultipartResolver object in the bean factory for this namespace.
|
PAGE_NOT_FOUND_LOG_CATEGORY | final public static String PAGE_NOT_FOUND_LOG_CATEGORY(Code) | | Log category to use when no mapped handler is found for a request.
|
REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME | final public static String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME(Code) | | Well-known name for the RequestToViewNameTranslator object in the bean factory for
this namespace.
|
THEME_RESOLVER_BEAN_NAME | final public static String THEME_RESOLVER_BEAN_NAME(Code) | | Well-known name for the ThemeResolver object in the bean factory for this namespace.
|
pageNotFoundLogger | final protected static Log pageNotFoundLogger(Code) | | Additional logger to use when no mapped handler is found for a request.
|
buildLocaleContext | protected LocaleContext buildLocaleContext(HttpServletRequest request)(Code) | | 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.
Parameters: request - current HTTP request the corresponding LocaleContext |
doDispatch | protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception(Code) | | 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.
Parameters: request - current HTTP request Parameters: response - current HTTP response throws: Exception - in case of any kind of processing failure |
getDefaultStrategies | protected List getDefaultStrategies(ApplicationContext context, Class strategyInterface) throws BeansException(Code) | | 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.
Parameters: context - the current WebApplicationContext Parameters: strategyInterface - the strategy interface the List of corresponding strategy objects throws: BeansException - if initialization failed |
getDefaultViewName | protected String getDefaultViewName(HttpServletRequest request) throws Exception(Code) | | Translate the supplied request into a default view name.
Parameters: request - current HTTP servlet request the view name throws: Exception - if view name translation failed |
getHandler | protected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache) throws Exception(Code) | | Return the HandlerExecutionChain for this request.
Try all handler mappings in order.
Parameters: request - current HTTP request Parameters: cache - whether to cache the HandlerExecutionChain in a request attribute the HandlerExceutionChain, or null if no handler could be found |
getHandlerAdapter | protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException(Code) | | Return the HandlerAdapter for this handler object.
Parameters: handler - the handler object to find an adapter for throws: ServletException - if no HandlerAdapter can be found for the handler.This is a fatal error. |
getLastModified | protected long getLastModified(HttpServletRequest request)(Code) | | Override HttpServlet's getLastModified method to evaluate
the Last-Modified value of the mapped handler.
|
getMultipartResolver | public MultipartResolver getMultipartResolver()(Code) | | Obtain this servlet's MultipartResolver, if any.
the MultipartResolver used by this servlet, or null if none (indicating that no multipart support is available) |
getThemeSource | public ThemeSource getThemeSource()(Code) | | 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.
the ThemeSource, if any See Also: DispatcherServlet.getWebApplicationContext() |
initStrategies | protected void initStrategies(ApplicationContext context)(Code) | | Initialize the strategy objects that this servlet uses.
May be overridden in subclasses in order to initialize
further strategy objects.
|
noHandlerFound | protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws IOException(Code) | | No handler found -> set appropriate HTTP response status.
Parameters: request - current HTTP request Parameters: response - current HTTP response throws: IOException - if thrown by the HttpServletResponse |
processHandlerException | protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception(Code) | | Determine an error ModelAndView via the registered HandlerExceptionResolvers.
Parameters: request - current HTTP request Parameters: response - current HTTP response Parameters: handler - the executed handler, or null if none chosen at the time ofthe exception (for example, if multipart resolution failed) Parameters: ex - the exception that got thrown during handler execution a corresponding ModelAndView to forward to throws: Exception - if no error ModelAndView found |
render | protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception(Code) | | Render the given ModelAndView. This is the last stage in handling a request.
It may involve resolving the view by name.
Parameters: mv - the ModelAndView to render Parameters: request - current HTTP servlet request Parameters: response - current HTTP servlet response throws: Exception - if there's a problem rendering the view |
resolveViewName | protected View resolveViewName(String viewName, Map model, Locale locale, HttpServletRequest request) throws Exception(Code) | | Resolve the given view name into a View object (to be rendered).
Default implementations asks all ViewResolvers of this dispatcher.
Can be overridden for custom resolution strategies, potentially based
on specific model attributes or request parameters.
Parameters: viewName - the name of the view to resolve Parameters: model - the model to be passed to the view Parameters: locale - the current locale Parameters: request - current HTTP servlet request the View object, or null if none found throws: Exception - if the view cannot be resolved(typically in case of problems creating an actual View object) See Also: ViewResolver.resolveViewName |
setCleanupAfterInclude | public void setCleanupAfterInclude(boolean cleanupAfterInclude)(Code) | | 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. Else, 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.
|
setDetectAllHandlerAdapters | public void setDetectAllHandlerAdapters(boolean detectAllHandlerAdapters)(Code) | | Set whether to detect all HandlerAdapter beans in this servlet's context.
Else, 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.
|
setDetectAllHandlerExceptionResolvers | public void setDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers)(Code) | | Set whether to detect all HandlerExceptionResolver beans in this servlet's context.
Else, 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.
|
setDetectAllHandlerMappings | public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings)(Code) | | Set whether to detect all HandlerMapping beans in this servlet's context.
Else, 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.
|
setDetectAllViewResolvers | public void setDetectAllViewResolvers(boolean detectAllViewResolvers)(Code) | | Set whether to detect all ViewResolver beans in this servlet's context.
Else, 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.
|
setThreadContextInheritable | public void setThreadContextInheritable(boolean threadContextInheritable)(Code) | | Set whether to expose the LocaleContext and RequestAttributes as inheritable
for child threads (using an
java.lang.InheritableThreadLocal ).
Default is "false", to avoid side effects on spawned background threads.
Switch this to "true" to enable inheritance for custom child threads which
are spawned during request processing and only used for this request
(that is, ending after their initial task, without reuse of the thread).
WARNING: Do not use inheritance for child threads if you are
accessing a thread pool which is configured to potentially add new threads
on demand (e.g. a JDK
java.util.concurrent.ThreadPoolExecutor ),
since this will expose the inherited context to such a pooled thread.
|
Methods inherited from org.springframework.web.servlet.FrameworkServlet | protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException(Code)(Java Doc) public void destroy()(Code)(Java Doc) final protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException(Code)(Java Doc) final protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException(Code)(Java Doc) final protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException(Code)(Java Doc) final protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException(Code)(Java Doc) abstract protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception(Code)(Java Doc) public Class getContextClass()(Code)(Java Doc) public String getContextConfigLocation()(Code)(Java Doc) public String getNamespace()(Code)(Java Doc) public String getServletContextAttributeName()(Code)(Java Doc) protected String getUsernameForRequest(HttpServletRequest request)(Code)(Java Doc) final public WebApplicationContext getWebApplicationContext()(Code)(Java Doc) protected void initFrameworkServlet() throws ServletException, BeansException(Code)(Java Doc) final protected void initServletBean() throws ServletException, BeansException(Code)(Java Doc) protected WebApplicationContext initWebApplicationContext() throws BeansException(Code)(Java Doc) public boolean isPublishContext()(Code)(Java Doc) public boolean isPublishEvents()(Code)(Java Doc) public void onApplicationEvent(ApplicationEvent event)(Code)(Java Doc) protected void onRefresh(ApplicationContext context) throws BeansException(Code)(Java Doc) protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac)(Code)(Java Doc) final protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException(Code)(Java Doc) public void refresh() throws BeansException(Code)(Java Doc) public void setContextClass(Class contextClass)(Code)(Java Doc) public void setContextConfigLocation(String contextConfigLocation)(Code)(Java Doc) public void setNamespace(String namespace)(Code)(Java Doc) public void setPublishContext(boolean publishContext)(Code)(Java Doc) public void setPublishEvents(boolean publishEvents)(Code)(Java Doc)
|
Fields inherited from org.springframework.web.servlet.HttpServletBean | final protected Log logger(Code)(Java Doc)
|
|
|