本文主要是介绍Spring中DispatcherServlet类源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DispatcherServlet类源码
public class DispatcherServlet extends FrameworkServlet {public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; public static final String LOCALE_RESOLVER_BEAN_NAME = "localeResolver"; public static final String THEME_RESOLVER_BEAN_NAME = "themeResolver"; public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping"; public static final String HANDLER_ADAPTER_BEAN_NAME = "handlerAdapter"; public static final String HANDLER_EXCEPTION_RESOLVER_BEAN_NAME = "handlerExceptionResolver"; public static final String REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME = "viewNameTranslator"; public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver"; public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager"; public static final String WEB_APPLICATION_CONTEXT_ATTRIBUTE = DispatcherServlet.class.getName() + ".CONTEXT"; public static final String LOCALE_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".LOCALE_RESOLVER"; public static final String THEME_RESOLVER_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_RESOLVER"; public static final String THEME_SOURCE_ATTRIBUTE = DispatcherServlet.class.getName() + ".THEME_SOURCE"; public static final String INPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".INPUT_FLASH_MAP"; public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP"; public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER"; public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; private static final String DEFAULT_STRATEGIES_PATH = "DispatcherServlet.properties"; protected static final Log pageNotFoundLogger = LogFactory.getLog("org.springframework.web.servlet.PageNotFound"); private static final UrlPathHelper urlPathHelper = new UrlPathHelper(); private static final Properties defaultStrategies; private boolean detectAllHandlerMappings = true; private boolean detectAllHandlerAdapters = true; private boolean detectAllHandlerExceptionResolvers = true; private boolean detectAllViewResolvers = true; private boolean cleanupAfterInclude = true; private MultipartResolver multipartResolver; private LocaleResolver localeResolver; private ThemeResolver themeResolver; private List<HandlerMapping> handlerMappings; private List<HandlerAdapter> handlerAdapters; private List<HandlerExceptionResolver> handlerExceptionResolvers; private RequestToViewNameTranslator viewNameTranslator; private FlashMapManager flashMapManager; private List<ViewResolver> viewResolvers; static {try {ClassPathResource ex = new ClassPathResource("DispatcherServlet.properties", DispatcherServlet.class); defaultStrategies = PropertiesLoaderUtils.loadProperties(ex); } catch (IOException var1) {throw new IllegalStateException("Could not load \'DispatcherServlet.properties\': " + var1.getMessage()); }}public DispatcherServlet() {}public DispatcherServlet(WebApplicationContext webApplicationContext) {super(webApplicationContext); }public void setDetectAllHandlerMappings(boolean detectAllHandlerMappings) {this.detectAllHandlerMappings = detectAllHandlerMappings; }public void setDetectAllHandlerAdapters(boolean detectAllHandlerAdapters) {this.detectAllHandlerAdapters = detectAllHandlerAdapters; }public void setDetectAllHandlerExceptionResolvers(boolean detectAllHandlerExceptionResolvers) {this.detectAllHandlerExceptionResolvers = detectAllHandlerExceptionResolvers; }public void setDetectAllViewResolvers(boolean detectAllViewResolvers) {this.detectAllViewResolvers = detectAllViewResolvers; }public void setCleanupAfterInclude(boolean cleanupAfterInclude) {this.cleanupAfterInclude = cleanupAfterInclude; }protected void onRefresh(ApplicationContext context) {this.initStrategies(context); }protected void initStrategies(ApplicationContext context) {this.initMultipartResolver(context); this.initLocaleResolver(context); this.initThemeResolver(context); this.initHandlerMappings(context); this.initHandlerAdapters(context); this.initHandlerExceptionResolvers(context); this.initRequestToViewNameTranslator(context); this.initViewResolvers(context); this.initFlashMapManager(context); }private void initMultipartResolver(ApplicationContext context) {try {this.multipartResolver = (MultipartResolver)context.getBean("multipartResolver", MultipartResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Using MultipartResolver [" + this.multipartResolver + "]"); }} catch (NoSuchBeanDefinitionException var2) {this.multipartResolver = null; if(this.logger.isDebugEnabled()) {this.logger.debug("Unable to locate MultipartResolver with name \'multipartResolver\': no multipart request handling provided"); }}}private void initLocaleResolver(ApplicationContext context) {try {this.localeResolver = (LocaleResolver)context.getBean("localeResolver", LocaleResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Using LocaleResolver [" + this.localeResolver + "]"); }} catch (NoSuchBeanDefinitionException var2) {this.localeResolver = (LocaleResolver)this.getDefaultStrategy(context, LocaleResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Unable to locate LocaleResolver with name \'localeResolver\': using default [" + this.localeResolver + "]"); }}}private void initThemeResolver(ApplicationContext context) {try {this.themeResolver = (ThemeResolver)context.getBean("themeResolver", ThemeResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Using ThemeResolver [" + this.themeResolver + "]"); }} catch (NoSuchBeanDefinitionException var2) {this.themeResolver = (ThemeResolver)this.getDefaultStrategy(context, ThemeResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Unable to locate ThemeResolver with name \'themeResolver\': using default [" + this.themeResolver + "]"); }}}private void initHandlerMappings(ApplicationContext context) {this.handlerMappings = null; if(this.detectAllHandlerMappings) {Map hm = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false); if(!hm.isEmpty()) {this.handlerMappings = new ArrayList(hm.values()); OrderComparator.sort(this.handlerMappings); }} else {try {HandlerMapping hm1 = (HandlerMapping)context.getBean("handlerMapping", HandlerMapping.class); this.handlerMappings = Collections.singletonList(hm1); } catch (NoSuchBeanDefinitionException var3) {; }}if(this.handlerMappings == null) {this.handlerMappings = this.getDefaultStrategies(context, HandlerMapping.class); if(this.logger.isDebugEnabled()) {this.logger.debug("No HandlerMappings found in servlet \'" + this.getServletName() + "\': using default"); }}}private void initHandlerAdapters(ApplicationContext context) {this.handlerAdapters = null; if(this.detectAllHandlerAdapters) {Map ha = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.class, true, false); if(!ha.isEmpty()) {this.handlerAdapters = new ArrayList(ha.values()); OrderComparator.sort(this.handlerAdapters); }} else {try {HandlerAdapter ha1 = (HandlerAdapter)context.getBean("handlerAdapter", HandlerAdapter.class); this.handlerAdapters = Collections.singletonList(ha1); } catch (NoSuchBeanDefinitionException var3) {; }}if(this.handlerAdapters == null) {this.handlerAdapters = this.getDefaultStrategies(context, HandlerAdapter.class); if(this.logger.isDebugEnabled()) {this.logger.debug("No HandlerAdapters found in servlet \'" + this.getServletName() + "\': using default"); }}}private void initHandlerExceptionResolvers(ApplicationContext context) {this.handlerExceptionResolvers = null; if(this.detectAllHandlerExceptionResolvers) {Map her = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerExceptionResolver.class, true, false); if(!her.isEmpty()) {this.handlerExceptionResolvers = new ArrayList(her.values()); OrderComparator.sort(this.handlerExceptionResolvers); }} else {try {HandlerExceptionResolver her1 = (HandlerExceptionResolver)context.getBean("handlerExceptionResolver", HandlerExceptionResolver.class); this.handlerExceptionResolvers = Collections.singletonList(her1); } catch (NoSuchBeanDefinitionException var3) {; }}if(this.handlerExceptionResolvers == null) {this.handlerExceptionResolvers = this.getDefaultStrategies(context, HandlerExceptionResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("No HandlerExceptionResolvers found in servlet \'" + this.getServletName() + "\': using default"); }}}private void initRequestToViewNameTranslator(ApplicationContext context) {try {this.viewNameTranslator = (RequestToViewNameTranslator)context.getBean("viewNameTranslator", RequestToViewNameTranslator.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Using RequestToViewNameTranslator [" + this.viewNameTranslator + "]"); }} catch (NoSuchBeanDefinitionException var2) {this.viewNameTranslator = (RequestToViewNameTranslator)this.getDefaultStrategy(context, RequestToViewNameTranslator.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Unable to locate RequestToViewNameTranslator with name \'viewNameTranslator\': using default [" + this.viewNameTranslator + "]"); }}}private void initViewResolvers(ApplicationContext context) {this.viewResolvers = null; if(this.detectAllViewResolvers) {Map vr = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, ViewResolver.class, true, false); if(!vr.isEmpty()) {this.viewResolvers = new ArrayList(vr.values()); OrderComparator.sort(this.viewResolvers); }} else {try {ViewResolver vr1 = (ViewResolver)context.getBean("viewResolver", ViewResolver.class); this.viewResolvers = Collections.singletonList(vr1); } catch (NoSuchBeanDefinitionException var3) {; }}if(this.viewResolvers == null) {this.viewResolvers = this.getDefaultStrategies(context, ViewResolver.class); if(this.logger.isDebugEnabled()) {this.logger.debug("No ViewResolvers found in servlet \'" + this.getServletName() + "\': using default"); }}}private void initFlashMapManager(ApplicationContext context) {try {this.flashMapManager = (FlashMapManager)context.getBean("flashMapManager", FlashMapManager.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Using FlashMapManager [" + this.flashMapManager + "]"); }} catch (NoSuchBeanDefinitionException var2) {this.flashMapManager = (FlashMapManager)this.getDefaultStrategy(context, FlashMapManager.class); if(this.logger.isDebugEnabled()) {this.logger.debug("Unable to locate FlashMapManager with name \'flashMapManager\': using default [" + this.flashMapManager + "]"); }}}public final ThemeSource getThemeSource() {return this.getWebApplicationContext() instanceof ThemeSource?(ThemeSource)this.getWebApplicationContext():null; }public final MultipartResolver getMultipartResolver() {return this.multipartResolver; }protected <T> T getDefaultStrategy(ApplicationContext context, Class<T> strategyInterface) {List strategies = this.getDefaultStrategies(context, strategyInterface); if(strategies.size() != 1) {throw new BeanInitializationException("DispatcherServlet needs exactly 1 strategy for interface [" + strategyInterface.getName() + "]"); } else {return strategies.get(0); }}protected <T> List<T> getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface) {String key = strategyInterface.getName(); String value = defaultStrategies.getProperty(key); if(value == null) {return new LinkedList(); } else {String[] classNames = StringUtils.commaDelimitedListToStringArray(value); ArrayList strategies = new ArrayList(classNames.length); String[] var10 = classNames; int var9 = classNames.length; for(int var8 = 0; var8 < var9; ++var8) {String className = var10[var8]; try {Class err = ClassUtils.forName(className, DispatcherServlet.class.getClassLoader()); Object strategy = this.createDefaultStrategy(context, err); strategies.add(strategy); } catch (ClassNotFoundException var13) {throw new BeanInitializationException("Could not find DispatcherServlet\'s default strategy class [" + className + "] for interface [" + key + "]", var13); } catch (LinkageError var14) {throw new BeanInitializationException("Error loading DispatcherServlet\'s default strategy class [" + className + "] for interface [" + key + "]: problem with class file or dependent class", var14); }}return strategies; }}protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) {return context.getAutowireCapableBeanFactory().createBean(clazz); }protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {if(this.logger.isDebugEnabled()) {String attributesSnapshot = urlPathHelper.getRequestUri(request); this.logger.debug("DispatcherServlet with name \'" + this.getServletName() + "\' processing " + request.getMethod() + " request for [" + attributesSnapshot + "]"); }HashMap attributesSnapshot1 = null; if(WebUtils.isIncludeRequest(request)) {this.logger.debug("Taking snapshot of request attributes before include"); attributesSnapshot1 = new HashMap(); Enumeration inputFlashMap = request.getAttributeNames(); label90:while(true) {String attrName; do {if(!inputFlashMap.hasMoreElements()) {break label90; }attrName = (String)inputFlashMap.nextElement(); } while(!this.cleanupAfterInclude && !attrName.startsWith("org.springframework.web.servlet")); attributesSnapshot1.put(attrName, request.getAttribute(attrName)); }}request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.getWebApplicationContext()); request.setAttribute(LOCALE_RESOLVER_ATTRIBUTE, this.localeResolver); request.setAttribute(THEME_RESOLVER_ATTRIBUTE, this.themeResolver); request.setAttribute(THEME_SOURCE_ATTRIBUTE, this.getThemeSource()); FlashMap inputFlashMap1 = this.flashMapManager.retrieveAndUpdate(request, response); if(inputFlashMap1 != null) {request.setAttribute(INPUT_FLASH_MAP_ATTRIBUTE, Collections.unmodifiableMap(inputFlashMap1)); }request.setAttribute(OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); request.setAttribute(FLASH_MAP_MANAGER_ATTRIBUTE, this.flashMapManager); try {this.doDispatch(request, response); } finally {if(attributesSnapshot1 != null) {this.restoreAttributesAfterInclude(request, attributesSnapshot1); }}}protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request; HandlerExecutionChain mappedHandler = null; int interceptorIndex = -1; try {boolean var25 = false; ModelAndView err; try {processedRequest = this.checkMultipart(request); mappedHandler = this.getHandler(processedRequest, false); if(mappedHandler == null || mappedHandler.getHandler() == null) {this.noHandlerFound(processedRequest, response); return; }HandlerAdapter ex1 = this.getHandlerAdapter(mappedHandler.getHandler()); String var26 = request.getMethod(); boolean isGet = "GET".equals(var26); if(isGet || "HEAD".equals(var26)) {long interceptors = ex1.getLastModified(request, mappedHandler.getHandler()); if(this.logger.isDebugEnabled()) {String interceptor = urlPathHelper.getRequestUri(request); this.logger.debug("Last-Modified value for [" + interceptor + "] is: " + interceptors); }if((new ServletWebRequest(request, response)).checkNotModified(interceptors) && isGet) {return; }}HandlerInterceptor[] var27 = mappedHandler.getInterceptors(); int i; HandlerInterceptor var28; if(var27 != null) {for(i = 0; i < var27.length; interceptorIndex = i++) {var28 = var27[i]; if(!var28.preHandle(processedRequest, response, mappedHandler.getHandler())) {this.triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, (Exception)null); return; }}}err = ex1.handle(processedRequest, response, mappedHandler.getHandler()); if(err != null && !err.hasView()) {err.setViewName(this.getDefaultViewName(request)); }if(var27 != null) {for(i = var27.length - 1; i >= 0; --i) {var28 = var27[i]; var28.postHandle(processedRequest, response, mappedHandler.getHandler(), err); }}} catch (ModelAndViewDefiningException var20) {this.logger.debug("ModelAndViewDefiningException encountered", var20); err = var20.getModelAndView(); } catch (Exception var21) {Object handler = mappedHandler != null?mappedHandler.getHandler():null; err = this.processHandlerException(processedRequest, response, handler, var21); var25 = err != null; }if(err != null && !err.wasCleared()) {this.render(err, processedRequest, response); if(var25) {WebUtils.clearErrorRequestAttributes(request); }} else if(this.logger.isDebugEnabled()) {this.logger.debug("Null ModelAndView returned to DispatcherServlet with name \'" + this.getServletName() + "\': assuming HandlerAdapter completed request handling"); }this.triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, (Exception)null); } catch (Exception var22) {this.triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, var22); throw var22; } catch (Error var23) {NestedServletException ex = new NestedServletException("Handler processing failed", var23); this.triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex); throw ex; } finally {if(processedRequest != request) {this.cleanupMultipart(processedRequest); }}}protected LocaleContext buildLocaleContext(final HttpServletRequest request) {return new LocaleContext() {public Locale getLocale() {return DispatcherServlet.this.localeResolver.resolveLocale(request); }public String toString() {return this.getLocale().toString(); }}; }protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {if(this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {if(!(request instanceof MultipartHttpServletRequest)) {return this.multipartResolver.resolveMultipart(request); }this.logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, this typically results from an additional MultipartFilter in web.xml"); }return request; }protected void cleanupMultipart(HttpServletRequest request) {if(request instanceof MultipartHttpServletRequest) {this.multipartResolver.cleanupMultipart((MultipartHttpServletRequest)request); }}/** @deprecated */ @Deprecatedprotected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache) throws Exception {return this.getHandler(request); }protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {Iterator var3 = this.handlerMappings.iterator(); HandlerExecutionChain handler; do {if(!var3.hasNext()) {return null; }HandlerMapping hm = (HandlerMapping)var3.next(); if(this.logger.isTraceEnabled()) {this.logger.trace("Testing handler map [" + hm + "] in DispatcherServlet with name \'" + this.getServletName() + "\'"); }handler = hm.getHandler(request); } while(handler == null); return handler; }protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {if(pageNotFoundLogger.isWarnEnabled()) {String requestUri = urlPathHelper.getRequestUri(request); pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri + "] in DispatcherServlet with name \'" + this.getServletName() + "\'"); }response.sendError(404); }protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {Iterator var3 = this.handlerAdapters.iterator(); HandlerAdapter ha; do {if(!var3.hasNext()) {throw new ServletException("No adapter for handler [" + handler + "]: Does your handler implement a supported interface like Controller?"); }ha = (HandlerAdapter)var3.next(); if(this.logger.isTraceEnabled()) {this.logger.trace("Testing handler adapter [" + ha + "]"); }} while(!ha.supports(handler)); return ha; }protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {ModelAndView exMv = null; Iterator var7 = this.handlerExceptionResolvers.iterator(); while(var7.hasNext()) {HandlerExceptionResolver handlerExceptionResolver = (HandlerExceptionResolver)var7.next(); exMv = handlerExceptionResolver.resolveException(request, response, handler, ex); if(exMv != null) {break; }}if(exMv != null) {if(exMv.isEmpty()) {return null; } else {if(!exMv.hasView()) {exMv.setViewName(this.getDefaultViewName(request)); }if(this.logger.isDebugEnabled()) {this.logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv, ex); }WebUtils.exposeErrorRequestAttributes(request, ex, this.getServletName()); return exMv; }} else {throw ex; }}protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale); View view; if(mv.isReference()) {view = this.resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); if(view == null) {throw new ServletException("Could not resolve view with name \'" + mv.getViewName() + "\' in servlet with name \'" + this.getServletName() + "\'"); }} else {view = mv.getView(); if(view == null) {throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name \'" + this.getServletName() + "\'"); }}if(this.logger.isDebugEnabled()) {this.logger.debug("Rendering view [" + view + "] in DispatcherServlet with name \'" + this.getServletName() + "\'"); }view.render(mv.getModelInternal(), request, response); }protected String getDefaultViewName(HttpServletRequest request) throws Exception {return this.viewNameTranslator.getViewName(request); }protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale, HttpServletRequest request) throws Exception {Iterator var6 = this.viewResolvers.iterator(); while(var6.hasNext()) {ViewResolver viewResolver = (ViewResolver)var6.next(); View view = viewResolver.resolveViewName(viewName, locale); if(view != null) {return view; }}return null; }private void triggerAfterCompletion(HandlerExecutionChain mappedHandler, int interceptorIndex, HttpServletRequest request, HttpServletResponse response, Exception ex) throws Exception {if(mappedHandler != null) {HandlerInterceptor[] interceptors = mappedHandler.getInterceptors(); if(interceptors != null) {for(int i = interceptorIndex; i >= 0; --i) {HandlerInterceptor interceptor = interceptors[i]; try {interceptor.afterCompletion(request, response, mappedHandler.getHandler(), ex); } catch (Throwable var10) {this.logger.error("HandlerInterceptor.afterCompletion threw exception", var10); }}}}}private void restoreAttributesAfterInclude(HttpServletRequest request, Map<?, ?> attributesSnapshot) {this.logger.debug("Restoring snapshot of request attributes after include"); HashSet attrsToCheck = new HashSet(); Enumeration attrNames = request.getAttributeNames(); while(true) {String attrName; do {if(!attrNames.hasMoreElements()) {Iterator var6 = attrsToCheck.iterator(); while(var6.hasNext()) {attrName = (String)var6.next(); Object attrValue = attributesSnapshot.get(attrName); if(attrValue == null) {if(this.logger.isDebugEnabled()) {this.logger.debug("Removing attribute [" + attrName + "] after include"); }request.removeAttribute(attrName); } else if(attrValue != request.getAttribute(attrName)) {if(this.logger.isDebugEnabled()) {this.logger.debug("Restoring original value of attribute [" + attrName + "] after include"); }request.setAttribute(attrName, attrValue); }}return; }attrName = (String)attrNames.nextElement(); } while(!this.cleanupAfterInclude && !attrName.startsWith("org.springframework.web.servlet")); attrsToCheck.add(attrName); }} }
这篇关于Spring中DispatcherServlet类源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!