2011-09-12 51 views
5

按鈕,我有這樣的代碼在我的JSP:多提交Struts中1.3

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> 
.. 
.. 
<html:form action="update" > 
    .. 
    .. 
    <html:submit value="delete" /> 
    <html:submit value="edit" /> 
    <html:sumit value="update" /> 
</html:form> 

這在struts-config.xml文件:

<action path="/delete" name="currentTimeForm" input="/viewall.jsp" type="com.action.DeleteProduct"> 
    <forward name="success" path="/viewall.jsp" /> 
    <forward name="failure" path="/viewall.jsp" /> 
</action> 

delete行動,我有editupdate。它工作正常,如果我給名稱具體如<html:form action="delete">但如何使其動態工作updateedit

回答

18

您有一個表單和多個提交按鈕。問題在於無論表單中有多少個提交按鈕,表單只能提交一個動作。

三種解決方案浮現在腦海現在:

只有一個動作,您提交的所有信息。一旦進入Action類,請檢查使用哪個按鈕提交表單並根據該表單執行適當的處​​理。

<html:form action="modify"> 
    .. 
    .. 
    <html:submit value="delete"/> 
    <html:submit value="edit" /> 
    <html:sumit value="update" > 
</html:form> 

ModifyAction.execute(...)方法有類似:

if (request.getParameter("delete") != null || request.getParameter("delete.x") != null) { 
    //... delete stuff 
} else if (request.getParameter("edit") != null || request.getParameter("edit.x") != null) { 
    //...edit stuff 
} else if (request.getParameter("update") != null || request.getParameter("update.x") != null) { 
    //... update stuff 
} 

2.在提交表單前使用JavaScript改變HTML表單的action屬性。您首先更改提交按鈕爲普通的人附有單擊處理:

隨着處理器:

function submitTheForm(theNewAction) { 
    var theForm = ... // get your form here, normally: document.forms[0] 
    theForm.action = theNewAction; 
    theForm.submit(); 
} 

使用DispatchAction(類似於一個Action類第1點),但沒有由於DispatchAction的處理,所以需要測試點擊了哪個按鈕。

您只需提供三種正確命名爲delete,editupdate的執行方法。 Here is an example that explains how you might do it

結論:對於編號1,我不太喜歡那些醜陋的測試....對於編號2,我不太喜歡這樣一個事實,即您必須使用JavaScript來處理動作表單,所以我會親自去3號

+0

非常感謝你....讓我試試... – Nageswaran

+2

真的很好的指導給你 - +1 – Naved

+0

哇。正是我需要的。謝謝一堆! – ykombinator

0

還有一個要做到這一點如下簡單的方法:

在ActionForm的currentTimeForm,添加一個字符串屬性(例如:buttonClicked)。

在jsp中,在每個html:submit標籤中添加這個屬性。

現在在動作執行方法中,只需檢查該屬性的值,即。

if(currentTimeForm.getButtonClicked().equals("delete"){ 
} 
else if((currentTimeForm.getButtonClicked().equals("edit")) 

等等...

+0

我認爲這不是這樣做的有效方式。例如,有10個表單具有雙重或三重提交按鈕,您不能繼續添加會話或請求變量或向您的bean添加更多變量。 –