DispatchAction是Struts包含的另一個(gè)能大量節省開(kāi)發(fā)時(shí)間的Action類(lèi)我想用DispatchAction來(lái)做一個(gè)從表單添加,修改,刪除記錄的功能
<html:form action="/MyDispatchAction">
MyDispatchAction中有add,alert,delete等方法,問(wèn)題是如何讓表單提交的時(shí)候加上參數呢?
比如:按下add button實(shí)現 MyDispathAction?method=add這樣的一次提交?
1.使用 DispatchAction
DispatchAction是Struts包含的另一個(gè)能大量節省開(kāi)發(fā)時(shí)間的Action類(lèi)。與其它Action類(lèi)僅提供單個(gè)execute()方法實(shí)現單個(gè)業(yè)務(wù)不同,DispatchAction允許你在單個(gè)Action類(lèi)中編寫(xiě)多個(gè)與業(yè)務(wù)相關(guān)的方法。這樣可以減少Action類(lèi)的數量,并且把相關(guān)的業(yè)務(wù)方法集合在一起使得維護起來(lái)更容易。
要使用DispatchAction的功能,需要自己創(chuàng )建一個(gè)類(lèi),通過(guò)繼承抽象的DispatchAction得到。對每個(gè)要提供的業(yè)務(wù)方法必須有特定的方法signature。例如,我們想要提供一個(gè)方法來(lái)實(shí)現對購物車(chē)添加商品清單,創(chuàng )建了一個(gè)類(lèi)ShoppingCartDispatchAction提供以下的方法:
那么,這個(gè)類(lèi)很可能還需要一個(gè)deleteItem()方法從客戶(hù)的購物車(chē)中刪除商品清單,還有clearCart()方法清除購物車(chē)等等。這時(shí)我們就可以把這些方法集合在單個(gè)Action類(lèi),不用為每個(gè)方法都提供一個(gè)Action類(lèi)。
在調用ShoppingCartDispatchAction里的某個(gè)方法時(shí),只需在URL中提供方法名作為參數值。就是說(shuō),調用addItem()方ǖ?URL看起來(lái)可能類(lèi)似于:
http://myhost/storefront/action/cart?method=addItem
其中method參數指定ShoppingCartDispatchAction中要調用的方法。參數的名稱(chēng)可以任意配置,這里使用的"method"只是一個(gè)例子。參數的名稱(chēng)可以在Struts配置文件中自行設定。
2.使用 LookupDispatchAction
org.apache.struts.actions.LookupDispatchAction類(lèi):
通常LookupDispatchAction主要應用于在一個(gè)表單中有多個(gè)提交按鈕,而這些按鈕又有一個(gè)共同的名字的場(chǎng)合,這些按鈕的名字和具體的ActionMapping的parameter屬性相對應。
配置LookupDispatchAction時(shí),應該在<action>元素中,把parameter屬性設置為"action",使它和<html:submit>標簽的property屬性相一致。
做一個(gè)隱藏變量就可以了
然后用JS判斷一下
<SCRIPT LANGUAGE="JavaScript">
<!--
function SetAction(opType) {
document.name1.action.value = opType
document.name1.submit();
}
//-->
</SCRIPT>
</head>
<body>
<html:form name="name1" action="/Del" type="XX.XX.Del">
<html:hidden property="action" />
<html:button property="update" value="UDDATE"
onclick="SetAction'updateDsp');"></html:button>
<html:button property="add" value="ADD"
onclick="SetAction'addDsp');"></html:button>
</html:form>
</body>
</HTML>
定義一個(gè)hidden的元素,JS控制提交的參數
用這個(gè)類(lèi)用多了就比較亂了,form的表單映射會(huì )煩死你。
同樣用樓上老兄的方法就可以,這個(gè)用在同個(gè)頁(yè)面上多個(gè)提交時(shí)候使用比較合適。
如果僅僅是一個(gè)頁(yè)面一個(gè)提交的話(huà),還有個(gè)更簡(jiǎn)單的方法。
<html:form action="/MyDispatchAction?action=add">
加個(gè)參數不就行了么~在action里面驗證這個(gè)參數,如果有form的話(huà)加個(gè)getset方法同樣驗證加個(gè)注釋也可以解決問(wèn)題。
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:
<action path="/test"
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:
<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
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.
聯(lián)系客服