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

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

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

    隨筆 - 37  文章 - 14  trackbacks - 0
    <2007年6月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    文章分類

    相關鏈接

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    在AppFuse框架中,struts action 采用了繼承DispatchAction的方法來實現一個Action里實現多種操作。
    首先看看DispatchAction原理
    DispatchAction在配置上于標準的Action稍有不同,就是要在Action配置中多一個parametr屬性,這個屬性將指導DispatchAction找到對應的方法,例如這樣配置

        <action
          
    path="/persons"

          type
    ="com.lms.webapp.action.PersonAction"
          name
    ="personForm"
          scope
    ="request"
          input
    ="mainMenu"
          parameter
    ="method"
          unknown
    ="false"
          validate
    ="false"    >
    parameter的屬性值是可以任意起的,只要你記得在傳參數的時候統一就可以了。比如我寫了一個類似這樣的Action,它繼承自DispatchAction類,包含了三個操作方法,有Add(),Update(),Delete()。當我想要調用這個Action的Update操作時,提交的URL應該類似這樣的:
    http://localhost:8080/myapp/persons.do?method=update
    在AppFuse中的應用
    在AppFuse中,有個繼承DispatchAction的BaseAction類,每個Action都繼承BaseAction,因此在執行Action的時候,都先執行BaseAction里的execute()方法達到指導要執行哪個操作。代碼如下
        public ActionForward execute(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        
    throws Exception {
            
            
    if (isCancelled(request)) {
                
    try {
                    getMethod(
    "cancel");
                    
    return dispatchMethod(mapping, form, request, response, "cancel");
                }
     catch (NoSuchMethodException n) {
                    log.warn(
    "No 'cancel' method found, returning null");
                    
    return cancelled(mapping, form, request, response);
                }

            }


            
    // Check to see if methodName indicated by request parameter
            String actionMethod = getActionMethodWithMapping(request, mapping);
            
            
    if (actionMethod != null{
                
    return dispatchMethod(mapping, form, request, response, actionMethod);
            }
     else {
                String[] rules 
    = {"edit""save""search""view"};
                
    for (int i = 0; i < rules.length; i++{
                    
    // apply the rules for automatically appending the method name
                    if (request.getServletPath().indexOf(rules[i]) > -1{
                        
    return dispatchMethod(mapping, form, request, response, rules[i]);
                    }

                }

            }

            
            
    return super.execute(mapping, form, request, response);
        }
    代碼分析:
    isCancelled(request)方法:if the current form's cancel button was pressed!進而執行cancel方法。
    getActionMethodWithMapping方法來確定所要執行的操作。在getActionMethod方法里,首先是判斷request里是否有method=methodName,如果有則返回methodName;如果沒有則判斷request里是否有包含method.methodName的parameter,有的話則返回截取"method."后面的methodName。
    如果通過getActionMethodWithMapping方法取不到methodName,則判斷servletPath里是否有包含edit....等字符,以決定執行什么操作。
    如果以上都不能取到methodName,那么將執行 return super.execute(mapping, form, request, response);通過查看DispatchAction源碼發現最終會執行
    unspecified(),而在AppFuse里的每個Action都會重寫這個方法。
        private String getActionMethodWithMapping(HttpServletRequest request, ActionMapping mapping) {
            
    return getActionMethod(request, mapping.getParameter());
        }


        
    /** 
         * Gets the method name based on the prepender passed to it.
         
    */

        
    protected String getActionMethod(HttpServletRequest request, String prepend) {
            String name 
    = null;
            
            
    // for backwards compatibility, try with no prepend first
            name = request.getParameter(prepend);
            
    if (name != null{
                
    // trim any whitespace around - this might happen on buttons
                name = name.trim();
                
    // lowercase first letter
                return name.replace(name.charAt(0), Character.toLowerCase(name.charAt(0)));
            }

            
            Enumeration e 
    = request.getParameterNames();

            
    while (e.hasMoreElements()) {
                String currentName 
    = (String) e.nextElement();

                
    if (currentName.startsWith(prepend + ".")) {
                    
    if (log.isDebugEnabled()) {
                        log.debug(
    "calling method: " + currentName);
                    }


                    String[] parameterMethodNameAndArgs 
    = StringUtils.split(currentName, ".");
                    name 
    = parameterMethodNameAndArgs[1];
                    
    break;
                }

            }

            
            
    return name;
        }
    綜上,在AppFuse里可以通過以下幾種方法來達到指定實現哪種操作:
    1:在路徑里包含edit、add、delete字段
    :http://loaclhost:8080/myapp/editPerson.html
    2:在request里包含method.methodName參數
    如:
            <html:submit styleClass="button" property="method.save" onclick="bCancel=false">
                
    <fmt:message key="button.save"/>
            
    </html:submit>
    3:執行cancel操作
            <html:cancel styleClass="button" onclick="bCancel=true">
                
    <fmt:message key="button.cancel"/>
            
    </html:cancel>
    4:都不執行以上操作時,執行unspecified()方法
        public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response)
                throws Exception {
            return search(mapping, form, request, response);
        }
    posted on 2007-06-20 16:39 扭曲的鉛筆 閱讀(645) 評論(0)  編輯  收藏 所屬分類: AppFuse

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


    網站導航:
     
    主站蜘蛛池模板: 女人隐私秘视频黄www免费| 四虎国产精品成人免费久久 | 亚洲人成电影在在线观看网色| 香蕉国产在线观看免费| 无码欧精品亚洲日韩一区夜夜嗨 | 免费在线看v网址| 亚洲成人高清在线观看| av无码免费一区二区三区| 亚洲av专区无码观看精品天堂| 无码国产精品久久一区免费 | 国产精品免费视频网站| 偷自拍亚洲视频在线观看99| 日韩精品成人亚洲专区| 一级特黄色毛片免费看| 亚洲人成精品久久久久| 97免费人妻在线视频| 亚洲国产模特在线播放| 免费看的黄色大片| 精品多毛少妇人妻AV免费久久| 亚洲精品一品区二品区三品区| 免费不卡在线观看AV| 亚洲av专区无码观看精品天堂| 国产无遮挡又黄又爽免费视频| j8又粗又长又硬又爽免费视频| 国产精品久久久亚洲| 日韩中文字幕精品免费一区| 99亚洲男女激情在线观看| 国产亚洲精品线观看动态图| 久久精品无码专区免费东京热| 中文字幕亚洲码在线| 亚洲精品一级无码鲁丝片| 免费精品无码AV片在线观看| 亚洲av日韩专区在线观看| 国产精品亚洲综合专区片高清久久久 | 国产成人亚洲精品狼色在线 | 日本一区二区在线免费观看| 亚洲国产第一页www| 韩国二级毛片免费播放| 久操免费在线观看| 亚洲AV色欲色欲WWW| 亚洲自偷精品视频自拍|