<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    推薦淘寶秋冬男裝熱賣網(wǎng)店

    追求無止境

    我的程序人生
    隨筆 - 31, 文章 - 2, 評論 - 20, 引用 - 0
    數(shù)據(jù)加載中……

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

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

    Spring MVC框架中的Controller對請求進行處理:所有的Controller都實現(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可以使空,表明請求都是該函數(shù)中處理完成了,不需要modeland來進行渲染。
     
    在繼續(xù)之前先介紹一個有用的工具類:WebUtils。用這個可以簡化session,request的處理。具體的內(nèi)容可以參考文檔。
     
    Controller的第一個實現(xiàn)是:AbstractController。他是一個Abstract類,除了實現(xiàn)了Controller接口,它還繼承了WebContentGenerator。
     
    WebContentGenerator的作用是什么?參考文檔可以發(fā)現(xiàn),該類主要對Cache和Session進行管理。
     
    cacheSeconds 指定內(nèi)容緩存的時間,默認為1
    requireSession 是否需要會話,默認支持
    supportedMethods 支持的方法,默認是GET\post\Head
    useCacheControlHeader 指定是否使用http1.1的cache控制頭信息,默認使用
    useCacheControlNoStore 指定是否設(shè)置http1.1的cache控制頭信息為no-store。默認使用
    useExpiresHeader 指定是否使用http1.0的expire頭信息。默認使用
    用戶可以對這些參數(shù)進行測試,cache和expire信息涉及到了http協(xié)議信息,更多信息可以參考http協(xié)議文檔。這里不再說明。
     
    再看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的目的就是使用用于進行的配置來對request進行預(yù)處理和準備。
    他會檢查支持的方法,和會話,然后應(yīng)用cache設(shè)置。
    如果需要session同步,就進行同步處理。session同步應(yīng)用于有session的情況下。如果沒有session,session同步是沒有用的。
    AbstractController會調(diào)用handleRequestInternal方法進行處理,繼承AbstractController的類需要實現(xiàn)該方法。
    下面我們再看看AbstractUrlViewController 的代碼實現(xiàn)和文檔,先看handleRequestInternal的實現(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是一個抽象函數(shù),需要子類實現(xiàn)。lookupPath就是我們請求的URL中的一部分。如我們使用UrlFilenameViewController來進行如下的配置:
    <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對/index.do解析成index,然后再通過viewResolver對index進行擴展為/jsp/index.jsp。從而找到該頁面。
    可以看到這個類的主要是用于對url進行解析,然后轉(zhuǎn)到合適的頁面上,而在轉(zhuǎn)到這個頁面之前不需要進行特別的處理。
    明白了該類的作用自然也就知道了UrlFilenameViewController的作用。這里不再進行詳細分析。
     
     

    posted on 2009-11-26 09:35 追求無止境 閱讀(4164) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲色大成网站www尤物| 亚洲精品无码乱码成人| 亚洲欧美日韩中文无线码| 在线永久免费的视频草莓| 亚洲伊人久久大香线焦| 19禁啪啪无遮挡免费网站| 91亚洲精品视频| 国产又黄又爽又猛免费app| 亚洲一区二区三区播放在线| 最近最新MV在线观看免费高清| 亚洲另类自拍丝袜第1页| 好先生在线观看免费播放| 亚洲一久久久久久久久| 国产无遮挡吃胸膜奶免费看视频 | 中文亚洲成a人片在线观看| 国产99精品一区二区三区免费| 国产亚洲美女精品久久久2020| 中文字幕不卡免费视频| 久久亚洲AV成人出白浆无码国产| 美女内射毛片在线看免费人动物| 亚洲综合校园春色| 免费国产真实迷j在线观看| 国产精品hd免费观看| 亚洲激情在线视频| 免费做爰猛烈吃奶摸视频在线观看| 亚洲精华国产精华精华液好用| 又粗又大又长又爽免费视频| 九九99热免费最新版| 亚洲欧洲日韩综合| 国产一区二区三区免费视频| 精品一区二区三区免费观看| 亚洲日产2021三区在线| 国产一级理论免费版| 中国国产高清免费av片| 亚洲国产成+人+综合| 亚洲成A人片在线观看中文| 中文字幕日本人妻久久久免费| 亚洲av片不卡无码久久| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲va在线va天堂成人| 久久久精品国产亚洲成人满18免费网站 |