注釈を使用してSpring MVCで複数の送信ボタンでフォームを作成する方法はありますか?
-
06-07-2019 - |
質問
注釈ベースのSpring MVCを使用して、単純な追加/削除フォームを作成しようとしています。 「追加」機能はスムーズに機能しますが、別のボタンをフォームに追加しようとするとスタックします。
ここに私のコードがあります:
コントローラーアクション:
@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とControllerだけで行うのがかなり簡単です。たとえば、次の2つの送信ボタンは、「前の」アクションと「保存」アクションを処理します。
<input value="Save" name="save" type="submit" id="btnSave" class="submit_button">
<input value="Previous" name="previous" type="submit" id="btnPrevious" class="submit_button">
次に、コントローラーで、コントローラー「アドレス」とともに、リクエストマッピングのパラメーターとして入力名を受け入れます:
@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
}
他のヒント
フォームに2つの送信ボタンが本当に必要な場合は、次のようなJavascriptを使用して実行できます(この例ではjQueryを使用):
<SCRIPT language=JavaScript>
function remove() {
$('#editForm').attr("action", "documentTypeRemove.do");
$("#editForm").submit();
}
</SCRIPT>
...
<button type="button" onclick="remove();">Remove</button>
次に、コントローラーで別のRequestMappingを作成します:
@RequestMapping(value = "/books/documentTypeRemove.do", method = RequestMethod.POST)
public String removeDocType(...
所属していません StackOverflow