StevenBot-Saltsam

          眼睛能裝下世界,為何卻裝不下眼淚? 一只風(fēng)箏一輩子只為一根線(xiàn)冒險(xiǎn)。 那不是一場(chǎng)游戲,為何總有一根線(xiàn)牽著心懷,隱隱作疼? 那不是一段邂逅,為何飄在橋上的影子,總纏進(jìn)夢(mèng)鄉(xiāng)? 那不是一個(gè)夢(mèng)境,為何你的溫柔私語(yǔ),總是不經(jīng)意的響起?

          導(dǎo)航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          datas link

          OSChinal Sources codes Library

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          SpringMVC web框架學(xué)習(xí) Controller 分析

          SpringMVC web框架學(xué)習(xí) Controller 分析

          org.springframework.web.servlet.mvc.AbstractController (implements org.springframework.web.servlet.mvc.Controller)

          Spring MVC框架中的Controller對(duì)請(qǐng)求進(jìn)行處理:所有的Controller都實(shí)現(xiàn)接口Controller:

          public interface Controller {       /**      * Process the request and return a ModelAndView object which the DispatcherServlet      * will render. A <code>null</code> return value is not an error: It indicates that      * this object completed request processing itself, thus there is no ModelAndView      * to render.      * @param request current HTTP request      * @param response current HTTP response      * @return a ModelAndView to render, or <code>null</code> if handled directly      * @throws Exception in case of errors      */     ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;   } 上面的doc表明Controller返回的modelandview可以使空,表明請(qǐng)求都是該函數(shù)中處理完成了,不需要modeland來(lái)進(jìn)行渲染。
           
          在繼續(xù)之前先介紹一個(gè)有用的工具類(lèi):WebUtils。用這個(gè)可以簡(jiǎn)化session,request的處理。具體的內(nèi)容可以參考文檔。
           
          Controller的第一個(gè)實(shí)現(xiàn)是:AbstractController。他是一個(gè)Abstract類(lèi),除了實(shí)現(xiàn)了Controller接口,它還繼承了WebContentGenerator。
           
          WebContentGenerator的作用是什么?參考文檔可以發(fā)現(xiàn),該類(lèi)主要對(duì)Cache和Session進(jìn)行管理。
           
          cacheSeconds 指定內(nèi)容緩存的時(shí)間,默認(rèn)為1
          requireSession 是否需要會(huì)話(huà),默認(rèn)支持
          supportedMethods 支持的方法,默認(rèn)是GET\post\Head
          useCacheControlHeader 指定是否使用http1.1的cache控制頭信息,默認(rèn)使用
          useCacheControlNoStore 指定是否設(shè)置http1.1的cache控制頭信息為no-store。默認(rèn)使用
          useExpiresHeader 指定是否使用http1.0的expire頭信息。默認(rèn)使用
          用戶(hù)可以對(duì)這些參數(shù)進(jìn)行測(cè)試,cache和expire信息涉及到了http協(xié)議信息,更多信息可以參考http協(xié)議文檔。這里不再說(shuō)明。
           
          再看AbstractController的代碼:
          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
          throws Exception {

          // Delegate to WebContentGenerator for checking and preparing.
          checkAndPrepare(request, response, this instanceof LastModified);

          // Execute handleRequestInternal in synchronized block if required.
          if (this.synchronizeOnSession) {
          HttpSession session = request.getSession(false);
          if (session != null) {
          Object mutex = WebUtils.getSessionMutex(session);
          synchronized (mutex) {
          return handleRequestInternal(request, response);
          }
          }
          }
          return handleRequestInternal(request, response);
          }
          checkandPrepare的目的就是使用用于進(jìn)行的配置來(lái)對(duì)request進(jìn)行預(yù)處理和準(zhǔn)備。
          他會(huì)檢查支持的方法,和會(huì)話(huà),然后應(yīng)用cache設(shè)置。
          如果需要session同步,就進(jìn)行同步處理。session同步應(yīng)用于有session的情況下。如果沒(méi)有session,session同步是沒(méi)有用的。
          AbstractController會(huì)調(diào)用handleRequestInternal方法進(jìn)行處理,繼承AbstractController的類(lèi)需要實(shí)現(xiàn)該方法。
          下面我們?cè)倏纯?strong>AbstractUrlViewController 的代碼實(shí)現(xiàn)和文檔,先看handleRequestInternal的實(shí)現(xiàn):

           
          /**
          * Retrieves the URL path to use for lookup and delegates to
          * {@link #getViewNameForRequest}.
          */
          protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
          String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
          String viewName = getViewNameForRequest(request);
          if (logger.isDebugEnabled()) {
          logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
          }
          return new ModelAndView(viewName);
          }
          可以看到,它使用了getViewNameForRequest獲取需要的viewName。而getViewNameForRequest是一個(gè)抽象函數(shù),需要子類(lèi)實(shí)現(xiàn)。lookupPath就是我們請(qǐng)求的URL中的一部分。如我們使用UrlFilenameViewController來(lái)進(jìn)行如下的配置:
          <bean name="/index.do" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>、
          09-11-25 11:56:06 - DEBUG [http-8200-1] - Returning view name 'index' for lookup path [/index.do]
          該Controller對(duì)/index.do解析成index,然后再通過(guò)viewResolver對(duì)index進(jìn)行擴(kuò)展為/jsp/index.jsp。從而找到該頁(yè)面。
          可以看到這個(gè)類(lèi)的主要是用于對(duì)url進(jìn)行解析,然后轉(zhuǎn)到合適的頁(yè)面上,而在轉(zhuǎn)到這個(gè)頁(yè)面之前不需要進(jìn)行特別的處理。
          明白了該類(lèi)的作用自然也就知道了UrlFilenameViewController的作用。這里不再進(jìn)行詳細(xì)分析。

          posted on 2011-10-15 09:49 Steven_bot 閱讀(723) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): spring

          主站蜘蛛池模板: 开阳县| 林甸县| 武冈市| 桐乡市| 衡山县| 民县| 稷山县| 盱眙县| 鲁甸县| 营口市| 绥中县| 开平市| 鄢陵县| 荆州市| 高邮市| 贵南县| 蓝田县| 游戏| 五家渠市| 华坪县| 长乐市| 神池县| 大化| 鹤山市| 武清区| 永顺县| 五台县| 泸西县| 右玉县| 德阳市| 昌江| 兖州市| 仁布县| 海淀区| 含山县| 梅州市| 海南省| 当雄县| 刚察县| 吉木乃县| 如皋市|