최원종의 개발 블로그

V1(익명블로그)-게시글 수정하기 본문

Spring boot 입문

V1(익명블로그)-게시글 수정하기

chl6698 2026. 4. 30. 17:39

목표

게시글 수정 시 ---> 다시 사용자가 게시글  작성할 수 있도록 설계

 

update-form.mustache 코드

{{> layout/header}}
<div class="container p-5 flex-grow-1">
    <div class="card">
        <div class="card-header"><b>글수정 화면입니다</b></div>
        <div class="card-body">
            <!--  익명 게시글 작성    -->
            <form action="/board/save" method="post">
                <div class="mb-3">
                    <input type="text" name="username"  class="form-control"
                           placeholder="enter username" value="{{board.username}}">
                </div>
                <div class="mb-3">
                    <input type="text" name="title" class="form-control"
                           placeholder="enter title" value="{{board.title}}">
                </div>
                <div class="mb-3">
                    <textarea type="text" name="content" rows="5" class="form-control" placeholder="enter title">
                        {{board.content}}
                    </textarea>
                </div>
                <button class="btn btn-primary">글수정 완료</button>
            </form>
        </div>
    </div>
</div>

{{> layout/footer}}

 

BoardController 코드 부분

    // http://localhost:8080/board/1/update-form
    @GetMapping("/board/{id}/update-form")
    public String updateFormPage(@PathVariable(name = "id") Integer id, Model model) {
        // 사용자 에게 해당 게시물 내용을 보여 줘야 한다.

        // 조회 기능 - 게시글 id로
        Board board = boardNativeRepository.findById(id);
        model.addAttribute("board", board);

        return "board/update-form";
    }

 

게시글 수정하기 페이지 만들기

 


 

BoardController 코드 부분

// /board/{id}/update
    @PostMapping("/board/{id}/update")
    public String updateProc(@PathVariable(name = "id") Integer id,
                         @RequestParam(name = "username") String username,
                         @RequestParam(name = "title") String title,
                         @RequestParam(name = "content") String content) {

        log.info("username : " + username);
        log.info("title : " + title);
        log.info("content : " + content);
        log.info("id : " + id);

        boardNativeRepository.updateById(username, title, content, id);
        // 게시글 수정 완료 ---> 게시글 목록, 게시글 상세보기 화면
        // 리다이렉트는 뷰 리졸브 동작이 아닌 (내부 파일 찾는 것이 아니고)
        // 그냥 새로은 HTTP Get 요청이다.
        return "redirect:/board/" + id;
    }

 

BoardNativeRepository 코드 부분

    // 게시글 수정하기
    @Transactional
    public boolean updateById(String username, String title, String content, Integer id) {

        String queryStr = """
                update board_tb set username = ?, title = ?, content = ? where id = ?
                """;
        Query query = em.createNativeQuery(queryStr);
        query.setParameter(1, username);
        query.setParameter(2, title);
        query.setParameter(3, content);
        query.setParameter(4, id);

        int rows = query.executeUpdate();
        if(rows > 0) {
            return true;
        } else {
            return false;
        }
    }