Struts DispatchAction,表单带method参数选择执行方法

来源:百度文库 编辑:神马文学网 时间:2024/04/28 00:27:25
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类我想用DispatchAction来做一个从表单添加,修改,删除记录的功能

MyDispatchAction中有add,alert,delete等方法,问题是如何让表单提交的时候加上参数呢?
比如:按下add button实现 MyDispathAction?method=add这样的一次提交?
1.使用 DispatchAction
DispatchAction是Struts包含的另一个能大量节省开发时间的Action类。与其它Action类仅提供单个execute()方法实现单个业务不同,DispatchAction允许你在单个Action类中编写多个与业务相关的方法。这样可以减少Action类的数量,并且把相关的业务方法集合在一起使得维护起来更容易。
要使用DispatchAction的功能,需要自己创建一个类,通过继承抽象的DispatchAction得到。对每个要提供的业务方法必须有特定的方法signature。例如,我们想要提供一个方法来实现对购物车添加商品清单,创建了一个类ShoppingCartDispatchAction提供以下的方法:
那么,这个类很可能还需要一个deleteItem()方法从客户的购物车中删除商品清单,还有clearCart()方法清除购物车等等。这时我们就可以把这些方法集合在单个Action类,不用为每个方法都提供一个Action类。
在调用ShoppingCartDispatchAction里的某个方法时,只需在URL中提供方法名作为参数值。就是说,调用addItem()方ǖ?URL看起来可能类似于:
http://myhost/storefront/action/cart?method=addItem
其中method参数指定ShoppingCartDispatchAction中要调用的方法。参数的名称可以任意配置,这里使用的"method"只是一个例子。参数的名称可以在Struts配置文件中自行设定。
2.使用 LookupDispatchAction
org.apache.struts.actions.LookupDispatchAction类:
通常LookupDispatchAction主要应用于在一个表单中有多个提交按钮,而这些按钮又有一个共同的名字的场合,这些按钮的名字和具体的ActionMapping的parameter属性相对应。
配置LookupDispatchAction时,应该在元素中,把parameter属性设置为"action",使它和标签的property属性相一致。
做一个隐藏变量就可以了
然后用JS判断一下





onclick="SetAction'updateDsp');">
onclick="SetAction'addDsp');">



定义一个hidden的元素,JS控制提交的参数
用这个类用多了就比较乱了,form的表单映射会烦死你。
同样用楼上老兄的方法就可以,这个用在同个页面上多个提交时候使用比较合适。
如果仅仅是一个页面一个提交的话,还有个更简单的方法。

加个参数不就行了么~在action里面验证这个参数,如果有form的话加个getset方法同样验证加个注释也可以解决问题。
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:
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
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:
button.add=Add Record
button.delete=Delete Record
And your JSP would have the following format for submit buttons:








Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:
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 IOException, ServletException {
// do add
return mapping.findForward("success");
}
public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}
Notes - If duplicate values exist for the keys returned by
getKeys, only the first one found will be returned. If no corresponding key
is found then an exception will be thrown. You can override the
method unspecified to provide a custom handler. If the submit
was cancelled (a html:cancel button was pressed), the custom
handler cancelled will be used instead.