Spring

Spring 일지 #64 (exam) 시험 대비9(수정하기)

uni5948 2021. 10. 8. 00:34

64. 수정하기

-수정하기

 * 수정하기 태그 추가(board_select_one.jsp)

... ...

 

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

    <a th:href="@{/board/update(no=${board.no})}">수정하기</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>

 

... ...

 

 * /update get 생성(BoardController.java)

 

    @GetMapping(value = "/update")

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

        try{

            if(no == 0){

                return "redirect:/board/select";

            }

            Optional<Boardobj = bRepository.findById(no);

            if(obj.isPresent()){

                Board board = obj.get();

                model.addAttribute("board"board);

            }

            return "/board_update";

        }

        catch(Exception e){

            e.printStackTrace();

            return "redirect:/board/select";

        }

    }

    

 * 수정페이지 생성(board_update.jsp)

 

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

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

    <hr />

    <form th:action="@{/board/update}" method="post"> 

        <input type="hidden" name="no" th:value="${board.no}"/> <br/>

        제목 <input type="text" name="title" th:value="${board.title}"/><br />

        내용 <input type="text" name="content" th:value="${board.content}"/><br />

        작성자 <input type="text" name="writer" th:value="${board.writer}"/><br />

        <input type="submit" value="글수정" />

    </form>

</body>

</html>

 

 *게시물 수정 페이지 확인

게시물 수정 페이지

 

 * /update post 생성(BoardController.java)

 * 제목, 내용, 작성자만 수정한다.

    //수정하기

    @PostMapping(value = "/update")

    public String updatePOST(

        @ModelAttribute Board board

        @RequestParam("no"long no,

        @RequestParam("title"String title,

        @RequestParam("content"String content,

        @RequestParam("writer"String writerthrows IOException {

        //board로 넘어오는 값을 받고 없는 항목은

        //글번호, 제목, 내용, 작성자

        board.setNo(no);

        board.setTitle(title);

        board.setContent(content);

        board.setWriter(writer);

        

          //조회수, 이미지 관련내용

         //board.getNo()를 이용해서 가져온 값을 넣은 후 save

        Board board2 = bRepository.getById(no); <- 수정하지 않을 항목

        board.setImage(board2.getImage());

        board.setImagesize(board2.getImagesize());

        board.setImagetype(board2.getImagetype());

        board.setHit(board2.getHit());

        bRepository.save(board);

 

        System.out.println(board.toString());

        bRepository.save(board);

        return "board_update";

    }

 

 * 수정하기

수정하기
수정 완료