使用LookupDispatchAction- -

  1.   
  2. org.apache.struts.actions.LookupDispatchAction类别是DispatchAction类别的子类,与DispatchAction类似    
  3.   
  4. 的是,它透过请求上的参数来决定该执行哪一个方法,不过LookupDispatchAction多了查询讯息资源文件的功能    
  5.   
  6. ,LookupDispatchAction的用处之后,就是当一个窗体中包括两个以上同名的送出按钮时,可以透过查询讯息资    
  7.   
  8. 源文件来确定相对应的动作。    
  9.   
  10. 直接以实例来说明,在继承LookupDispatchAction之后,您要重新定义getKeyMethodMap()方法,并定义好自己的    
  11.   
  12. 相关处理方法,例如:    
  13.   
  14. 代码:    
  15.   
  16. package onlyfun.caterpillar;    
  17.                                                                                     
  18. import javax.servlet.http.*;    
  19. import org.apache.struts.action.*;    
  20. import org.apache.struts.actions.*;    
  21.                                                                                     
  22. public class ShoppingAction extends LookupDispatchAction {    
  23.     protected Map getKeyMethodMap() {    
  24.         Map map = new HashMap();    
  25.         map.put("button.continue", "continue");    
  26.         map.put("button.checkout", "checkout");    
  27.         return map;    
  28.     }    
  29.   
  30.     public ActionForward continue(ActionMapping mapping,    
  31.                               ActionForm form,    
  32.                               HttpServletRequest request,    
  33.                               HttpServletResponse response)    
  34.     throws Exception {    
  35.         // ......    
  36.     }    
  37.   
  38.      public ActionForward checkout(ActionMapping mapping,    
  39.                               ActionForm form,    
  40.                               HttpServletRequest request,    
  41.                               HttpServletResponse response)    
  42.     throws Exception {    
  43.         // ......    
  44.     }                                                                  
  45. }    
  46.   
  47.   
  48.   
  49.   
  50. 假设讯息资源文件中包括以下的讯息:    
  51.   
  52. 代码:    
  53.   
  54. button.continue=Continue    
  55. button.checkout=Checkout    
  56.   
  57.   
  58.   
  59.   
  60. 为了要使用LookupDispatchAction,我们如同DispatchAction一样在struts-config.xml中定义请求参数中该有的    
  61.   
  62. 名称:    
  63.   
  64. 代码:    
  65.   
  66.    <action path="/shopping"    
  67.            type="onlyfun.caterpillar.ShoppingAction"    
  68.            parameter="method"    
  69.            name="cartForm"/>    
  70.   
  71.   
  72.   
  73.   
  74. 现在假设您的窗体页面包括以下的内容:    
  75.   
  76. 代码:    
  77.   
  78.     <html:form action="/shopping">    
  79.         <html:submit property="method">    
  80.             <bean:message key="button.continue"/>    
  81.         html:submit>    
  82.         <html:submit property="method">    
  83.             <bean:message key="button.checkout"/>    
  84.         html:submit>    
  85.     html:form>    
  86.   
  87.   
  88.   
  89.   
  90. 这些Struts自订卷标在执行后会产生以下的内容:    
  91.   
  92. 代码:    
  93.   
  94. <form name="cartForm" method="post" action="/HelloStruts/shopping.do">    
  95.     <input type="submit" name="method" value="Continue"/>    
  96.     <input type="submit" name="method" value="Checkout"/>    
  97. form>    
  98.   
  99.   
  100.   
  101.   
  102. 所以当您按下任一个按钮时,请求参数中会包括method=Continue或是method=Checkout,假设是method=Continue    
  103.   
  104. 好了,LookupDispatchAction会根据它作为value,在讯息信息文件找到对应的key,然后根据key与    
  105.   
  106. getKeyMethodMap()得知要执行的方法为continue()方法。    
  107.   
  108.   
  109.   
  110. ********************************************************    
  111. Struts DispatchAction的如何在表单带method参数提交    
  112. DispatchAction是Struts包含的另一个能大量节省开发时间的Action类我想用DispatchAction来做一个从表单添加,修改,删除记录的功能    
  113. <html:form action="/MyDispatchAction">    
  114.   
  115. MyDispatchAction中有add,alert,delete等方法,问题是如何让表单提交的时候加上参数呢?    
  116. 比如:按下add button实现 MyDispathAction?method=add这样的一次提交?    
  117.   
  118.   
  119. 1.使用 DispatchAction    
  120. DispatchAction是Struts包含的另一个能大量节省开发时间的Action类。与其它Action类仅提供单个execute()方法实现单个业务不同,DispatchAction允许你在单个Action类中编写多个与业务相关的方法。这样可以减少Action类的数量,并且把相关的业务方法集合在一起使得维护起来更容易。    
  121.   
  122. 要使用DispatchAction的功能,需要自己创建一个类,通过继承抽象的DispatchAction得到。对每个要提供的业务方法必须有特定的方法signature。例如,我们想要提供一个方法来实现对购物车添加商品清单,创建了一个类ShoppingCartDispatchAction提供以下的方法:    
  123.   
  124. 那么,这个类很可能还需要一个deleteItem()方法从客户的购物车中删除商品清单,还有clearCart()方法清除购物车等等。这时我们就可以把这些方法集合在单个Action类,不用为每个方法都提供一个Action类。    
  125.   
  126. 在调用ShoppingCartDispatchAction里的某个方法时,只需在URL中提供方法名作为参数值。就是说,调用addItem()方ǖ?URL看起来可能类似于:    
  127.   
  128. http://myhost/storefront/action/cart?method=addItem  
  129.   
  130. 其中method参数指定ShoppingCartDispatchAction中要调用的方法。参数的名称可以任意配置,这里使用的"method"只是一个例子。参数的名称可以在Struts配置文件中自行设定。    
  131.   
  132. =================================================================    
  133. 2.使用 LookupDispatchAction    
  134. org.apache.struts.actions.LookupDispatchAction类:    
  135.   
  136. 通常LookupDispatchAction主要应用于在一个表单中有多个提交按钮,而这些按钮又有一个共同的名字的场合,这些按钮的名字和具体的ActionMapping的parameter属性相对应。    
  137.   
  138. 配置LookupDispatchAction时,应该在<action>元素中,把parameter属性设置为"action",使它和<html:submit>标签的property属性相一致。    
  139.   
  140. 做一个隐藏变量就可以了    
  141. 然后用JS判断一下    
  142.   
  143. <SCRIPT LANGUAGE="javascript">    
  144. function SetAction(opType) {    
  145. document.name1.action.value = opType    
  146. document.name1.submit();    
  147. }    
  148. //-->    
  149. SCRIPT>    
  150. head>    
  151. <body>    
  152.   
  153. <html:form name="name1" action="/Del" type="XX.XX.Del">    
  154. <html:hidden property="action" />    
  155. <html:button property="update" value="UDDATE"    
  156. onclick="SetAction’updateDsp’);">html:button>    
  157. <html:button property="add" value="ADD"    
  158. onclick="SetAction’addDsp’);">html:button>    
  159. html:form>    
  160. body>    
  161. HTML>    
  162.   
  163.   
  164. 定义一个hidden的元素,JS控制提交的参数    
  165.   
  166. 用这个类用多了就比较乱了,form的表单映射会烦死你。    
  167. 同样用楼上老兄的方法就可以,这个用在同个页面上多个提交时候使用比较合适。    
  168.   
  169. 如果仅仅是一个页面一个提交的话,还有个更简单的方法。    
  170. <html:form action="/MyDispatchAction?action=add">    
  171.   
  172. 加个参数不就行了么~在action里面验证这个参数,如果有form的话加个getset方法同样验证加个注释也可以解决问题。    
  173.   
  174.   
  175. An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. To configure the use of this action in your struts-config.xml file, create an entry like this:    
  176.   
  177. <action path="/test"    
  178. type="org.example.MyAction"    
  179. name="MyForm"    
  180. scope="request"    
  181. input="/test.jsp"    
  182. parameter="method"/>    
  183. which will use the value of the request parameter named "method" to locate the corresponding key in ApplicationResources. For example, you might have the following ApplicationResources.properties:    
  184.   
  185. button.add=Add Record    
  186. button.delete=Delete Record    
  187. And your JSP would have the following format for submit buttons:    
  188.   
  189. <html:form action="/test">    
  190. <html:submit property="method">    
  191. <bean:message key="button.add"/>    
  192. html:submit>    
  193. <html:submit property="method">    
  194. <bean:message key="button.delete"/>    
  195. html:submit>    
  196. html:form>    
  197. Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:    
  198.   
  199. protected Map getKeyMethodMap() {    
  200. Map map = new HashMap();    
  201. map.put("button.add", "add");    
  202. map.put("button.delete", "delete");    
  203. return map;    
  204. }    
  205.   
  206. public ActionForward add(ActionMapping mapping,    
  207. ActionForm form,    
  208. HttpServletRequest request,    
  209. HttpServletResponse response)    
  210. throws IOException, ServletException {    
  211. // do add    
  212. return mapping.findForward("success");    
  213. }    
  214.   
  215. public ActionForward delete(ActionMapping mapping,    
  216. ActionForm form,    
  217. HttpServletRequest request,    
  218. HttpServletResponse response)    
  219. throws IOException, ServletException {    
  220. // do delete    
  221. return mapping.findForward("success");    
  222. }    
  223.   
  224.   
  225. Notes - If duplicate values exist for the keys returned by    
  226. getKeys, only the first one found will be returned. If no corresponding key    
  227. is found then an exception will be thrown. You can override the    
  228. method unspecified to provide a custom handler. If the submit    
  229. was cancelled (a html:cancel button was pressed), the custom    
  230. handler cancelled will be used instead.    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值