주석을 사용하여 스프링 MVC에서 여러 제출 버튼으로 양식을 작성하는 방법이 있습니까?

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

  •  06-07-2019
  •  | 
  •  

문제

주석 기반 스프링 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 및 컨트롤러만으로는 합리적으로 간단합니다. 예를 들어이 두 제출 버튼은 '이전'및 '저장'작업을 처리합니다.

<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(value = "/books/documentTypeRemove.do", method = RequestMethod.POST)
public String removeDocType(...      
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top