Save This Page
Home » spring-framework-2.5.6-with-dependencies » org.springframework » web » servlet » [javadoc | source]
org.springframework.web.servlet
public class: DispatcherServlet [javadoc | source]
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

All Implemented Interfaces:
    ApplicationListener, Serializable, Servlet, ServletConfig

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:

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 org.springframework.web.context.ContextLoaderListener , if any, will be shared.

Field Summary
public static final  String MULTIPART_RESOLVER_BEAN_NAME    Well-known name for the MultipartResolver object in the bean factory for this namespace. 
public static final  String LOCALE_RESOLVER_BEAN_NAME    Well-known name for the LocaleResolver object in the bean factory for this namespace. 
public static final  String THEME_RESOLVER_BEAN_NAME    Well-known name for the ThemeResolver 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. Only used when "detectAllHandlerMappings" is turned off. 
public static final  String HANDLER_ADAPTER_BEAN_NAME    Well-known name for the HandlerAdapter object in the bean factory for this namespace. Only used when "detectAllHandlerAdapters" is turned off. 
public static final  String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME    Well-known name for the HandlerExceptionResolver object in the bean factory for this namespace. Only used when "detectAllHandlerExceptionResolvers" is turned off. 
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 VIEW_RESOLVER_BEAN_NAME    Well-known name for the ViewResolver object in the bean factory for this namespace. Only used when "detectAllViewResolvers" is turned off. 
public static final  String HANDLER_EXECUTION_CHAIN_ATTRIBUTE    Request attribute to hold the currently chosen HandlerExecutionChain. Only used for internal optimizations. 
public static final  String WEB_APPLICATION_CONTEXT_ATTRIBUTE    Request attribute to hold the current web application context. Otherwise only the global web app context is obtainable by tags etc.
    Also see:
    org.springframework.web.servlet.support.RequestContextUtils#getWebApplicationContext
 
public static final  String LOCALE_RESOLVER_ATTRIBUTE    Request attribute to hold the current LocaleResolver, retrievable by views.
    Also see:
    org.springframework.web.servlet.support.RequestContextUtils#getLocaleResolver
 
public static final  String THEME_RESOLVER_ATTRIBUTE    Request attribute to hold the current ThemeResolver, retrievable by views.
    Also see:
    org.springframework.web.servlet.support.RequestContextUtils#getThemeResolver
 
