Skip to main content

03D. 답변 개수 표시

Less than 1 minuteJavaSpringAWScrashcoursejavajdkjdk8streamspringspringframeworkspringbootawsaws-ec2

03D. 답변 개수 표시 관련


3-04. 답변 개수 표시

점프 투 스프링부트 - WikiDocs

pahkey/sbb3 - 3-04open in new window

이번에는 질문 목록에 "해당 질문에 달린 답변 개수"를 표시할 수 있는 기능을 추가해 보자. 코드의 분량은 많지 않지만, 게시판 서비스를 더욱 서비스답게 만들어 주는 기능이다.

답변 개수는 다음처럼 게시물 제목 바로 오른쪽에 표시하자.

파일명: /sbb/src/main/resources/templates/question_list.html

<html layout:decorate="~{layout}">
<div layout:fragment="content" class="container my-3">
    <table class="table">
        <thead class="table-dark">
            <tr>
                <th>번호</th>
                <th>제목</th>
                <th>작성일시</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="question, loop : ${paging}">
                <td th:text="${paging.getTotalElements - (paging.number * paging.size) - loop.index}"></td>
                <td>
                    <a th:href="@{|/question/detail/${question.id}|}" th:text="${question.subject}"></a>
                    <span class="text-danger small ms-2"
                        th:if="${#lists.size(question.answerList) > 0}" 
                        th:text="${#lists.size(question.answerList)}">
                    </span>
                </td>
                <td th:text="${#temporals.format(question.createDate, 'yyyy-MM-dd HH:mm')}"></d>
            </tr>
        </tbody>
    </table>
    <!-- (... 생략 ...)  -->

th:if="${#lists.size(question.answerList) > 0}"로 답변이 있는지 조사하고, th:text="${#lists.size(question.answerList)}"로 답변 개수를 표시했다.

#list.size(이터러블객체)는 이터러블 객체의 사이즈를 반환하는 타임리프의 유틸리티이다.

이제 답변이 있는 질문은 다음처럼 제목 오른쪽에 빨간색(text-danger) 숫자가 작게(small) 왼쪽 여백(ms-2)이 추가되어 표시된다.

04_1
04_1

이찬희 (MarkiiimarK)
Never Stop Learning.