하나씩 차근차근
article thumbnail

앞서 글쓴이를 Controller 를 통해 데이터베이스에 저장했습니다.

이번에는 DB 에 저장한 글쓴이의 이름을 화면에 표시해보겠습니다.

 

1. Question_list

다음과 같이 Question_list 파일에 글쓴이를 표시할 수 있는 열을 작성합니다.

<html />
<html layout:decorate="~{layout}"> <div layout:fragment="content" class="container my-3"> <table class="table"> <thead class="table-dark"> <tr class="text-center"> <th>번호</th> <th>제목</th> <th>글쓴이</th> <th>작성일시</th> </tr> </thead> <tbody> <tr class="text-center" th:each ="question : ${paging}"> <td th:text="${question.id}"></td> <td class="text-start"> <a th:href="@{|/question/detail/${question.id}|}" th:text = "${question.subject}"></a> </td> <td><span th:if="${question.author != null}" th:text="${question.author.username}"></span> <td th:text = "${#temporals.format(question.createDate,'yyyy-MM-dd HH:mm')}"></td> </tr> </tbody> </table> ... </html>

타임리프의 ${question.author != null} 을 통해 글쓴이가 있을 경우에만 표시하도록 했습니다.

 

2. Question_detail

Question_detail 파일도 위와 동일하게 글쓴이가 있을 경우에 표시해줍니다.

<html />
<html layout:decorate="~{layout}"> ... <div clas="card my-3"> <div class="card-body"> <div class="card-text" style="white-space : pre-line;" th:text="${question.content}"></div> <div class="d-flex justify-content-end"> <div class="mb-2"> <span th:if="${question.author != null}" th:text="${question.author.username}"></span> </div> <div class="badge bg-light text-dark p-2 text-start"> <div th:text="${#temporals.format(question.createDate, 'yyyy-MM-dd HH')}"></div> </div> </div> </div> </div> ... </html>

Question_detail 페이지에서 보여지는 답변의 경우에도 답변에 작성자가 있을 경우 표시를 합니다.

<html />
<html layout:decorate="~{layout}"> ... <div class="card my-3" th:each = "answer : ${question.answerList}"> <div class="card-body"> <div class="card-text" style="white-space:pre-line;" th:text="${answer.content}"></div> <div class="d-flex justify-content-end"> <div class="mb-2"> <span th:if="${answer.author != null}" th:text="${answer.author.username}"></span> </div> <div class="badge bg-light text-dark p-2 text-start"> <div th:text="${#temporals.format(answer.createDate, 'yyyy-MM-dd HH:mm')}"></div> </div> </div> </div> </div> ... </html>

 

3. 결과

스프링부트를 실행해서 브라우저로 접속해보면 다음과 같이 글쓴이가 보입니다.

profile

하나씩 차근차근

@jeehwan_lee

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!