具有两个按钮的 Struts2 表单 — 操作未正确处理
我尝试在论坛中搜索这个问题的答案,但没有成功。我有一个带有两个按钮的 Struts2 表单,我希望操作类根据按下的按钮执行不同的操作。那并没有发生。谁能帮我解决这个问题吗?这是我的表格,后面是动作类。
<s:form action="ApproveBulletin" method="post">
<table>
<tr>
<td colspan="2"><b>From:</b> <s:property value="name" /></td>
</tr>
<tr>
<td colspan="2"><b>Subject:</b> <s:property value="subject" /></td>
</tr>
<tr>
<td colspan="2"><b>Date:</b> <s:property value="date" /> <br>
</td>
</tr>
<tr>
<td colspan="2"><s:property value="note" />
<s:hidden name="id" value="%{id}" /></td>
</tr>
<tr>
<td><s:submit type="button" name="approve" value="approve" label="Approve" /></td>
<td><s:submit type="button" name="deny" value="deny" label="Deny" /></td>
</tr>
</table>
<br />
</s:form>
public String execute() {
BulletinDAO bulletinDAOInstance = new BulletinDAO();
if ("Approve".equals(buttonName)) {
if (bulletinDAOInstance.approveBulletin(id) == true) {
return "redirect";
}
}
if ("Deny".equals(buttonName)) {
if (bulletinDAOInstance.denyBulletin(id) == true) {
return "redirect";
}
}
return "failure";
}
I've tried searching the forums for the answer to this, and I've been unsuccessful. I have a Struts2 form with two buttons, and I want the action class to perform a different action depending on which button was pressed. That's not happening. Can anyone help me with this? Here is my form, followed by the action class.
<s:form action="ApproveBulletin" method="post">
<table>
<tr>
<td colspan="2"><b>From:</b> <s:property value="name" /></td>
</tr>
<tr>
<td colspan="2"><b>Subject:</b> <s:property value="subject" /></td>
</tr>
<tr>
<td colspan="2"><b>Date:</b> <s:property value="date" /> <br>
</td>
</tr>
<tr>
<td colspan="2"><s:property value="note" />
<s:hidden name="id" value="%{id}" /></td>
</tr>
<tr>
<td><s:submit type="button" name="approve" value="approve" label="Approve" /></td>
<td><s:submit type="button" name="deny" value="deny" label="Deny" /></td>
</tr>
</table>
<br />
</s:form>
public String execute() {
BulletinDAO bulletinDAOInstance = new BulletinDAO();
if ("Approve".equals(buttonName)) {
if (bulletinDAOInstance.approveBulletin(id) == true) {
return "redirect";
}
}
if ("Deny".equals(buttonName)) {
if (bulletinDAOInstance.denyBulletin(id) == true) {
return "redirect";
}
}
return "failure";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该在“struts.xml”文件中为这两个按钮设置两个不同的操作。
在jsp中,可以通过javascript设置对应按钮的form action。
在 Jsp 上:
对于“批准按钮”,将操作设置为“ApproveAction”,对于“拒绝按钮”,通过 javascript 将操作设置为“DenyAction”
示例 Struts.xml
在“YourActionClass”中,然后您可以用两种不同的方法编写代码,即“批准”和“拒绝”。
I think you should have 2 different actions in the "struts.xml" file for the 2 buttons.
In jsp, you can set the form action for the corresponding button through javascript.
On Jsp:
For "Approve Button" set action as "ApproveAction", for "Deny Button" set action as "DenyAction" through javascript
Sample Struts.xml
In "YourActionClass", you can then write your code in two different method i.e "approve" and "deny".
这是我想出的解决方案的操作代码。
Here is the action code of the solution I came up with.