Spring

Spring 일지 #63 (exam) 시험 대비8(삭제하기)

uni5948 2021. 10. 8. 00:20

63. 삭제하기

-삭제하기

 * 삭제하기 태그 추가(board_select_one.jsp)

 * 조회수 증가 처럼 script를 이용해 삭제하기를 구현한다.

 

... ....

<a th:href="@{/board/select}">목록으로</a>

    <a href="#" th:onclick="|javascript:deleteBoard( '${board.no}' ) |">삭제하기</a>

 

    <a th:if="${prev != 0}" th:href="@{/board/select_one(no=${prev})}">이전글</a>

    <a th:if="${next != 0}" th:href="@{/board/select_one(no=${next})}">다음글</a>

... ...

 

 * script 생성

 * 삭제 확인창을 띄워 확인 시 삭제가 되도록 한다.

 

더보기

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>상세 페이지</title>

</head>

<body>

    <h3>상세 페이지</h3>

    <hr/>

    글번호 : <div style="display: inline-block;" th:text="${board.no}"></div><br/>

    글제목 : <div style="display: inline-block" th:text="${board.title}"></div><br />

    글내용 : <div style="display: inline-block" th:text="${board.content}"></div><br />

    작성자 : <div style="display: inline-block" th:text="${board.writer}"></div><br />

    조회수 : <div style="display: inline-block" th:text="${board.hit}"></div><br />

    날짜 : <div style="display: inline-block" th:text="${board.regdate}"></div><br />

    이미지 : <img th:src="@{/board/select_image(no=${board.no})}" style="width: 50px; height: 50px;"></div><br />

    <hr/>

 

    <a th:href="@{/board/select}">목록으로</a>

    <a href="#" th:onclick="|javascript:deleteBoard( '${board.no}' ) |">삭제하기</a>

 

    <a th:if="${prev != 0}" th:href="@{/board/select_one(no=${prev})}">이전글</a>

    <a th:if="${next != 0}" th:href="@{/board/select_one(no=${next})}">다음글</a>

 

    <script>

        function deleteBoard(no){

            const ret = confirm("삭제할까요?");

            if(ret){

                const xhr = new XMLHttpRequest();

                const url = "/ROOT/board/delete?no="+no;

                xhr.open('GET'urltrue);

                xhr.responseType = 'json';

                xhr.setRequestHeader("Content-Type",

                'application/json; charset=UTF8;');

                xhr.onload = function(e){

                    console.log(e);

                    if(e.target.status === 200) {

                        location.href"/ROOT/board/delete?no="+no;

                    }

                }

                xhr.send();

            }

        }

    </script>

</body>

</html>

 

 */delete get 생성

 

    @GetMapping(value = "/delete")

    public String deleteGET(@RequestParam("no")long noModel model) {

        try{

            if(no == 0){

                return "redirect:/board/select";

            }

            bRepository.deleteById(no);

            return "redirect:/board/select";

        }

        catch(Exception e){

            e.printStackTrace();

            return "redirect:/board/select";

        }

    }

 

 * 삭제하기

삭제 알림
삭제 완료