有没有办法使用注释在 Spring MVC 上创建带有多个提交按钮的表单? [英] Is there any way to create form with multiple submit buttons on Spring MVC using annotations?
本文介绍了有没有办法使用注释在 Spring MVC 上创建带有多个提交按钮的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用基于注释的 Spring MVC 创建简单的添加/删除表单.添加"功能很顺利,但是当我尝试添加另一个按钮来表单时,我被卡住了.
I'm trying to create simple add/remove form using annotation based Spring MVC. 'Add' functionality comes smoothly, but when I tried to add another button to form i've got stuck.
这是我的代码:
控制器动作:
@RequestMapping(value = "/books/documentType.do", method = RequestMethod.GET)
public String getDocType(
@RequestParam(required = false, value = "id") Long id,
ModelMap model) {
DocTypeDTO docType = new DocTypeDTO();
if (id != null)
docType = docTypeConverter.getDTO(id);
model.addAttribute("docType", docType);
return "/books/documentType";
}
@RequestMapping(value = "/books/documentType.do", method = RequestMethod.POST)
public String setDocType(
@ModelAttribute("docType") DocTypeDTO docType,
BindingResult result,
SessionStatus sessionStatus
) {
docTypeValidator.validate(docType, result);
if (result.hasErrors())
return "/books/documentType";
else {
docTypeConverter.saveDTO(docType);
sessionStatus.setComplete();
return "redirect:/books/documentTypes.do";
}
}
非常标记的表单:
<form:form method="post" commandName="docType" id="editForm">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#dbdbdb">
<tr>
<td></td>
<td>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="spacer"><img src="/images/spacer.gif" width="116" height="1" border="0"/></td>
<td class="spacer"><img src="/images/spacer.gif" width="216" height="1" border="0"/></td>
</tr>
<tr>
<td class="form-cell-text-underlined">Отображать на сайте</td>
<td colspan="2">
<form:checkbox path="shownOnSite"/>
</td>
</tr>
<tr>
<td class="form-cell-text-underlined">Международный</td>
<td colspan="2">
<form:checkbox path="international"/>
</td>
</tr>
<tr>
<td class="form-cell-text-underlined">Внутренний код</td>
<td colspan="2">
<form:input path="internalCode"/>
</td>
</tr>
<tr>
<td class="form-cell-text-underlined">Код</td>
<td colspan="2">
<form:input path="code"/>
<form:errors path="code"/>
</td>
</tr>
<tr>
<td class="form-cell-text-underlined">Код IATA</td>
<td colspan="2">
<form:input path="codeIATA"/>
</td>
</tr>
<tr>
<td class="padded-underlined">Название</td>
<td colspan="2">
<form:input path="name"/>
<form:errors path="name"/>
</td>
</tr>
<tr>
<td class="padded-underlined">Название(Англ.)</td>
<td colspan="2">
<form:input path="nameEn"/>
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="Сохранить">
</td>
</tr>
</table>
</td>
<td></td>
</tr>
</table>
谢谢!
推荐答案
使用 Spring MVC 3,只需使用 JSP 和控制器就可以相当直接地做到这一点.例如,这两个提交按钮处理上一个"和保存"操作:
With Spring MVC 3, this is reasonably straight-forward to do with just JSP and Controller. For example, these two submit buttons handle 'previous' and 'save' actions:
<input value="Save" name="save" type="submit" id="btnSave" class="submit_button">
<input value="Previous" name="previous" type="submit" id="btnPrevious" class="submit_button">
然后,在控制器中,您接受输入名称作为请求映射中的参数,以及控制器地址":
Then, in the controller, you accept the input name as a param in the request mapping, along with the controller 'address':
@RequestMapping(value="thisForm.form", params="save")
public String save() {
// save
}
@RequestMapping(value="thisForm.form", params="previous")
public String doPreviousStuff() {
// get mapping for previous page and return
}
这篇关于有没有办法使用注释在 Spring MVC 上创建带有多个提交按钮的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文