Struts 1.3中的多个提交按钮 [英] Multiple Submit button in Struts 1.3

查看:93
本文介绍了Struts 1.3中的多个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSP中有以下代码:

I have this code in my 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>

这在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?

Like the delete action, I have edit and update. It works fine, if I give the name specifically like <html:form action="delete"> but, how to make it dynamically work for update and edit?

推荐答案

您有一个表单和多个提交按钮.问题在于,无论您在表单中有多少个提交按钮,表单都只能提交一个动作.

You have one form and multiple submit buttons. The problems is that a form can only submit to one action, no matter how many submit buttons you have inside the form.

现在想到三个解决方案:

Three solutions come to mind right now:

1..只需执行一项操作即可提交所有内容.进入Action类后,检查使用了什么按钮来提交表单并根据该按钮执行适当的处​​理.

1. Have just one action where you submit everything. Once inside the Action class check what button was used to submit the form and perform the appropriate treatment based on that.

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

ModifyAction.execute(...)方法中,应具有以下内容:

In the ModifyAction.execute(...) method have something like:

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属性.首先,将带有单击处理程序的提交"按钮更改为普通按钮:

2. Have the action attribute of the HTML form changed using JavaScript before submitting the form. You first change the submit buttons to plain ones with click handlers attached:

<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..使用您只需提供三个执行方法,分别命名为deleteeditupdate. 此处是一个示例,说明了如何执行此操作.

You just provide three execute methods properly named delete, edit and update. Here is an example that explains how you might do it.

结论:对于数字1,我​​真的不喜欢那些丑陋的测试……对于数字2,我真的不喜欢你必须采取行动表格使用JavaScript,因此我个人会进入数字3 .

In conclusion: For number 1, I don't really like those ugly tests.... for number 2, I don't really like the fact that you have to play with the action form using JavaScript, so I would personally go for number 3.

这篇关于Struts 1.3中的多个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