读书人

应用LookupDispatchAction-

发布时间: 2012-10-24 14:15:58 作者: rapoo

使用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. <!---->
  145. function?SetAction(opType)?{? ??
  146. document.name1.action.value?=?opType? ??
  147. document.name1.submit();? ??
  148. }? ??
  149. //-->? ??
  150. SCRIPT>? ??
  151. head>? ??
  152. <body>? ??
  153. ??
  154. <html:form?name="name1"?action="/Del"?type="XX.XX.Del">? ??
  155. <html:hidden?property="action"?/>? ??
  156. <html:button?property="update"?value="UDDATE"? ??
  157. onclick="SetAction’updateDsp’);">html:button>? ??
  158. <html:button?property="add"?value="ADD"? ??
  159. onclick="SetAction’addDsp’);">html:button>? ??
  160. html:form>? ??
  161. body>? ??
  162. HTML>? ??
  163. ??
  164. ??
  165. 定义一个hidden的元素,JS控制提交的参数? ??
  166. ??
  167. 用这个类用多了就比较乱了,form的表单映射会烦死你。? ??
  168. 同样用楼上老兄的方法就可以,这个用在同个页面上多个提交时候使用比较合适。? ??
  169. ??
  170. 如果仅仅是一个页面一个提交的话,还有个更简单的方法。? ??
  171. <html:form?action="/MyDispatchAction?action=add">? ??
  172. ??
  173. 加个参数不就行了么~在action里面验证这个参数,如果有form的话加个getset方法同样验证加个注释也可以解决问题。? ??
  174. ??
  175. ??
  176. 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:? ??
  177. ??
  178. <action?path="/test"? ??
  179. type="org.example.MyAction"? ??
  180. name="MyForm"? ??
  181. scope="request"? ??
  182. input="/test.jsp"? ??
  183. parameter="method"/>? ??
  184. 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:? ??
  185. ??
  186. button.add=Add?Record? ??
  187. button.delete=Delete?Record? ??
  188. And?your?JSP?would?have?the?following?format?for?submit?buttons:? ??
  189. ??
  190. <html:form?action="/test">? ??
  191. <html:submit?property="method">? ??
  192. <bean:message?key="button.add"/>? ??
  193. html:submit>? ??
  194. <html:submit?property="method">? ??
  195. <bean:message?key="button.delete"/>? ??
  196. html:submit>? ??
  197. html:form>? ??
  198. Your?subclass?must?implement?both?getKeyMethodMap?and?the?methods?defined?in?the?map.?An?example?of?such?implementations?are:? ??
  199. ??
  200. protected?Map?getKeyMethodMap()?{? ??
  201. Map?map?=?new?HashMap();? ??
  202. map.put("button.add",?"add");? ??
  203. map.put("button.delete",?"delete");? ??
  204. return?map;? ??
  205. }? ??
  206. ??
  207. public?ActionForward?add(ActionMapping?mapping,? ??
  208. ActionForm?form,? ??
  209. HttpServletRequest?request,? ??
  210. HttpServletResponse?response)? ??
  211. throws?IOException,?ServletException?{? ??
  212. //?do?add? ??
  213. return?mapping.findForward("success");? ??
  214. }? ??
  215. ??
  216. public?ActionForward?delete(ActionMapping?mapping,? ??
  217. ActionForm?form,? ??
  218. HttpServletRequest?request,? ??
  219. HttpServletResponse?response)? ??
  220. throws?IOException,?ServletException?{? ??
  221. //?do?delete? ??
  222. return?mapping.findForward("success");? ??
  223. }? ??
  224. ??
  225. ??
  226. Notes?-?If?duplicate?values?exist?for?the?keys?returned?by? ??
  227. getKeys,?only?the?first?one?found?will?be?returned.?If?no?corresponding?key? ??
  228. is?found?then?an?exception?will?be?thrown.?You?can?override?the? ??
  229. method?unspecified?to?provide?a?custom?handler.?If?the?submit? ??
  230. was?cancelled?(a?html:cancel?button?was?pressed),?the?custom? ??
  231. handler?cancelled?will?be?used?instead.? ??

读书人网 >PowerDesigner

热点推荐