您有一个表单和多个提交按钮。问题在于,无论表单中有多少个提交按钮,表单都只能提交一个操作。
现在想到三个解决方案:
1.只需执行一项操作即可提交所有内容。进入 Action 类后,检查使用哪个按钮来提交表单并根据该按钮执行适当的处理。
<html:form action="modify">
..
..
<html:submit value="delete"/>
<html:submit value="edit" />
<html:sumit value="update" >
</html:form>
In the 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 表单的操作属性。首先将提交按钮更改为带有附加点击处理程序的普通按钮:
<html:form action="whatever">
..
..
<html:button value="delete" onclick="submitTheForm('delete.do')" />
<html:button value="edit" onclick="submitTheForm('edit.do')" />
<html:button value="update" onclick="submitTheForm('update.do')" />
</html:form>
使用处理程序:
function submitTheForm(theNewAction) {
var theForm = ... // get your form here, normally: document.forms[0]
theForm.action = theNewAction;
theForm.submit();
}
3. Use a DispatchAction http://struts.apache.org/1.x/struts-extras/apidocs/org/apache/struts/actions/DispatchAction.html(一个 Action 类类似于第 1 点),但无需测试单击了哪个按钮,因为该按钮由DispatchAction
.
您只需提供三个正确命名的执行方法delete
, edit
and update
. 这是一个示例,说明了您可以如何做到这一点 http://www.vaannila.com/struts/struts-example/struts-dispatch-action-example-1.html.
综上所述:对于第一点,我真的不喜欢那些丑陋的测试......对于第二点,我真的不喜欢你必须使用 JavaScript 来玩操作表单,所以我个人会选3号.