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

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

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

    框架-struts2

    一:struts2概要
        以WebWork優秀設計思想為核心,吸收了struts1的部分優點。

    二:struts2詳解
        主要就是詳解struts2與struts1之間的區別,以及為什么要采用webwork重新設計新框架,以及吸收了struts1的哪部分優點。
        首先將區別:
    •     最大的區別是與servlet成功解耦,不在依賴容器來初始化HttpServletRequest和HttpServletResponse
        struts1里依賴的核心控制器為ActionServlet而struts2依賴ServletDispatcher,一個是servlet一個是filter,正是采用了filter才不至于和servlet耦合,所有的數據 都是通過攔截器來實現,如下圖顯示:
        

    •     web層表現層的豐富,struts2已經可以使用jsp、velocity、freemarker
    •     線程模式方面:struts1的action是單例模式而且必須是線程安全或同步的,是struts2的action對每一個請求都產生一個新的實例,因此沒有線程安全問       題。
    •     封裝請求參數:是struts1采用ActionForm封裝請求參數,都必須繼承ActionForm基類,而struts2通過bean的屬性封裝,大大降低了耦合。
    •     類型轉換:struts1封裝的ActionForm都是String類型,采用Commons- Beanutils進行類型轉換,每個類一個轉換器;struts2采用OGNL進行類型轉       換,支持基本數據類型和封裝類型的自動轉換。
    •     數據校驗:struts1在ActionForm中重寫validate方法;struts2直接重寫validate方法,直接在action里面重寫即可,不需要繼承任何基類,實際的調用順序是,validate()-->execute(),會在執行execute之前調用validate,也支持xwork校驗框架來校驗。
        其次,講一下為什么要采用webwork來重新設計struts2
              
    首先的從核心控制器談起,struts2的FilterDispatcher,這里我們知道是一個filter而不是一個servlet,講到這里很多人還不是很清楚web.xml里它們之間的聯系,先簡短講一下它們的加載順序,context-param(應用范圍的初始化參數)-->listener(監聽應用端的任何修改通知)-->filter(過濾)-->servlet。
        filter在執行servlet之間就以及調用了,所以才有可能解脫完全依賴servlet的局面,那我們來看看這個filter做了什么事情:
        /**
         * Process an action or handle a request a static resource.
         * <p/>
         * The filter tries to match the request to an action mapping.
         * If mapping is found, the action processes is delegated to the dispatcher's serviceAction method.
         * If action processing fails, doFilter will try to create an error page via the dispatcher.
         * <p/>
         * Otherwise, if the request is for a static resource,
         * the resource is copied directly to the response, with the appropriate caching headers set.
         * <p/>
         * If the request does not match an action mapping, or a static resource page,
         * then it passes through.
         *
         * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
         
    */
        
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

            HttpServletRequest request 
    = (HttpServletRequest) req;
            HttpServletResponse response 
    = (HttpServletResponse) res;
            ServletContext servletContext 
    = getServletContext();

            String timerKey 
    = "FilterDispatcher_doFilter: ";
            
    try {

                
    // FIXME: this should be refactored better to not duplicate work with the action invocation
                ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
                ActionContext ctx 
    = new ActionContext(stack.getContext());
                ActionContext.setContext(ctx);

                UtilTimerStack.push(timerKey);
                request 
    = prepareDispatcherAndWrapRequest(request, response);
                ActionMapping mapping;
                
    try {
                    mapping 
    = actionMapper.getMapping(request, dispatcher.getConfigurationManager());
                } 
    catch (Exception ex) {
                    log.error(
    "error getting ActionMapping", ex);
                    dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
                    
    return;
                }

                
    if (mapping == null) {
                    
    // there is no action in this request, should we look for a static resource?
                    String resourcePath = RequestUtils.getServletPath(request);

                    
    if ("".equals(resourcePath) && null != request.getPathInfo()) {
                        resourcePath 
    = request.getPathInfo();
                    }

                    
    if (staticResourceLoader.canHandle(resourcePath)) {
                        staticResourceLoader.findStaticResource(resourcePath, request, response);
                    } 
    else {
                        
    // this is a normal request, let it pass through
                        chain.doFilter(request, response);
                    }
                    
    // The framework did its job here
                    return;
                }

                dispatcher.serviceAction(request, response, servletContext, mapping);
    //過濾用戶請求,攔截器執行,把對應的action請求轉到業務action執行        } 

    finally {
                
    try {
                    ActionContextCleanUp.cleanUp(req);
                } 
    finally {
                    UtilTimerStack.pop(timerKey);
                }
            }
        }
        對應的action參數由攔截器獲取。
        解耦servlet是struts2采用webwork思路的最重要的一個原因,也迎合了整個技術的一個發展方向,解耦一直貫穿于整個框架。
            

    posted on 2012-02-27 17:07 陳睿 閱讀(1660) 評論(2)  編輯  收藏 所屬分類: 框架

    評論

    # re: 框架-struts2 2012-02-27 22:28 todayx.org

    不錯

    歷史上的今天
    回顧歷史的今天,歷史就像生活的一面鏡子;可以了解歷史的這一天發生的事件;借古可以鑒今;歷史是不能忘記的.要記住歷史的每一天
    http://www.todayx.org/  回復  更多評論   

    # re: 框架-struts2 2012-02-28 09:13 tb

    講解的不錯   回復  更多評論   


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


    網站導航:
     

    導航

    <2012年2月>
    2930311234
    567891011
    12131415161718
    19202122232425
    26272829123
    45678910

    統計

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 最近免费中文字幕MV在线视频3| 亚洲精品色在线网站| 中文字幕高清免费不卡视频| 国产真实伦在线视频免费观看| 亚洲自偷自偷在线成人网站传媒| 2019中文字幕免费电影在线播放| 久久久久亚洲AV片无码| 久久一本岛在免费线观看2020| 亚洲av无码一区二区乱子伦as| 国产一区二区免费| 亚洲天堂久久精品| 人禽杂交18禁网站免费| 精品久久久久亚洲| 亚洲一区二区三区在线视频| 91免费福利视频| 亚洲天天做日日做天天看| 久久久高清免费视频| 亚洲av日韩综合一区二区三区| 亚洲成a人无码av波多野按摩| 精品国产福利尤物免费| 亚洲AV无码成人精品区天堂| 51精品视频免费国产专区| 亚洲AV综合色区无码二区偷拍| 免费毛片在线视频| 一级**爱片免费视频| 亚洲午夜精品久久久久久人妖 | 国产大片免费天天看| 亚洲国产精品久久久久婷婷老年| 222www在线观看免费| 亚洲AV无码国产一区二区三区| 精品亚洲一区二区三区在线播放| 国产成人免费视频| 亚洲日本va一区二区三区| 久久99亚洲综合精品首页| 亚洲免费观看在线视频| 亚洲日韩在线中文字幕综合| 亚洲AV无码久久精品成人| 在线播放免费播放av片| 久久久精品视频免费观看| 亚洲av成人一区二区三区| 亚洲一区二区三区在线视频|