我正在尝试使用基于注释的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来说是相当简单的。例如,这两个提交按钮处理“上一个”和“保存”操作:

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

其他提示

如果你真的想在表单上有两个提交按钮,你可以使用这样的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(...      
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top