搜索
首页Javajava教程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
是否有任何威胁或增强Java平台独立性的新兴技术?是否有任何威胁或增强Java平台独立性的新兴技术?Apr 24, 2025 am 12:11 AM

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

JVM的实现是什么,它们都提供了相同的平台独立性?JVM的实现是什么,它们都提供了相同的平台独立性?Apr 24, 2025 am 12:10 AM

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性如何降低发展成本和时间?平台独立性如何降低发展成本和时间?Apr 24, 2025 am 12:08 AM

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。

Java的平台独立性如何促进代码重用?Java的平台独立性如何促进代码重用?Apr 24, 2025 am 12:05 AM

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

您如何在Java应用程序中对平台特定问题进行故障排除?您如何在Java应用程序中对平台特定问题进行故障排除?Apr 24, 2025 am 12:04 AM

要解决Java应用程序中的平台特定问题,可以采取以下步骤:1.使用Java的System类查看系统属性以了解运行环境。2.利用File类或java.nio.file包处理文件路径。3.根据操作系统条件加载本地库。4.使用VisualVM或JProfiler优化跨平台性能。5.通过Docker容器化确保测试环境与生产环境一致。6.利用GitHubActions在多个平台上进行自动化测试。这些方法有助于有效地解决Java应用程序中的平台特定问题。

JVM中的类加载程序子系统如何促进平台独立性?JVM中的类加载程序子系统如何促进平台独立性?Apr 23, 2025 am 12:14 AM

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器会产生特定于平台的代码吗?解释。Java编译器会产生特定于平台的代码吗?解释。Apr 23, 2025 am 12:09 AM

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

JVM如何处理不同操作系统的多线程?JVM如何处理不同操作系统的多线程?Apr 23, 2025 am 12:07 AM

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。

See all articles

热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

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

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),