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 {@link HandlerMapping} implementation - pre-built or providedas part of an application - to control the routing of requests to handler objects. Default is {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping}, as well as a {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}when running on Java 5+. 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 {@link HandlerAdapter}; this allows for using any handler interface. Default adapters are {@link org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter}, {@link org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter} and{@link org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter}, for Spring's {@link org.springframework.web.HttpRequestHandler}, {@link org.springframework.web.servlet.mvc.Controller} and{@link org.springframework.web.servlet.mvc.throwaway.ThrowawayController} interfaces,respectively. When running in a Java 5+ environment, a default {@link org.springframework.web.servlet.mvc.annotation.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).
- The dispatcher's exception resolution strategy can be specified via a {@link 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 {@link ViewResolver}implementation, resolving symbolic view names into View objects. Default is {@link 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 {@link View} or view name is not supplied by the user, then the configured{@link RequestToViewNameTranslator} will translate the current request into aview name. The corresponding bean name is "viewNameTranslator"; the default is {@link org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator}.
- The dispatcher's strategy for resolving multipart requests is determined by a {@link org.springframework.web.multipart.MultipartResolver} implementation.Implementations for Jakarta Commons FileUpload and Jason Hunter's COS are included; the typical choise is {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}. The MultipartResolver bean name is "multipartResolver"; default is none.
- Its locale resolution strategy is determined by a {@link LocaleResolver}. Out-of-the-box implementations work via HTTP accept header, cookie, or session. The LocaleResolver bean name is "localeResolver"; default is {@link org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver}.
- Its theme resolution strategy is determined by a {@link ThemeResolver}. Implementations for a fixed theme and for cookie and session storage are included. The ThemeResolver bean name is "themeResolver"; default is {@link org.springframework.web.servlet.theme.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 {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared.
@author Rod Johnson
@author Juergen Hoeller
@author Rob Harrop
@see org.springframework.web.HttpRequestHandler
@see org.springframework.web.servlet.mvc.Controller
@see org.springframework.web.context.ContextLoaderListener