搜尋
首頁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; //视图解析器列表</viewresolver></handlerexceptionresolver></handleradapter></handlermapping>

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

下面是類別的無參建構子

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

<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 {</p></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 {</p></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 {</p></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);
    }

 

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

下面是Dispather

Servlet的doService()方法
==================

#

        doService(HttpServletRequest request, HttpServletResponse response) = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : """DispatcherServlet with name '" + getServletName() + "'" + resumed +
                    " processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]"<string> attributesSnapshot = =  HashMap<string>> attrNames == (.cleanupAfterInclude || attrName.startsWith("org.springframework.web.servlet"=  (inputFlashMap !=  (! (attributesSnapshot !=</string></string>
 

#=================================================== ========================

  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;
    }</p>

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

 /** * 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;
        }
    }</p>

 

###========================= ==================================================== ######
 /** * 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> 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;
    }</string></p>
###### ######================================= ==========================================######== ==================================================== =====================######========================= ==================================================== ######============================================== =============================######=============== ==================================================== ========###

以上是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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具