posts - 15,  comments - 8,  trackbacks - 0
          java.lang.Object

              org.apache.struts.action.Action

                  org.apache.struts.actions.DispatchAction

                      org.apache.struts.actions.LookupDispatchAction(Struts1.
          1)

                      org.apache.struts.actions.EventDispatchAction(Struts1.
          2.9)

                      org.apache.struts.actions.MappingDispatchAction(Struts1.
          2)


          DispatchAction
          public abstract class DispatchAction extends Action


          這是一個(gè)抽象的Action,它會根據(jù)request 中的parameter來執(zhí)行相應(yīng)的方法。通個(gè)這個(gè)Action類可以將不同的Action集中到一個(gè)Action文件中來。

          struts-config.xml:


          <action path="/subscription" type="org.example.SubscriptionAction" name="subscriptionForm"  scope="request" input="/subscription.jsp" parameter="method"/>

          在Action中要有相應(yīng)的方法:


          public class SubscriptionAction extends DispatchAction {
             
              
          public ActionForward delete(ActionMapping mapping, ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) 
          throws Exception {}
              
              
          public ActionForward insert(ActionMapping mapping, ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) 
          throws Exception {}
              
              
          public ActionForward update(ActionMapping mapping, ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) 
          throws Exception {}
              
          }

          然后可以通過這樣的方法來訪問你的程序:

          http://localhost:8080/myapp/subscription.do?method=delete

          http://localhost:8080/myapp/subscription.do?method=insert

          http://localhost:8080/myapp/subscription.do?method=update

          如果parameter中參數(shù)為空,則調(diào)用Action中的unspecified方法



          LookupDispatchAction
          public abstract class LookupDispatchAction extends DispatchAction


          通過這個(gè)Action抽象類繼承DispatchAction,它的相應(yīng)方法的執(zhí)行由ActionMapping中parameter屬性決定。每個(gè)動(dòng)作實(shí)際上就是<html:submit>標(biāo)簽的property屬性值。它適合在一個(gè)form中有很多按鈕,按不同的按鈕則執(zhí)行不同的操作。

          struts-config.xml:


          <action path="/subscription" type="org.example.SubscriptionAction" name="subscriptionForm" scope="request"
          input
          ="/subscription.jsp" parameter="method"/>

          ApplicationResources.properties:

          button.add=Add Record
          button.delete=Delete Record

          JSP:

          <%@ page pageEncoding="GBK"%>
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
          <html>
            
          <head>
              
          <title>多提交演示</title>
            
          </head>
            
          <body>
              
          <html:form action="subscription"> 
                
          <html:submit property="method"> 
                  
          <bean:message key="button.add"/> 
                
          </html:submit> 
                
          <html:submit property="method"> 
                  
          <bean:message key="button.delete"/> 
                
          </html:submit> 
              
          </html:form>
            
          </body> 
          </html>

          在Action中必須實(shí)現(xiàn)getKeyMethodMap方法:

          public class SubscriptionAction extends LookupDispatchAction {

              
          protected Map getKeyMethodMap() {
                  Map map 
          = new HashMap();
                  map.put(
          "button.add""add");
                  map.put(
          "button.delete""delete");
                  
          return map;
              }

              
          public ActionForward add(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response) 
          throws Exception {}

              
          public ActionForward delete(ActionMapping mapping,
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) 
          throws Exception {}

          }


          EventDispatchAction
          public class EventDispatchAction extends DispatchAction


          通過這個(gè)Action抽象類繼承DispatchAction,它的相應(yīng)方法的執(zhí)行由ActionMapping中parameter屬性指定多個(gè)動(dòng)作,中間用逗號(,)分隔。每個(gè)動(dòng)作實(shí)際上就是<html:submit>標(biāo)簽的property屬性值。它適合在一個(gè)form中有很多按鈕,按不同的按鈕則執(zhí)行不同的操作。

          struts-config.xml:

          (parameter中的"recalc=recalculate"意思為<html:submit>標(biāo)簽的property屬性值為recalc調(diào)用recalculate方法,出于安全考慮能夠隱藏后臺的業(yè)務(wù)方法名。"defaule=save"這是可選的,沒有配置property屬性的<html:submit>標(biāo)簽將調(diào)用defaule中設(shè)置的方法名,如果defaule沒有指定任何參數(shù),則調(diào)用Action中的unspecified方法)


          <action path="/subscription" type="org.example.SubscriptionAction" name="subscriptionForm" scope="request"
          input
          ="/subscription.jsp" parameter="save,back,recalc=recalculate,default=save"/>


          JSP:

          <%@ page pageEncoding="GBK"%>
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
          <html>
            
          <head>
              
          <title>多提交演示</title>
            
          </head>
            
          <body>           
              
          <html:form action="subscription">
                
          <html:submit property="save" value="保存"/>
                
          <html:submit property="back" value="后退"/>
                
          <html:submit property="recalc" value="重新計(jì)算"/>
              
          </html:form>  
            
          </body> 
          </html>

          在Action中要有相應(yīng)的方法:


          public class SubscriptionAction extends LookupDispatchAction {

              
          public ActionForward save(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) 
          throws Exception {}

              
          public ActionForward back(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) 
          throws Exception {}

              
          public ActionForward recalculate(ActionMapping mapping,
                                               ActionForm form,
                                               HttpServletRequest request,
                                               HttpServletResponse response) 
          throws Exception {}

          }


          MappingDispatchAction
          public class MappingDispatchAction extends DispatchAction


          它的相應(yīng)方法的執(zhí)行由ActionMapping中parameter名決定,注意這里和LookupDispatchAction不同,LookupDispatchAction的相應(yīng)方法的執(zhí)行由ActionMapping中parameter屬性決定。

          struts-config.xml:


          <action path="/createSubscription" type="org.example.SubscriptionAction" parameter="create"> 
                
          <forward name="success" path="/createSubscription.jsp"/> 
          </action>
          <action path="/editSubscription" type="org.example.SubscriptionAction" parameter="edit"> 
                
          <forward name="success" path="/editSubscription.jsp"/> 
          </action>
          <action path="/saveSubscription" type="org.example.SubscriptionAction" parameter="save"
          name
          ="subscriptionForm" validate="true" input="/editSubscription.jsp" scope="request"> 
                
          <forward name="success" path="/savedSubscription.jsp"/> 
          </action>
          <action path="/deleteSubscription" type="org.example.SubscriptionAction" name="subscriptionForm"
          scope
          ="request" input="/subscription.jsp" parameter="delete"> 
                
          <forward name="success" path="/deletedSubscription.jsp"/> 
          </action>
          <action path="/listSubscriptions" type="org.example.SubscriptionAction" parameter="list"> 
                
          <forward name="success" path="/subscriptionList.jsp"/>
          </action>

          在Action中要有相應(yīng)的方法:

          public class SubscriptionAction extends MappingDispatchAction {

              
          public ActionForward create(ActionMapping mapping, ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) 
          throws Exception {}


              
          public ActionForward edit(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) 
          throws Exception {}


              
          public ActionForward save(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) 
          throws Exception {}


              
          public ActionForward delete(ActionMapping mapping, ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response) 
          throws Exception {}


              
          public ActionForward list(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) 
          throws Exception {}

          }

          然后可以通過這樣的方法來訪問你的程序

          http://localhost:8080/myapp/create.do

          http://localhost:8080/myapp/edit.do

          http://localhost:8080/myapp/save.do

          http://localhost:8080/myapp/delete.do

          http://localhost:8080/myapp/list.do

          如果parameter中參數(shù)為空,則調(diào)用Action中的unspecified方法

          posted on 2009-02-25 17:42 lvq810 閱讀(952) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 伊吾县| 砚山县| 沽源县| 乌恰县| 固原市| 浦城县| 章丘市| 荔浦县| 新津县| 屏南县| 大渡口区| 富顺县| 秦安县| 阳东县| 钟祥市| 宜川县| 台前县| 滦平县| 连南| 公主岭市| 平塘县| 介休市| 鄱阳县| 鲁山县| 肃北| 武安市| 天津市| 石嘴山市| 阳山县| 宝清县| 盘锦市| 泽库县| 正宁县| 治县。| 柳州市| 山西省| 民县| 阿合奇县| 正镶白旗| 丰顺县| 临城县|