There was an unexpected error(type=Method Not Allowed, status=405).Request method 'POST' not support

Jinju Bae·2022년 2월 20일
0

게시판을 만들던 중 다음과 같은 오류가 발생했다.

There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported

구글에 쳐보니 메소드 매칭이 되지 않아 생기는 문제라고 한다.

컨트롤러에서 다음과 같이 @PutMapping 으로 update 를 진행했다.

    @PutMapping("/update")
    public String updateBoard(Board board) {
        service.updateBoard(board);
        return "redirect:/board/main";
    }

내용을 입력하세요.
그래서 update.html 에서 form 태그의 method 또한 post 가 아닌 put 으로 보내주어야 한다.

<form id="form" th:action="@{update}" method="put">
    <p>글번호 : [[${update.boardId}]]</p>
    제목 :
    <div id="title">
        <textarea class="form-control" th:placeholder="${update.title}" id='updateTitle'>[[${update.title}]]</textarea>
    </div>
    <p>작성자 : [[${update.name}]]</p>
    내용 :
    <div id="content">
        <textarea class="form-control" th:placeholder="${update.content}" id='updateContent'>[[${update.content}]]</textarea>
    </div>
</form>

<form th:action="@{main}" method="put">
    <input type="hidden" name="boardId" th:value="${update.boardId}">
    <button type="submit">수정</button>
</form>

내용을 입력하세요.
그런데 html 의 form 태그는 post, get 방식만 지원한다. 그래서 나도 method="post" 라고 써서 오류가 났던 것이다.

다행히도, form 태그가 method 를 put 으로 보낼 수 있게 하는 방법도 있다고 한다.

  • resources 폴더의 application.properties 혹은 application.yml 파일에 들어간다.

  • spring.mvc.hiddenmethod.filter.enabled=true 를 추가한다.

수정 버튼을 다시 누르면 put 방식으로 제대로 전송되는 모습을 확인할 수 있다.

profile
진주개발일지 (珍珠開發日誌)

0개의 댓글