[스프링부트 blogV1] 17. 게시글 수정화면

silver's avatar
Dec 30, 2024
[스프링부트 blogV1] 17. 게시글 수정화면
 

1. 수정화면 mustache로 그리기

{{> layout/header}} <section> <form action="/board/{{model.id}}/update" method="post" enctype="application/x-www-form-urlencoded"> <input type="text" name="id" value="1" ><br> <input type="text" name="title" value="제목1" ><br></br> <input type="text" name="content" value="내용1" ><br></br> <input type="text" name="createdAt" value="2024.08.11" ><br></br> <button type="submit">글 수정하기</button> <button type="submit">글 수정하기</button> </form> </section> </body> </html>

2. Repository

게시글 상세보기에 서 사용한 메서드 그대로 가져와서 사용
public Board findById(int id) { Query q = em.createNativeQuery("select * from board_tb where id = ?", Board.class); q.setParameter(1, id); return (Board) q.getSingleResult(); }

BoardResponse

public static class UpdateFormDTO { private int id; private String title; private String content; private String createdAt; public UpdateFormDTO(Board board) { this.id = board.getId(); this.title = board.getTitle(); this.content = board.getContent(); this.createdAt = formatDate(board.getCreatedAt()); } }

3. Controller

@GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable int id, Model model) { BoardResponse.DetailDTO boardDetail = boardService.게시글수정화면보기(id); return "update-form"; }
 

4. Service

public BoardResponse.UpdateFormDTO 게시글수정화면보기(int id) { Board board = boardRepository.findById(id); return new BoardResponse.UpdateFormDTO(board); }
Share article

silver