java的dispatcher_DispatcherServlet类讲解

=================下面是类定义以及类注释的一些翻译,还需要进行修改。=================

/**

"serial" DispatcherServlet}

========================下面是这个类的部分比较重要的属性========================

/** MultipartResolver used by this servlet */private MultipartResolver multipartResolver; /** LocaleResolver used by this servlet */private LocaleResolver localeResolver;/** ThemeResolver used by this servlet */private ThemeResolver themeResolver;/** List of HandlerMappings used by this servlet */private List handlerMappings; //处理器映射列表/** List of HandlerAdapters used by this servlet */private List handlerAdapters; //处理器适配器列表/** List of HandlerExceptionResolvers used by this servlet */private List handlerExceptionResolvers; //处理器异常解析器列表/** RequestToViewNameTranslator used by this servlet */private RequestToViewNameTranslator viewNameTranslator;/** FlashMapManager used by this servlet */private FlashMapManager flashMapManager;/** List of ViewResolvers used by this servlet */private List viewResolvers; //视图解析器列表

============================下面是类的无参构造器============================

/*** Create a new {@codeDispatcherServlet} that 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 through {@codeweb.xml} which requires the use

* of a no-arg constructor.

*

Calling {@link#setContextConfigLocation} (init-param 'contextConfigLocation')

* will dictate which XML files will be loaded by the

* {@linkplain#DEFAULT_CONTEXT_CLASS default XmlWebApplicationContext}

*

Calling {@link#setContextClass} (init-param 'contextClass') overrides the

* default {@codeXmlWebApplicationContext} and allows for specifying an alternative class,

* such as {@codeAnnotationConfigWebApplicationContext}.

*

Calling {@link#setContextInitializerClasses} (init-param 'contextInitializerClasses')

* indicates which {@codeApplicationContextInitializer} classes should be used to

* further configure the internal application context prior to refresh().

*@see#DispatcherServlet(WebApplicationContext)

**/publicDispatcherServlet() {super();

setDispatchOptionsRequest(true);

}

=================下面是initStrategies(ApplicationContext context)方法=================

主要是初始化上面提到的部分重要属性。

//初始化这个DispatcherServlet使用到的策略对象。这个方法有可能会被子类覆盖。protected void initStrategies(ApplicationContext context) { initMultipartResolver(context);

initLocaleResolver(context);

initThemeResolver(context);/*

*

*

*/

initHandlerMappings(context);initHandlerExceptionResolvers(context);

initRequestToViewNameTranslator(context);

initViewResolvers(context);

initFlashMapManager(context);

}

========================下面是DispatherServlet的doService()方法==================

doService(HttpServletRequest request, HttpServletResponse response) = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : """DispatcherServlet with name '" + getServletName() + "'" + resumed +

" processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]" attributesSnapshot = = HashMap> attrNames == (.cleanupAfterInclude || attrName.startsWith("org.springframework.web.servlet"= (inputFlashMap != (! (attributesSnapshot !=

=========================================================================

doDispatch(HttpServletRequest request, HttpServletResponse response) == multipartRequestParsed = WebAsyncManager asyncManager == = processedRequest == (processedRequest !=mappedHandler = (mappedHandler == || mappedHandler.getHandler() == HandlerAdapter ha =String method = isGet = "GET" (isGet || "HEAD" lastModified ="Last-Modified value for [" + getRequestUri(request) + "] is: " + ( ServletWebRequest(request, response).checkNotModified(lastModified) && (!mv ==dispatchException = NestedServletException("Handler dispatch failed" NestedServletException("Handler processing failed" (mappedHandler !=

=========================================================================

/** * Return the HandlerExecutionChain for this request.

*

Tries all handler mappings in order.

* @param request current HTTP request

* @return the HandlerExecutionChain, or {@code null} if no handler could be found */protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {for (HandlerMapping hm : this.handlerMappings) {if (logger.isTraceEnabled()) {

logger.trace("Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");

}

HandlerExecutionChain handler = hm.getHandler(request);if (handler != null) {return handler;

}

}return null;

}

=========================================================================

/** * Render the given ModelAndView.

*

This is the last stage in handling a request. It may involve resolving the view by name.

* @param mv the ModelAndView to render

* @param request current HTTP servlet request

* @param response current HTTP servlet response

* @throws ServletException if view is missing or cannot be resolved

* @throws Exception if there's a problem rendering the view */protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {// Determine locale for request and apply it to the response.Locale locale = this.localeResolver.resolveLocale(request);

response.setLocale(locale);

View view;if (mv.isReference()) {// We need to resolve the view name.view = 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 '" + getServletName() + "'");

}

}else {// No need to lookup: the ModelAndView object contains the actual View object.view = mv.getView();if (view == null) {throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +

"View object in servlet with name '" + getServletName() + "'");

}

}// Delegate to the View object for rendering.if (logger.isDebugEnabled()) {

logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");

}try {if (mv.getStatus() != null) {

response.setStatus(mv.getStatus().value());

}

view.render(mv.getModelInternal(), request, response);

}catch (Exception ex) {if (logger.isDebugEnabled()) {

logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" +getServletName() + "'", ex);

}throw ex;

}

}

=========================================================================

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

* @param viewName the name of the view to resolve

* @param model the model to be passed to the view

* @param locale the current locale

* @param request current HTTP servlet request

* @return the View object, or {@code null} if none found

* @throws Exception if the view cannot be resolved

* (typically in case of problems creating an actual View object)

* @see ViewResolver#resolveViewName */protected View resolveViewName(String viewName, Map model, Locale locale,

HttpServletRequest request) throws Exception {for (ViewResolver viewResolver : this.viewResolvers) {

View view = viewResolver.resolveViewName(viewName, locale);if (view != null) {return view;

}

}return null;

}

=========================================================================

=========================================================================

=========================================================================

=========================================================================

=========================================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值