Struts 1.3中的多个"提交"按钮

Nag*_*ran 5 struts struts-1

我的JSP中有这个代码:

<%@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>
Run Code Online (Sandbox Code Playgroud)

这在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>
Run Code Online (Sandbox Code Playgroud)

就像delete行动一样,我有editupdate.它工作正常,如果我给的名字特别喜欢<html:form action="delete">,但是,如何使之成为工作动态updateedit

小智 19

您有一个表单和多个提交按钮.问题是表单只能提交到一个操作,无论表单中有多少提交按钮.

现在想到三种解决方案:

1.只需一个动作即可提交所有内容.进入Action类后,检查用于提交表单的按钮,并根据该按钮执行适当的处​​理.

<html:form action="modify">
  ..
  ..
  <html:submit value="delete"/>
  <html:submit value="edit" />
  <html:sumit value="update" >
</html:form>
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

2.在提交表单之前,使用JavaScript更改HTML表单的action属性.首先使用附加的点击处理程序将提交按钮更改为普通按钮:

<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>
Run Code Online (Sandbox Code Playgroud)

使用处理程序:

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

3.使用DispatchAction(一个类似于第1点的Action类)但不需要测试点击了什么按钮,因为它被处理了DispatchAction.

您只需提供三个正确命名的执行方法delete,editupdate.这是一个例子,解释了如何做到这一点.

总结:对于数字1,我​​真的不喜欢那些丑陋的测试....对于数字2,我不太喜欢你必须使用JavaScript来使用动作表单的事实,所以我个人会去3号.

  • 非常好的指导 - +1 (2认同)