首页 Java java教程 DispatcherServlet类讲解

DispatcherServlet类讲解

Jul 24, 2017 pm 04:22 PM
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<HandlerMapping> handlerMappings;   //处理器映射列表/** List of HandlerAdapters used by this servlet */private List<HandlerAdapter> handlerAdapters;   //处理器适配器列表/** List of HandlerExceptionResolvers used by this servlet */private List<HandlerExceptionResolver> 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<ViewResolver> viewResolvers; //视图解析器列表
登录后复制

 

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

<span style="color: #008000">/**</span><span style="color: #008000"> * Create a new {</span><span style="color: #808080">@code</span><span style="color: #008000"> DispatcherServlet} 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 {</span><span style="color: #808080">@code</span><span style="color: #008000"> web.xml} which requires the use
     * of a no-arg constructor.
     * <p>Calling {</span><span style="color: #808080">@link</span><span style="color: #008000"> #setContextConfigLocation} (init-param 'contextConfigLocation')
     * will dictate which XML files will be loaded by the
     * {</span><span style="color: #808080">@linkplain</span><span style="color: #008000"> #DEFAULT_CONTEXT_CLASS default XmlWebApplicationContext}
     * <p>Calling {</span><span style="color: #808080">@link</span><span style="color: #008000"> #setContextClass} (init-param 'contextClass') overrides the
     * default {</span><span style="color: #808080">@code</span><span style="color: #008000"> XmlWebApplicationContext} and allows for specifying an alternative class,
     * such as {</span><span style="color: #808080">@code</span><span style="color: #008000"> AnnotationConfigWebApplicationContext}.
     * <p>Calling {</span><span style="color: #808080">@link</span><span style="color: #008000"> #setContextInitializerClasses} (init-param 'contextInitializerClasses')
     * indicates which {</span><span style="color: #808080">@code</span><span style="color: #008000"> ApplicationContextInitializer} classes should be used to
     * further configure the internal application context prior to refresh().
     * </span><span style="color: #808080">@see</span><span style="color: #008000"> #DispatcherServlet(WebApplicationContext)<br>     * 
     </span><span style="color: #008000">*/</span><span style="color: #0000ff">public</span><span style="color: #000000"> DispatcherServlet() {</span><span style="color: #0000ff">super</span><span style="color: #000000">();
        setDispatchOptionsRequest(</span><span style="color: #0000ff">true</span><span style="color: #000000">);
    }</span>
登录后复制

 

 =================下面是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) + "]"<String, Object> attributesSnapshot = =  HashMap<String, Object><?> 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.
     * <p>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.
     * <p>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).
     * <p>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<String, Object> 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;
    }
登录后复制

 

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

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

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

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

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

以上是DispatcherServlet类讲解的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何使用MySQL数据库进行预测和预测分析? 如何使用MySQL数据库进行预测和预测分析? Jul 12, 2023 pm 08:43 PM

如何使用MySQL数据库进行预测和预测分析?概述:预测和预测分析在数据分析中扮演着重要角色。MySQL作为一种广泛使用的关系型数据库管理系统,也可以用于预测和预测分析任务。本文将介绍如何使用MySQL进行预测和预测分析,并提供相关的代码示例。数据准备:首先,我们需要准备相关的数据。假设我们要进行销售预测,我们需要具有销售数据的表。在MySQL中,我们可以使用

如何在uniapp中实现数据统计和分析 如何在uniapp中实现数据统计和分析 Oct 24, 2023 pm 12:37 PM

如何在uniapp中实现数据统计和分析一、背景介绍数据统计和分析是移动应用开发过程中非常重要的一环,通过对用户行为的统计和分析,开发者可以深入了解用户的喜好和使用习惯,从而优化产品设计和用户体验。本文将介绍如何在uniapp中实现数据统计和分析的功能,并提供一些具体的代码示例。二、选择合适的数据统计和分析工具在uniapp中实现数据统计和分析的第一步是选择合

Linux下的实时日志监控与分析 Linux下的实时日志监控与分析 Jul 29, 2023 am 08:06 AM

Linux下的实时日志监控与分析在日常的系统管理和故障排查中,日志是一个非常重要的数据来源。通过对系统日志的实时监控和分析,我们可以及时发现异常情况并进行相应的处理。本文将介绍Linux下如何进行实时日志监控和分析,并提供相应的代码示例。一、实时日志监控在Linux下,最常用的日志系统是rsyslog。通过配置rsyslog,我们可以实现将不同应用程序的日志

如何使用 Go 语言进行数据可视化分析? 如何使用 Go 语言进行数据可视化分析? Jun 10, 2023 am 10:46 AM

随着大数据时代的到来,数据可视化分析在各行各业中扮演着至关重要的角色。而Go语言作为一种快速、高效、安全的编程语言,也逐渐在数据可视化分析领域占据一席之地。本文将探讨如何使用Go语言进行数据可视化分析。一、Go语言常用的数据可视化库Plotly:可用于在浏览器中创建交互式的图形,支持多种图形类型,如线图、条形图、散点图、热力图等。Gonum/plo

统计分析法的步骤 统计分析法的步骤 Jun 28, 2023 pm 03:27 PM

统计分析,常指对收集到的有关数据资料进行整理归类并进行解释的过程。统计分析的基本步骤包括:1、收集数据;2、整理数据;3、分析数据。

织梦CMS二级目录打不开的原因分析 织梦CMS二级目录打不开的原因分析 Mar 13, 2024 pm 06:24 PM

标题:解析织梦CMS二级目录打不开的原因及解决方案织梦CMS(DedeCMS)是一款功能强大的开源内容管理系统,被广泛应用于各类网站建设中。然而,有时候在搭建网站过程中可能会遇到二级目录无法打开的情况,这给网站的正常运行带来了困扰。在本文中,我们将分析二级目录打不开的可能原因,并提供具体的代码示例来解决这一问题。一、可能的原因分析:伪静态规则配置问题:在使用

Python在智能交通系统中的应用案例分析 Python在智能交通系统中的应用案例分析 Sep 08, 2023 am 08:13 AM

Python在智能交通系统中的应用案例分析摘要:随着智能交通系统的快速发展,Python作为一种多功能、易于学习和使用的编程语言,被广泛应用于智能交通系统的开发和应用中。本文通过分析Python在智能交通系统中的应用案例,并给出相关的代码示例,展示了Python在智能交通领域中的优势和应用潜力。引言智能交通系统是指利用现代通信、信息、传感等技术手段,通过对交

分析腾讯主要的编程语言是否为Go 分析腾讯主要的编程语言是否为Go Mar 27, 2024 pm 04:21 PM

标题:腾讯主要的编程语言是否为Go:一项深入分析腾讯作为中国领先的科技公司,在编程语言的选择上一直备受关注。近年来,有人认为腾讯主要采用Go作为主要的编程语言。本文将对腾讯主要的编程语言是否为Go进行深入分析,并给出具体的代码示例来支持这一观点。一、Go语言在腾讯的应用Go是一种由Google开发的开源编程语言,它的高效性、并发性和简洁性受到众多开发者的喜

See all articles