类 DispatcherServlet
- 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
- 所有已实现的接口:
Serializable,Servlet,ServletConfig,Aware,ApplicationContextAware,EnvironmentAware,EnvironmentCapable
public class DispatcherServlet extends FrameworkServlet
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
HandlerMappingimplementation - pre-built or provided as part of an application - to control the routing of requests to handler objects. Default isBeanNameUrlHandlerMappingandDefaultAnnotationHandlerMapping. 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 for using any handler interface. Default adapters areHttpRequestHandlerAdapter,SimpleControllerHandlerAdapter, for Spring'sHttpRequestHandlerandControllerinterfaces, respectively. A defaultAnnotationMethodHandlerAdapterwill 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
HandlerExceptionResolver, for example mapping certain exceptions to error pages. Default areAnnotationMethodHandlerExceptionResolver,ResponseStatusExceptionResolver, andDefaultHandlerExceptionResolver. These HandlerExceptionResolvers can be overridden 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
ViewResolverimplementation, resolving symbolic view names into View objects. Default isInternalResourceViewResolver. 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
Viewor view name is not supplied by the user, then the configuredRequestToViewNameTranslatorwill translate the current request into a view name. The corresponding bean name is "viewNameTranslator"; the default isDefaultRequestToViewNameTranslator. - The dispatcher's strategy for resolving multipart requests is determined by a
MultipartResolverimplementation. Implementations for Apache Commons FileUpload and Servlet 3 are included; the typical choice isCommonsMultipartResolver. 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 isAcceptHeaderLocaleResolver. - 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 isFixedThemeResolver.
NOTE: The
@RequestMappingannotation will only be processed if a correspondingHandlerMapping(for type-level annotations) and/orHandlerAdapter(for method-level annotations) is present in the dispatcher. This is the case by default. However, if you are defining customHandlerMappingsorHandlerAdapters, then you need to make sure that a corresponding customDefaultAnnotationHandlerMappingand/orAnnotationMethodHandlerAdapteris 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.As of Spring 3.1,
DispatcherServletmay now 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 theDispatcherServlet(WebApplicationContext)javadoc for details.- 作者:
- Rod Johnson, Juergen Hoeller, Rob Harrop, Chris Beams, Rossen Stoyanchev
- 另请参阅:
HttpRequestHandler,Controller,ContextLoaderListener, 序列化表格
字段概要
字段 修饰符和类型 字段 说明 static StringEXCEPTION_ATTRIBUTEName of request attribute that exposes an Exception resolved with anHandlerExceptionResolverbut where no view was rendered (e.g. setting the status code).static StringFLASH_MAP_MANAGER_ATTRIBUTEName of request attribute that holds theFlashMapManager.static StringFLASH_MAP_MANAGER_BEAN_NAMEWell-known name for the FlashMapManager object in the bean factory for this namespace.static StringHANDLER_ADAPTER_BEAN_NAMEWell-known name for the HandlerAdapter object in the bean factory for this namespace.static StringHANDLER_EXCEPTION_RESOLVER_BEAN_NAMEWell-known name for the HandlerExceptionResolver object in the bean factory for this namespace.static StringHANDLER_MAPPING_BEAN_NAMEWell-known name for the HandlerMapping object in the bean factory for this namespace.static StringINPUT_FLASH_MAP_ATTRIBUTEName of request attribute that holds a read-onlyMap<String,?>with "input" flash attributes saved by a previous request, if any.static StringLOCALE_RESOLVER_ATTRIBUTERequest attribute to hold the current LocaleResolver, retrievable by views.static StringLOCALE_RESOLVER_BEAN_NAMEWell-known name for the LocaleResolver object in the bean factory for this namespace.static StringMULTIPART_RESOLVER_BEAN_NAMEWell-known name for the MultipartResolver object in the bean factory for this namespace.static StringOUTPUT_FLASH_MAP_ATTRIBUTEName of request attribute that holds the "output"FlashMapwith attributes to save for a subsequent request.static StringPAGE_NOT_FOUND_LOG_CATEGORYLog category to use when no mapped handler is found for a request.protected static LogpageNotFoundLoggerAdditional logger to use when no mapped handler is found for a request.static StringREQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAMEWell-known name for the RequestToViewNameTranslator object in the bean factory for this namespace.static StringTHEME_RESOLVER_ATTRIBUTERequest attribute to hold the current ThemeResolver, retrievable by views.static StringTHEME_RESOLVER_BEAN_NAMEWell-known name for the ThemeResolver object in the bean factory for this namespace.static StringTHEME_SOURCE_ATTRIBUTERequest attribute to hold the current ThemeSource, retrievable by views.static StringVIEW_RESOLVER_BEAN_NAMEWell-known name for the ViewResolver object in the bean factory for this namespace.static StringWEB_APPLICATION_CONTEXT_ATTRIBUTERequest attribute to hold the current web application context.从类继承的字段 org.springframework.web.servlet.FrameworkServlet
DEFAULT_CONTEXT_CLASS, DEFAULT_NAMESPACE_SUFFIX, SERVLET_CONTEXT_PREFIX
从类继承的字段 org.springframework.web.servlet.HttpServletBean
logger
构造器概要
构造器 构造器 说明 DispatcherServlet()Create a newDispatcherServletthat will create its own internal web application context based on defaults and values provided through servlet init-params.DispatcherServlet(WebApplicationContext webApplicationContext)Create a newDispatcherServletwith the given web application context.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected LocaleContextbuildLocaleContext(HttpServletRequest request)Build a LocaleContext for the given request, exposing the request's primary locale as current locale.protected HttpServletRequestcheckMultipart(HttpServletRequest request)Convert the request into a multipart request, and make multipart resolver available.protected voidcleanupMultipart(HttpServletRequest request)Clean up any resources used by the given multipart request (if any).protected ObjectcreateDefaultStrategy(ApplicationContext context, Class<?> clazz)Create a default strategy.protected voiddoDispatch(HttpServletRequest request, HttpServletResponse response)Process the actual dispatching to the handler.protected voiddoService(HttpServletRequest request, HttpServletResponse response)Exposes the DispatcherServlet-specific request attributes and delegates todoDispatch(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)for the actual dispatching.protected <T> List<T>getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface)Create a List of default strategy objects for the given strategy interface.protected <T> TgetDefaultStrategy(ApplicationContext context, Class<T> strategyInterface)Return the default strategy object for the given strategy interface.protected StringgetDefaultViewName(HttpServletRequest request)Translate the supplied request into a default view name.protected HandlerExecutionChaingetHandler(HttpServletRequest request)Return the HandlerExecutionChain for this request.protected HandlerAdaptergetHandlerAdapter(Object handler)Return the HandlerAdapter for this handler object.MultipartResolvergetMultipartResolver()Obtain this servlet's MultipartResolver, if any.ThemeSourcegetThemeSource()Return this servlet's ThemeSource, if any; else returnnull.protected voidinitStrategies(ApplicationContext context)Initialize the strategy objects that this servlet uses.protected voidnoHandlerFound(HttpServletRequest request, HttpServletResponse response)No handler found -> set appropriate HTTP response status.protected voidonRefresh(ApplicationContext context)This implementation callsinitStrategies(org.springframework.context.ApplicationContext).protected ModelAndViewprocessHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)Determine an error ModelAndView via the registered HandlerExceptionResolvers.protected voidrender(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)Render the given ModelAndView.protected ViewresolveViewName(String viewName, Map<String,Object> model, Locale locale, HttpServletRequest request)Resolve the given view name into a View object (to be rendered).voidsetCleanupAfterInclude(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.voidsetDetectAllHandlerAdapters(boolean detectAllHandlerAdapters)Set whether to detect all HandlerAdapter beans in this servlet's context.voidsetDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers)Set whether to detect all HandlerExceptionResolver beans in this servlet's context.voidsetDetectAllHandlerMappings(boolean detectAllHandlerMappings)Set whether to detect all HandlerMapping beans in this servlet's context.voidsetDetectAllViewResolvers(boolean detectAllViewResolvers)Set whether to detect all ViewResolver beans in this servlet's context.voidsetThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound)Set whether to throw a NoHandlerFoundException when no Handler was found for this request.从类继承的方法 org.springframework.web.servlet.FrameworkServlet
applyInitializers, buildRequestAttributes, configureAndRefreshWebApplicationContext, createWebApplicationContext, createWebApplicationContext, destroy, doDelete, doGet, doOptions, doPost, doPut, doTrace, findWebApplicationContext, getContextAttribute, getContextClass, getContextConfigLocation, getContextId, getNamespace, getServletContextAttributeName, getUsernameForRequest, getWebApplicationContext, initFrameworkServlet, initServletBean, initWebApplicationContext, onApplicationEvent, postProcessWebApplicationContext, processRequest, refresh, service, setApplicationContext, setContextAttribute, setContextClass, setContextConfigLocation, setContextId, setContextInitializerClasses, setContextInitializers, setDispatchOptionsRequest, setDispatchTraceRequest, setNamespace, setPublishContext, setPublishEvents, setThreadContextInheritable
从类继承的方法 org.springframework.web.servlet.HttpServletBean
addRequiredProperty, createEnvironment, getEnvironment, getServletContext, getServletName, init, initBeanWrapper, setEnvironment
从类继承的方法 javax.servlet.http.HttpServlet
doHead, getLastModified, service
从类继承的方法 javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletInfo, init, log, log
字段详细资料
MULTIPART_RESOLVER_BEAN_NAME
public static final String MULTIPART_RESOLVER_BEAN_NAME
Well-known name for the MultipartResolver object in the bean factory for this namespace.- 另请参阅:
- 常量字段值
LOCALE_RESOLVER_BEAN_NAME
public static final String LOCALE_RESOLVER_BEAN_NAME
Well-known name for the LocaleResolver object in the bean factory for this namespace.- 另请参阅:
- 常量字段值
THEME_RESOLVER_BEAN_NAME
public static final String THEME_RESOLVER_BEAN_NAME
Well-known name for the ThemeResolver object in the bean factory for this namespace.- 另请参阅:
- 常量字段值
HANDLER_MAPPING_BEAN_NAME
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.
HANDLER_ADAPTER_BEAN_NAME
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.
HANDLER_EXCEPTION_RESOLVER_BEAN_NAME
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.
REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME
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.- 另请参阅:
- 常量字段值
VIEW_RESOLVER_BEAN_NAME
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.
FLASH_MAP_MANAGER_BEAN_NAME
public static final String FLASH_MAP_MANAGER_BEAN_NAME
Well-known name for the FlashMapManager object in the bean factory for this namespace.- 另请参阅:
- 常量字段值
WEB_APPLICATION_CONTEXT_ATTRIBUTE
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.
LOCALE_RESOLVER_ATTRIBUTE
public static final String LOCALE_RESOLVER_ATTRIBUTE
Request attribute to hold the current LocaleResolver, retrievable by views.
THEME_RESOLVER_ATTRIBUTE
public static final String THEME_RESOLVER_ATTRIBUTE
Request attribute to hold the current ThemeResolver, retrievable by views.
THEME_SOURCE_ATTRIBUTE
public static final String THEME_SOURCE_ATTRIBUTE
Request attribute to hold the current ThemeSource, retrievable by views.
INPUT_FLASH_MAP_ATTRIBUTE
public static final String INPUT_FLASH_MAP_ATTRIBUTE
Name of request attribute that holds a read-onlyMap<String,?>with "input" flash attributes saved by a previous request, if any.
OUTPUT_FLASH_MAP_ATTRIBUTE
public static final String OUTPUT_FLASH_MAP_ATTRIBUTE
Name of request attribute that holds the "output"FlashMapwith attributes to save for a subsequent request.
FLASH_MAP_MANAGER_ATTRIBUTE
public static final String FLASH_MAP_MANAGER_ATTRIBUTE
Name of request attribute that holds theFlashMapManager.
EXCEPTION_ATTRIBUTE
public static final String EXCEPTION_ATTRIBUTE
Name of request attribute that exposes an Exception resolved with anHandlerExceptionResolverbut where no view was rendered (e.g. setting the status code).
PAGE_NOT_FOUND_LOG_CATEGORY
public static final String PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.- 另请参阅:
- 常量字段值
pageNotFoundLogger
protected static final Log pageNotFoundLogger
Additional logger to use when no mapped handler is found for a request.
构造器详细资料
DispatcherServlet
public DispatcherServlet()
Create a newDispatcherServletthat will create its own internal web application context based on defaults and values provided through servlet init-params. Typically used in Servlet 2.5 or earlier environments, where the only option for servlet registration is throughweb.xmlwhich requires the use of a no-arg constructor.Calling
FrameworkServlet.setContextConfigLocation(java.lang.String)(init-param 'contextConfigLocation') will dictate which XML files will be loaded by the default XmlWebApplicationContextCalling
FrameworkServlet.setContextClass(java.lang.Class<?>)(init-param 'contextClass') overrides the defaultXmlWebApplicationContextand allows for specifying an alternative class, such asAnnotationConfigWebApplicationContext.Calling
FrameworkServlet.setContextInitializerClasses(java.lang.String)(init-param 'contextInitializerClasses') indicates whichApplicationContextInitializerclasses should be used to further configure the internal application context prior to refresh().
DispatcherServlet
public DispatcherServlet(WebApplicationContext webApplicationContext)
Create a newDispatcherServletwith the given web application context. This constructor is useful in Servlet 3.0+ environments where instance-based registration of servlets is possible through theServletContext.addServlet(java.lang.String, java.lang.String)API.Using this constructor indicates that the following properties / init-params will be ignored:
FrameworkServlet.setContextClass(Class)/ 'contextClass'FrameworkServlet.setContextConfigLocation(String)/ 'contextConfigLocation'FrameworkServlet.setContextAttribute(String)/ 'contextAttribute'FrameworkServlet.setNamespace(String)/ 'namespace'
The given web application context may or may not yet be refreshed. If it has not already been refreshed (the recommended approach), then the following will occur:
- If the given context does not already have a parent, the root application context will be set as the parent.
- If the given context has not already been assigned an id, one will be assigned to it
ServletContextandServletConfigobjects will be delegated to the application contextFrameworkServlet.postProcessWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext)will be called- Any
ApplicationContextInitializers specified through the "contextInitializerClasses" init-param or through theFrameworkServlet.setContextInitializers(org.springframework.context.ApplicationContextInitializer<?>...)property will be applied. refresh()will be called if the context implementsConfigurableApplicationContext
See
WebApplicationInitializerfor usage examples.- 参数:
webApplicationContext- the context to use- 另请参阅:
FrameworkServlet.initWebApplicationContext(),FrameworkServlet.configureAndRefreshWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext),WebApplicationInitializer
方法详细资料
setDetectAllHandlerMappings
public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings)
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.
setDetectAllHandlerAdapters
public void setDetectAllHandlerAdapters(boolean detectAllHandlerAdapters)
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.
setDetectAllHandlerExceptionResolvers
public void setDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers)
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.
setDetectAllViewResolvers
public void setDetectAllViewResolvers(boolean detectAllViewResolvers)
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.
setThrowExceptionIfNoHandlerFound
public void setThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound)
Set whether to throw a NoHandlerFoundException when no Handler was found for this request. This exception can then be caught with a HandlerExceptionResolver or an@ExceptionHandlercontroller method.Note that if
DefaultServletHttpRequestHandleris used, then requests will always be forwarded to the default servlet and a NoHandlerFoundException would never be thrown in that case.Default is "false", meaning the DispatcherServlet sends a NOT_FOUND error through the Servlet response.
- 从以下版本开始:
- 4.0
setCleanupAfterInclude
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. 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.
onRefresh
protected void onRefresh(ApplicationContext context)
This implementation callsinitStrategies(org.springframework.context.ApplicationContext).- 覆盖:
onRefresh在类中FrameworkServlet- 参数:
context- the current WebApplicationContext- 另请参阅:
FrameworkServlet.refresh()
initStrategies
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.
getThemeSource
public final ThemeSource getThemeSource()
Return this servlet's ThemeSource, if any; else returnnull.Default is to return the WebApplicationContext as ThemeSource, provided that it implements the ThemeSource interface.
- 返回:
- the ThemeSource, if any
- 另请参阅:
FrameworkServlet.getWebApplicationContext()
getMultipartResolver
public final MultipartResolver getMultipartResolver()
Obtain this servlet's MultipartResolver, if any.- 返回:
- the MultipartResolver used by this servlet, or
nullif none (indicating that no multipart support is available)
getDefaultStrategy
protected <T> T getDefaultStrategy(ApplicationContext context, Class<T> strategyInterface)
Return the default strategy object for the given strategy interface.The default implementation delegates to
getDefaultStrategies(org.springframework.context.ApplicationContext, java.lang.Class<T>), expecting a single object in the list.- 参数:
context- the current WebApplicationContextstrategyInterface- the strategy interface- 返回:
- the corresponding strategy object
- 另请参阅:
getDefaultStrategies(org.springframework.context.ApplicationContext, java.lang.Class<T>)
getDefaultStrategies
protected <T> List<T> getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface)
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 WebApplicationContextstrategyInterface- the strategy interface- 返回:
- the List of corresponding strategy objects
createDefaultStrategy
protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz)
Create a default strategy.The default implementation uses
AutowireCapableBeanFactory.createBean(java.lang.Class<T>).- 参数:
context- the current WebApplicationContextclazz- the strategy implementation class to instantiate- 返回:
- the fully configured strategy instance
- 另请参阅:
ApplicationContext.getAutowireCapableBeanFactory(),AutowireCapableBeanFactory.createBean(java.lang.Class<T>)
doService
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception
Exposes the DispatcherServlet-specific request attributes and delegates todoDispatch(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)for the actual dispatching.- 指定者:
doService在类中FrameworkServlet- 参数:
request- current HTTP requestresponse- current HTTP response- 抛出:
Exception- in case of any kind of processing failure- 另请参阅:
HttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse),HttpServlet.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
doDispatch
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.
- 参数:
request- current HTTP requestresponse- current HTTP response- 抛出:
Exception- in case of any kind of processing failure
buildLocaleContext
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.
- 覆盖:
buildLocaleContext在类中FrameworkServlet- 参数:
request- current HTTP request- 返回:
- the corresponding LocaleContext
- 另请参阅:
LocaleContextHolder.setLocaleContext(org.springframework.context.i18n.LocaleContext)
checkMultipart
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.
- 参数:
request- current HTTP request- 返回:
- the processed request (multipart wrapper if necessary)
- 抛出:
MultipartException- 另请参阅:
MultipartResolver.resolveMultipart(javax.servlet.http.HttpServletRequest)
cleanupMultipart
protected void cleanupMultipart(HttpServletRequest request)
Clean up any resources used by the given multipart request (if any).- 参数:
request- current HTTP request- 另请参阅:
MultipartResolver.cleanupMultipart(org.springframework.web.multipart.MultipartHttpServletRequest)
getHandler
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception
Return the HandlerExecutionChain for this request.Tries all handler mappings in order.
- 参数:
request- current HTTP request- 返回:
- the HandlerExecutionChain, or
nullif no handler could be found - 抛出:
Exception
noHandlerFound
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception
No handler found -> set appropriate HTTP response status.- 参数:
request- current HTTP requestresponse- current HTTP response- 抛出:
Exception- if preparing the response failed
getHandlerAdapter
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException
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.
processHandlerException
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
Determine an error ModelAndView via the registered HandlerExceptionResolvers.- 参数:
request- current HTTP requestresponse- current HTTP responsehandler- the executed handler, ornullif none chosen at the time of the exception (for example, if multipart resolution failed)ex- the exception that got thrown during handler execution- 返回:
- a corresponding ModelAndView to forward to
- 抛出:
Exception- if no error ModelAndView found
render
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.
- 参数:
mv- the ModelAndView to renderrequest- current HTTP servlet requestresponse- current HTTP servlet response- 抛出:
ServletException- if view is missing or cannot be resolvedException- if there's a problem rendering the view
getDefaultViewName
protected String getDefaultViewName(HttpServletRequest request) throws Exception
Translate the supplied request into a default view name.- 参数:
request- current HTTP servlet request- 返回:
- the view name (or
nullif no default found) - 抛出:
Exception- if view name translation failed
resolveViewName
protected View resolveViewName(String viewName, Map<String,Object> model, Locale locale, HttpServletRequest request) throws Exception
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 resolvemodel- the model to be passed to the viewlocale- the current localerequest- current HTTP servlet request- 返回:
- the View object, or
nullif none found - 抛出:
Exception- if the view cannot be resolved (typically in case of problems creating an actual View object)- 另请参阅:
ViewResolver.resolveViewName(java.lang.String, java.util.Locale)