Struts 1.3 中的多个提交按钮
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个表单和多个提交按钮。问题在于,无论表单中有多少个提交按钮,表单都只能提交一个操作。
现在我想到了三种解决方案:
1. 只需执行一项操作即可提交所有内容。进入 Action 类后,检查使用哪个按钮来提交表单并根据该按钮执行适当的处理。
在
ModifyAction.execute(...)
方法中包含如下内容:2. 在提交表单之前使用 JavaScript 更改 HTML 表单的操作属性。首先将提交按钮更改为附加了单击处理程序的普通按钮:
使用处理程序:
3. 使用
DispatchAction
(一个类似于第 1 点的 Action 类),但无需测试什么按钮被点击,因为它是由DispatchAction
处理的。您只需提供三个正确命名为
delete
、edit
和update
的执行方法。 这是一个示例,说明了如何执行此操作。结论:对于第一点,我真的不喜欢那些丑陋的测试...对于第二点,我真的不喜欢你必须使用 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.
In the
ModifyAction.execute(...)
method have something like: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:
With the handler:
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 theDispatchAction
.You just provide three execute methods properly named
delete
,edit
andupdate
. 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.
还有另一种更简单的方法可以执行此操作,如下所示:
在 ActionForm currentTimeForm 中,添加一个 String 属性(示例:buttonClicked)。
在 jsp 中,在每个 html:submit 标记中添加此属性。
现在,在操作执行方法中,只需检查此属性的值,即。
等等...
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.
and so on...