有什么办法来创建表单有多个使用注释提交关于Spring MVC的按钮?(Is there any w

2019-09-01 13:44发布

我试图创建简单的添加/使用基于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";
    }

}

非常markuped形式:

<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>

谢谢!

Answer 1:

借助于Spring MVC 3,这是相当直接的只用JSP和控制器做。 例如,这两个提交按钮处理“之前的”和“拯救”行动:

<input value="Save" name="save" type="submit" id="btnSave" class="submit_button">
<input value="Previous" name="previous" type="submit" id="btnPrevious" class="submit_button">

然后,在控制器,你接受输入名称,如请求映射设置了一个param,与控制器“地址”一起:

@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
} 


Answer 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(...      


文章来源: Is there any way to create form with multiple submit buttons on Spring MVC using annotations?