편집 기록

편집 기록
  • 프로필 정영훈님의 편집
    날짜2019.11.26

    Springboot RequestMapping


    중요부분만 넣었습니다.

    JpaBoardController.java

    ...생략...
    @RequestMapping(value="/jpa/board/{boardIdx}", method= RequestMethod.PUT)
        public String updateBoard(BoardEntity board) throws Exception{
            jpaBoardService.saveBoard(board, null);
            return "redirect:/jpa/board";
        }
    
        @RequestMapping(value="/jpa/board/{boardIdx}", method= RequestMethod.DELETE)
        public String deleteBoard(@PathVariable("boardIdx") int boardIdx) throws Exception{
            jpaBoardService.deleteBoard(boardIdx);
            return "redirect:/jpa/board";
        }
        ..생략...
    

    jpaBoardDetail.html

    <!DOCTYPE html>
    <html lang="ko" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>게시글 상세 화면</title>
        <link rel="stylesheet" th:href="@{/css/style.css}"/>
    </head>
    <body>
        <div class="container">
            <h2>게시글 상세 화면</h2>
            <form id="frm" method="post">
                <table class="board_detail">
                    <colgroup>
                        <col width="15%"/>
                        <col width="35%"/>
                        <col width="15%"/>
                        <col width="35%"/>
                    </colgroup>
                    <caption>게시글 상세내용</caption>
                    <tbody>
                        <tr>
                            <th scope="row">글 번호</th>
                            <td th:text="${board.boardIdx }"></td>
                            <th scope="row">조회수</th>
                            <td th:text="${board.hitCnt }"></td>
                        </tr>
                        <tr>
                            <th scope="row">작성자</th>
                            <td th:text="${board.creatorId }"></td>
                            <th scope="row">작성일</th>
                            <td th:text="${board.createdDatetime }"></td>
                        </tr>
                        <tr>
                            <th scope="row">제목</th>
                            <td colspan="3"><input type="text" id="title" name="title" th:value="${board.title }"/></td>
                        </tr>
                        <tr>
                            <td colspan="4" class="view_text">
                                <textarea title="내용" id="contents" name="contents" th:text="${board.contents }"></textarea>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <input type="hidden" id="boardIdx" name="boardIdx" th:value="${board.boardIdx}">
                <input type="hidden" id="method" name="_method"/>
            </form>
    
            <div class="file_list">
                <a th:each="list : ${board.fileList}" th:href="@{/jpa/board/file(idx=${list.idx}, boardIdx=${board.boardIdx})}" th:text="|${list.originalFileName} (${#numbers.formatInteger(list.fileSize/1000, 1, 'DEFAULT')} kb)|"></a>
            </div>
    
            <a href="#this" id="list" class="btn">목록으로</a>
            <a href="#this" id="edit" class="btn">수정하기</a>
            <a href="#this" id="delete" class="btn">삭제하기</a>
        </div>
    
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                var boardIdx = $("#boardIdx").val();
    
                $("#list").on("click", function(){
                    location.href = "/jpa/board/";
                });
    
                $("#edit").on("click", function(){
                    $("#method").val("put");
    
                    var frm = $("#frm")[0];
                    frm.action = "/jpa/board/"+boardIdx;
                    frm.submit();
                });
    
                $("#delete").on("click", function(){
                    $("#method").val("delete");
    
                    var frm = $("#frm")[0];
                    frm.action = "/jpa/board/"+boardIdx;
                    frm.submit();
                });
            });
        </script>
    </body>
    </html>
    

    Service하고 Repository부분은 에러가 없는거 같아서 안올렸습니다.

    에러내용입니다.

    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

    포스트 방식이 지원이 안되는거 같습니다. 위에 update부분에 @RequestMapping 부분에 method={RequestMethod.POST,RequestMethod.PUT} 방식으로 놔봤는데 update는 잘 이루어졌습니다. 하지만 delete부분도 똑같이 method={RequestMethod.POST,RequestMethod.DELETE} 방식으로 놨습니다.

    @RequestMapping(value="/jpa/board/{boardIdx}", method={RequestMethod.PUT,RequestMethod.POST})
        public String updateBoard(BoardEntity board) throws Exception{
            jpaBoardService.saveBoard(board, null);
            return "redirect:/jpa/board";
        }
    
        @RequestMapping(value="/jpa/board/{boardIdx}", method={RequestMethod.DELETE,RequestMethod.POST})
        public String deleteBoard(@PathVariable("boardIdx") int boardIdx) throws Exception{
            jpaBoardService.deleteBoard(boardIdx);
            return "redirect:/jpa/board";
        }
    

    그리고나서 에러입니다. java.lang.IllegalStateException: Ambiguous handler methods mapped for '/jpa/board/12': {public java.lang.String board.board.controller.JpaBoardController.deleteBoard(int) throws java.lang.Exception, public java.lang.String board.board.controller.JpaBoardController.updateBoard(board.board.entity.BoardEntity) throws java.lang.Exception}

    그리고 delete부분에만 post추가하면 수정을 누루면 delete처리됩니다. ㅠㅠ 책이랑 똑같이 해봤는데 왜이러는걸까요

  • 프로필 jgg0819님의 편집
    날짜2019.11.26

    Springboot RequestMapping


    중요부분만 넣었습니다.

    JpaBoardController.java

    ...생략... @RequestMapping(value="/jpa/board/{boardIdx}", method= RequestMethod.PUT) public String updateBoard(BoardEntity board) throws Exception{ jpaBoardService.saveBoard(board, null); return "redirect:/jpa/board"; }

    @RequestMapping(value="/jpa/board/{boardIdx}", method= RequestMethod.DELETE)
    public String deleteBoard(@PathVariable("boardIdx") int boardIdx) throws Exception{
        jpaBoardService.deleteBoard(boardIdx);
        return "redirect:/jpa/board";
    }
    ..생략...
    

    jpaBoardDetail.html <!DOCTYPE html> 게시글 상세 화면 게시글 상세 화면 게시글 상세내용 글 번호 조회수 작성자 작성일 제목

        <div class="file_list">
            <a th:each="list : ${board.fileList}" th:href="@{/jpa/board/file(idx=${list.idx}, boardIdx=${board.boardIdx})}" th:text="|${list.originalFileName} (${#numbers.formatInteger(list.fileSize/1000, 1, 'DEFAULT')} kb)|"></a>
        </div>
    
        <a href="#this" id="list" class="btn">목록으로</a>
        <a href="#this" id="edit" class="btn">수정하기</a>
        <a href="#this" id="delete" class="btn">삭제하기</a>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var boardIdx = $("#boardIdx").val();
    
            $("#list").on("click", function(){
                location.href = "/jpa/board/";
            });
    
            $("#edit").on("click", function(){
                $("#method").val("put");
    
                var frm = $("#frm")[0];
                frm.action = "/jpa/board/"+boardIdx;
                frm.submit();
            });
    
            $("#delete").on("click", function(){
                $("#method").val("delete");
    
                var frm = $("#frm")[0];
                frm.action = "/jpa/board/"+boardIdx;
                frm.submit();
            });
        });
    </script>
    

    Service하고 Repository부분은 에러가 없는거 같아서 안올렸습니다.

    에러내용입니다.

    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

    포스트 방식이 지원이 안되는거 같습니다. 위에 update부분에 @RequestMapping 부분에 method={RequestMethod.POST,RequestMethod.PUT} 방식으로 놔봤는데 update는 잘 이루어졌습니다. 하지만 delete부분도 똑같이 method={RequestMethod.POST,RequestMethod.DELETE} 방식으로 놨습니다.

    @RequestMapping(value="/jpa/board/{boardIdx}", method={RequestMethod.PUT,RequestMethod.POST}) public String updateBoard(BoardEntity board) throws Exception{ jpaBoardService.saveBoard(board, null); return "redirect:/jpa/board"; }

    @RequestMapping(value="/jpa/board/{boardIdx}", method={RequestMethod.DELETE,RequestMethod.POST})
    public String deleteBoard(@PathVariable("boardIdx") int boardIdx) throws Exception{
        jpaBoardService.deleteBoard(boardIdx);
        return "redirect:/jpa/board";
    }
    

    그리고나서 에러입니다. java.lang.IllegalStateException: Ambiguous handler methods mapped for '/jpa/board/12': {public java.lang.String board.board.controller.JpaBoardController.deleteBoard(int) throws java.lang.Exception, public java.lang.String board.board.controller.JpaBoardController.updateBoard(board.board.entity.BoardEntity) throws java.lang.Exception}

    그리고 delete부분에만 post추가하면 수정을 누루면 delete처리됩니다. ㅠㅠ 책이랑 똑같이 해봤는데 왜이러는걸까요