Struts 1.3 中的多个提交按钮

发布于 2024-12-04 06:32:32 字数 824 浏览 1 评论 0原文

我的 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 操作一样,我有 edit 和 <代码>更新。如果我专门指定名称,如 ,它工作得很好,但是,如何使其动态地适用于 update编辑?

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>

And this in the struts-config.xml file:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请恋爱 2024-12-11 06:32:32

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

现在我想到了三种解决方案:

1. 只需执行一项操作即可提交所有内容。进入 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 表单的操作属性。首先将提交按钮更改为附加了单击处理程序的普通按钮:

<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. 使用 DispatchAction (一个类似于第 1 点的 Action 类),但无需测试什么按钮被点击,因为它是由 DispatchAction 处理的。

您只需提供三个正确命名为 deleteeditupdate 的执行方法。 这是一个示例,说明了如何执行此操作

结论:对于第一点,我真的不喜欢那些丑陋的测试...对于第二点,我真的不喜欢你必须使用 JavaScript 来处理动作表单,所以我个人会选择第三种

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

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

With the handler:

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

3. Use a DispatchAction (one Action class similar to point 1) but without the need to test for what button was clicked since that's treated by the DispatchAction.

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

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.

橘亓 2024-12-11 06:32:32

还有另一种更简单的方法可以执行此操作,如下所示:

在 ActionForm currentTimeForm 中,添加一个 String 属性(示例:buttonClicked)。

在 jsp 中,在每个 html:submit 标记中添加此属性。

现在,在操作执行方法中,只需检查此属性的值,即。

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

等等...

There is another simpler way to do this as follows:

In the ActionForm currentTimeForm, add a String property (example: buttonClicked).

In the jsp, in each of the html:submit tags add this property.

Now in the action execute method simply check the value of this property, ie.

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

and so on...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文