public static final  String THEME_SOURCE_ATTRIBUTE    Request attribute to hold the current ThemeSource, retrievable by views.
    Also see:
    org.springframework.web.servlet.support.RequestContextUtils#getThemeSource
 
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. 
Fields inherited from org.springframework.web.servlet.FrameworkServlet:
DEFAULT_NAMESPACE_SUFFIX,  DEFAULT_CONTEXT_CLASS,  SERVLET_CONTEXT_PREFIX
Fields inherited from org.springframework.web.servlet.HttpServletBean:
logger
Method from org.springframework.web.servlet.DispatcherServlet Summary:
buildLocaleContext,   checkMultipart,   cleanupMultipart,   createDefaultStrategy,   doDispatch,   doService,   getDefaultStrategies,   getDefaultStrategy,   getDefaultViewName,   getHandler,   getHandlerAdapter,   getLastModified,   getMultipartResolver,   getThemeSource,   initStrategies,   noHandlerFound,   onRefresh,   processHandlerException,   render,   resolveViewName,   setCleanupAfterInclude,   setDetectAllHandlerAdapters,   setDetectAllHandlerExceptionResolvers,   setDetectAllHandlerMappings,   setDetectAllViewResolvers,   setThreadContextInheritable
Methods from org.springframework.web.servlet.FrameworkServlet:
createWebApplicationContext,   destroy,   doDelete,   doGet,   doOptions,   doPost,   doPut,   doService,   doTrace,   findWebApplicationContext,   getContextAttribute,   getContextClass,   getContextConfigLocation,   getNamespace,   getServletContextAttributeName,   getUsernameForRequest,   getWebApplicationContext,   initFrameworkServlet,   initServletBean,   initWebApplicationContext,   onApplicationEvent,   onRefresh,   postProcessWebApplicationContext,   processRequest,   refresh,   setContextAttribute,   setContextClass,   setContextConfigLocation,   setDispatchOptionsRequest,   setDispatchTraceRequest,   setNamespace,   setPublishContext,   setPublishEvents
Methods from org.springframework.web.servlet.HttpServletBean:
addRequiredProperty,   getServletContext,   getServletName,   init,   initBeanWrapper,   initServletBean
Methods from javax.servlet.http.HttpServlet:
service
Methods from javax.servlet.GenericServlet:
destroy,   getInitParameter,   getInitParameterNames,   getServletConfig,   getServletContext,   getServletInfo,   getServletName,   init,   init,   log,   log,   service
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.web.servlet.DispatcherServlet Detail:
 protected LocaleContext buildLocaleContext(HttpServletRequest request) 
    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.

 protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException 
    Convert the request into a multipart request, and make multipart resolver available. If no multipart resolver is set, simply use the existing request.
 protected  void cleanupMultipart(HttpServletRequest request) 
    Clean up any resources used by the given multipart request (if any).
 protected Object createDefaultStrategy(ApplicationContext context,
    Class clazz) throws BeansException 
 protected  void doDispatch(HttpServletRequest request,
    HttpServletResponse response) throws Exception 
    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.

 protected  void doService(HttpServletRequest request,
    HttpServletResponse response) throws Exception 
    Exposes the DispatcherServlet-specific request attributes and delegates to #doDispatch for the actual dispatching.
 protected List getDefaultStrategies(ApplicationContext context,
    Class strategyInterface) throws BeansException 
    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.

 protected Object getDefaultStrategy(ApplicationContext context,
    Class strategyInterface) throws BeansException 
    Return the default strategy object for the given strategy interface.

    The default implementation delegates to #getDefaultStrategies , expecting a single object in the list.

 protected String getDefaultViewName(HttpServletRequest request) throws Exception 
    Translate the supplied request into a default view name.
 protected HandlerExecutionChain getHandler(HttpServletRequest request,
    boolean cache) throws Exception 
    Return the HandlerExecutionChain for this request. Try all handler mappings in order.
 protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException 
    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 final MultipartResolver getMultipartResolver() 
    Obtain this servlet's MultipartResolver, if any.
 public final ThemeSource getThemeSource() 
    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.

 protected  void initStrategies(ApplicationContext context) 
    Initialize the strategy objects that this servlet uses.

    May be overridden in subclasses in order to initialize further strategy objects.

 protected  void noHandlerFound(HttpServletRequest request,
    HttpServletResponse response) throws Exception 
    No handler found -> set appropriate HTTP response status.
 protected  void onRefresh(ApplicationContext context) throws BeansException 
 protected ModelAndView processHandlerException(HttpServletRequest request,
    HttpServletResponse response,
    Object handler,
    Exception ex) throws Exception 
    Determine an error ModelAndView via the registered HandlerExceptionResolvers.
 protected  void render(ModelAndView mv,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception 
    Render the given ModelAndView. This is the last stage in handling a request. It may involve resolving the view by name.
 protected View resolveViewName(String viewName,
    Map model,
    Locale locale,
    HttpServletRequest request) throws Exception 
    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.

 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. 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.

 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". Turn this off if you want this servlet to use a single HandlerAdapter, despite multiple HandlerAdapter beans being defined in the context.

 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". Turn this off if you want this servlet to use a single HandlerExceptionResolver, despite multiple HandlerExceptionResolver beans being defined in the context.

 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". Turn this off if you want this servlet to use a single HandlerMapping, despite multiple HandlerMapping beans being defined in the context.

 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". Turn this off if you want this servlet to use a single ViewResolver, despite multiple ViewResolver beans being defined in the context.

 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. a JDK java.util.concurrent.ThreadPoolExecutor ), since this will expose the inherited context to such a pooled thread.