国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Struts DispatchAction,表單帶method參數(shù)選擇執(zhí)行方法

DispatchAction是Struts包含的另一個能大量節(jié)省開發(fā)時間的Action類我想用DispatchAction來做一個從表單添加,修改,刪除記錄的功能
<html:form action="/MyDispatchAction">

MyDispatchAction中有add,alert,delete等方法,問題是如何讓表單提交的時候加上參數(shù)呢?
比如:按下add button實現(xiàn) MyDispathAction?method=add這樣的一次提交?


1.使用 DispatchAction
DispatchAction是Struts包含的另一個能大量節(jié)省開發(fā)時間的Action類。與其它Action類僅提供單個execute()方法實現(xiàn)單個業(yè)務(wù)不同,DispatchAction允許你在單個Action類中編寫多個與業(yè)務(wù)相關(guān)的方法。這樣可以減少Action類的數(shù)量,并且把相關(guān)的業(yè)務(wù)方法集合在一起使得維護起來更容易。

要使用DispatchAction的功能,需要自己創(chuàng)建一個類,通過繼承抽象的DispatchAction得到。對每個要提供的業(yè)務(wù)方法必須有特定的方法signature。例如,我們想要提供一個方法來實現(xiàn)對購物車添加商品清單,創(chuàng)建了一個類ShoppingCartDispatchAction提供以下的方法:

那么,這個類很可能還需要一個deleteItem()方法從客戶的購物車中刪除商品清單,還有clearCart()方法清除購物車等等。這時我們就可以把這些方法集合在單個Action類,不用為每個方法都提供一個Action類。

在調(diào)用ShoppingCartDispatchAction里的某個方法時,只需在URL中提供方法名作為參數(shù)值。就是說,調(diào)用addItem()方ǖ?URL看起來可能類似于:

http://myhost/storefront/action/cart?method=addItem

其中method參數(shù)指定ShoppingCartDispatchAction中要調(diào)用的方法。參數(shù)的名稱可以任意配置,這里使用的"method"只是一個例子。參數(shù)的名稱可以在Struts配置文件中自行設(shè)定。


2.使用 LookupDispatchAction
org.apache.struts.actions.LookupDispatchAction類:

通常LookupDispatchAction主要應(yīng)用于在一個表單中有多個提交按鈕,而這些按鈕又有一個共同的名字的場合,這些按鈕的名字和具體的ActionMapping的parameter屬性相對應(yīng)。

配置LookupDispatchAction時,應(yīng)該在<action>元素中,把parameter屬性設(shè)置為"action",使它和<html:submit>標簽的property屬性相一致。

做一個隱藏變量就可以了
然后用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>


定義一個hidden的元素,JS控制提交的參數(shù)

用這個類用多了就比較亂了,form的表單映射會煩死你。
同樣用樓上老兄的方法就可以,這個用在同個頁面上多個提交時候使用比較合適。

如果僅僅是一個頁面一個提交的話,還有個更簡單的方法。
<html:form action="/MyDispatchAction?action=add">

加個參數(shù)不就行了么~在action里面驗證這個參數(shù),如果有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:

<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.

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
struts中DispatchAction的使用
struts1中幾種常用的Action
CSDN技術(shù)中心 Struts心得—DispatchAction使用日記
FORM中使用onSubmit="return false"防止表單自動提交,以及subm...
Html <form>表單標簽元素 </form>
覆蓋DispatchAction中的分發(fā)方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服