隨筆 - 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 扭曲的鉛筆 閱讀(647) 評論(0)  編輯  收藏 所屬分類: AppFuse

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


          網站導航:
           
          主站蜘蛛池模板: 江都市| 买车| 方山县| 白城市| 阿巴嘎旗| 宕昌县| 莱西市| 三亚市| 罗山县| 荆门市| 大名县| 乌什县| 内江市| 东明县| 搜索| 四会市| 扎兰屯市| 南和县| 潍坊市| 庆安县| 鄂州市| 囊谦县| 洛浦县| 全南县| 彭州市| 新源县| 斗六市| 牡丹江市| 锦州市| 驻马店市| 增城市| 拉萨市| 德昌县| 隆昌县| 沾化县| 垣曲县| 旬邑县| 神池县| 神农架林区| 莱西市| 托克托县|