Gibt es eine Möglichkeit Formular mit mehreren einreichen Tasten auf Spring MVC mit Annotationen zu erstellen?

StackOverflow https://stackoverflow.com/questions/1631262

  •  06-07-2019
  •  | 
  •  

Frage

Ich versuche einfach add erstellen / entfernen Form Annotation mit basierter Spring MVC. ‚Add‘ Funktionalität kommt reibungslos, aber als ich versuchte, eine andere Taste, um in der i stecken habe zu bilden.

Hier ist mein Code:

Controller Aktionen:

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

}

Awfully markuped Form:

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

Danke!

War es hilfreich?

Lösung

Mit Spring MVC 3, das ist ziemlich geradlinig mit nur JSP und Controller zu tun. Zum Beispiel, geben diese beiden Tasten ‚zurück‘ und ‚speichern‘ Aktionen behandeln:

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

Dann in der Steuerung, akzeptieren Sie die Eingangsnamen als param in dem Anfrage-Mapping, zusammen mit dem Controller 'Adresse':

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

Andere Tipps

Wenn Sie wirklich zwei Schaltflächen auf dem Formular einreichen möchten, können Sie es mit Javascript tun wie folgt aus (mit jQuery in diesem Beispiel):

<SCRIPT language=JavaScript>
  function remove() {
    $('#editForm').attr("action", "documentTypeRemove.do");
    $("#editForm").submit();
  }
</SCRIPT>

...

<button type="button" onclick="remove();">Remove</button>

Dann eine weitere RequestMapping in Ihrem Controller erstellen:

@RequestMapping(value = "/books/documentTypeRemove.do", method = RequestMethod.POST)
public String removeDocType(...      
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top