<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

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


    網站導航:
     
    主站蜘蛛池模板: 在线精品免费视频无码的| 亚洲黄色在线网站| 亚洲av午夜电影在线观看| a一级毛片免费高清在线| 全部免费毛片在线| 亚洲av日韩综合一区久热| 日本免费一区二区三区最新vr| 亚洲欧美日韩综合久久久久| 最近中文字幕mv免费高清视频7| 亚洲人成电影网站色| 国产美女无遮挡免费视频| 亚洲高清毛片一区二区| 久久久免费的精品| 亚洲国产人成网站在线电影动漫 | 成人无码区免费视频观看 | 97人伦色伦成人免费视频| 一本天堂ⅴ无码亚洲道久久| 波多野结衣久久高清免费| 狠狠热精品免费观看| 亚洲乳大丰满中文字幕| 精品熟女少妇av免费久久| 国产成人精品日本亚洲11| 四虎永久免费地址在线网站| 一级大黄美女免费播放| 久久亚洲国产视频| 无码人妻久久一区二区三区免费丨 | 国产精品成人啪精品视频免费| 亚洲国产精品VA在线观看麻豆| **一级一级毛片免费观看| 亚洲精品无码成人| 国产亚洲美女精品久久久2020 | 国产精品免费AV片在线观看| 亚洲中文字幕久久精品无码2021| 国产精品无码免费视频二三区| free哆拍拍免费永久视频 | 久久亚洲AV成人无码| 午夜神器成在线人成在线人免费| 中国在线观看免费的www| 亚洲an日韩专区在线| 亚洲国产精品成人网址天堂| 18级成人毛片免费观看|