Framework/Spring Boot

전체 게시글 목록에서 각 게시글의 댓글 수 출력

잔망루피 2022. 7. 7. 20:37
반응형

전체 게시글 목록에서 각 게시글 제목 옆에 총 댓글 수를 출력한다.

 

 

첫 번째 시도) 👉 실패

 
<td><a th:text="${post.title}" th:href="@{'/post/detail/' + ${post.id}}"></a>
    <span class="badge bg-primary">
        <span th:value="${#lists.size(post.comments)}"></span>
    </span></td>

comments

 

An error happened during template parsing (template: "class path resource [templates/post/postList.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/post/postList.html]")
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#lists.size(post.comments)" (template: "post/postList" - line 46, col 35)
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#lists.size(post.comments)" (template: "post/postList" - line 46, col 35)

Caused by: java.lang.IllegalArgumentException: Cannot get list size of null

Post에 PostRequestDto를 넣었는데 comments 필드가 null이다.

Post 엔티티에 comments 필드가 없기 때문에 Post 엔티티를 PostRequestDto로 바꿀 때 comments 필드는 null이다.

 

 

두 번째 시도) 👉 성공

private int comment_cnt;

post 엔티티에 댓글 수를 담는 필드를 추가했다.

 

@Builder
public Post(String title, String content, String author, int view, int comment_cnt){
    this.title=title;
    this.content=content;
    this.author=author;
    this.view=view;
    this.comment_cnt=comment_cnt;
}

public void update(String title, String content, int view, int comment_cnt){
    this.title=title;
    this.content=content;
    this.view=view;
    this.comment_cnt=comment_cnt;
}

post 엔티티의 생성자, update 메소드에 comment_cnt를 추가한다.

 

-- SQL
ALTER TABLE POST ADD COLUMN COMMENT_CNT INT DEFAULT 0;

post 테이블에 필드를 추가했다.

 

post.update(post.getTitle(), post.getContent(), post.getView(), post.getComment_cnt() + 1);

댓글을 등록할 때 comment_cnt가 1 증가한다.

댓글을 등록하는 메소드에 추가했다.

 

post.update(post.getTitle(), post.getContent(), post.getView(), post.getComment_cnt() - 1);

댓글을 삭제할 때 comment_cnt가 -1 감소한다.

댓글을 삭제하는 메소드에 추가했다.

 

<td><a th:text="${post.title}" th:href="@{'/post/detail/' + ${post.id}}"></a>
    <span class="badge bg-light">
        <i class="bi bi-chat-right-dots"></i>
        <a th:text="'+' + ${post.comment_cnt}"></a>
    </span>
</td>

Bootstrap의 말풍선 아이콘과 댓글 수를 Bootstrap의 badge에 담았다.

다음과 같이 제목 옆에 댓글 수가 뜬다.

 

 

참고 👇

https://walbatrossw.github.io/spring-mvc/2018/03/15/14-spring-mvc-board-transaction.html#35-%EB%8C%93%EA%B8%80-%EB%B9%84%EC%A7%80%EB%8B%88%EC%8A%A4-%EA%B3%84%EC%B8%B5%EC%9D%98-%EC%88%98%EC%A0%95-%EB%B0%8F-%ED%8A%B8%EB%9E%9C%EC%9E%AD%EC%85%98-%EC%A0%81%EC%9A%A9--replyserviceimpl

 

Spring-MVC 게시판 예제 14 - 게시글 조회수, 댓글 갯수의 출력 구현과 트랜잭션 처리 - 더블에스 Devl

본 포스팅은 코드로 배우는스프링 웹프로젝트를 참조하여 작성한 내용입니다. 개인적으로 학습한 내용을 복습하기 위해 기록한 내용이기 때문에 오류가 있다면 지적 부탁드리겠습니다. 포스팅

walbatrossw.github.io

 

https://getbootstrap.com/docs/5.2/components/badge/#examples

 

Badges

Documentation and examples for badges, our small count and labeling component.

getbootstrap.com

 

https://icons.getbootstrap.com/

 

Bootstrap Icons

Official open source SVG icon library for Bootstrap

icons.getbootstrap.com

 

반응형