Spring 3-带有2个按钮的表单,向控制器方法发送2个参数 [英] Spring 3 -- form with 2 buttons, sending 2 parameters to controller method
本文介绍了Spring 3-带有2个按钮的表单,向控制器方法发送2个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有2个参数的Spring 3 MVC表单,我试图将其发送到控制器方法,但出现404错误.这个问题的转折点在于该表单具有2个提交按钮,并且单击的提交按钮决定了其中一个参数的值.这是我的表格.
I have a Spring 3 MVC form with 2 parameters I'm trying to send to my controller method, and I'm getting a 404 error. The twist on this problem is that the form has 2 submit buttons, and the submit button that is clicked dictates the value of one of the parameters. Here is my form.
<form:form action="/approve/${bulletin.id}" method="post">
<table>
<tr>
<td colspan="2"><b>From:</b> <c:out value="${bulletin.name}" /></td>
</tr>
<tr>
<td colspan="2"><b>Subject:</b> <c:out
value="${bulletin.subject}" /></td>
</tr>
<tr>
<td colspan="2"><b>Date:</b> <c:out value="${bulletin.date}" />
<br></td>
</tr>
<tr>
<td colspan="2"><t:notePrint note="${bulletin.note}" /> <input
type="hidden" name="id" value="${bulletin.id}" /></td>
</tr>
<tr>
<td><input type="submit" name="approve" value="Approve" /></td>
<td><input type="submit" name="deny" value="Deny" /></td>
</tr>
</table>
<br />
</form:form>
这是我的控制器表格.
@RequestMapping(value = "/approve/{id}", method = RequestMethod.POST)
public String approveBulletin(@RequestParam int id,
@RequestParam(required = false, value = "approve") String approve,
@RequestParam(required = false, value = "deny") String deny, Model model) {
try {
if (approve.equalsIgnoreCase("approve")) {
bulletinDAO.approveBulletin(id);
model.addAttribute("approval",
"Your bulletin has been approved.");
}
if (deny.equalsIgnoreCase("deny")) {
bulletinDAO.denyBulletin(id);
model.addAttribute("approval", "Your bulletin has been denied.");
}
List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
model.addAttribute("bulletins", bulletins);
} catch (Exception e) {
System.out.println(e.getMessage());
return "FailurePage";
}
return "ApproveBulletin";
}
推荐答案
我已经解决了自己的问题.我发布我的代码是为了让其他遇到相同问题的线程受益.这是我的表格.
I have solved my own problem. I am posting my code for the benefit of anyone else who hits this thread with the same problem. Here is my form.
<form:form action="approve" method="post">
<table>
<tr>
<td colspan="2"><b>From:</b> <c:out value="${bulletin.name}" /></td>
</tr>
<tr>
<td colspan="2"><b>Subject:</b> <c:out
value="${bulletin.subject}" /></td>
</tr>
<tr>
<td colspan="2"><b>Date:</b> <c:out value="${bulletin.date}" />
<br></td>
</tr>
<tr>
<td colspan="2"><t:notePrint note="${bulletin.note}" /> <input
type="hidden" name="id" value="${bulletin.id}" /></td>
</tr>
<tr>
<td><input type="submit" name="approve" value="Approve" /></td>
<td><input type="submit" name="deny" value="Deny" /></td>
</tr>
</table>
<br />
</form:form>
这是我的控制器方法.
@RequestMapping(value = "/approve", method = RequestMethod.POST, params = { "approve" })
public String approve(@RequestParam int id, @RequestParam String approve, Model model) {
try {
bulletinDAO.approveBulletin(id);
model.addAttribute("approval", "Your bulletin has been approved.");
List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
model.addAttribute("bulletins", bulletins);
} catch (Exception e) {
System.out.println(e.getMessage());
return "FailurePage";
}
return "ApproveBulletin";
}
@RequestMapping(value = "/approve", method = RequestMethod.POST, params = { "deny" })
public String deny(@RequestParam int id, @RequestParam String deny, Model model) {
try {
bulletinDAO.denyBulletin(id);
model.addAttribute("approval", "Your bulletin has been denied.");
List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
model.addAttribute("bulletins", bulletins);
} catch (Exception e) {
System.out.println(e.getMessage());
return "FailurePage";
}
return "ApproveBulletin";
}
这篇关于Spring 3-带有2个按钮的表单,向控制器方法发送2个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文