@3/13/2023
게시글 조회 페이지 생성
•
본문 내용 출력
•
댓글 출력
•
댓글 달기 기능
화면 제작
목록으로 돌아가기
•
n페이지에서 들어왔으면 n페이지 목록으로 돌아가기
◦
referer 이용 → 해보니 url을 직접 쳐서 게시글로 들어올 경우 적합하지 않음
◦
requestParam 이용
▪
PostController
@GetMapping("/posts/{postId}")
public String viewPost(
@PathVariable Long postId,
@RequestParam(required = false) Long currentPage,
Model model
) {
// 게시글 로딩
Post foundPost = postService.findPostById(postId);
model.addAttribute("post", foundPost);
// 댓글 로딩
List<CommentListDTO> commentListDTOs = commentService.findAllByPostId(postId);
model.addAttribute("commentListDTOs", commentListDTOs);
log.info("commentListDTOs.size() = {}", commentListDTOs.size());
// 현재 페이지 저장
currentPage = (currentPage == null) ? 1 : currentPage;
model.addAttribute("currentPage", currentPage);
return "posts/postView";
}
Java
복사
▪
postView.html
<div class="container d-flex">
<button type="button" class="btn btn-outline-secondary btn ms-auto mt-2 fw-bold">
<a class="list-group-item" th:href="@{/posts?currentPage={currentPage}(currentPage=${currentPage})}">목록으로</a>
</button>
</div>
Java
복사