처음 만드는 Spring Boot 메모장

list.html 메모 리스트

· 1 views

이제 컨트롤러를 통해 데이터베이스에서 메모 목록을 가져오도록 했으니, 화면에서 이 데이터를 실제로 출력해보겠습니다.

list.html 템플릿 파일에서 Thymeleaf 반복문을 사용해, 전달받은 메모 목록을 테이블 형태로 출력할 수 있습니다.

아래 코드는 memos라는 이름으로 전달된 리스트를 화면에 반복 출력하는 예시입니다.

▼ Code

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메모 목록</title>
<link rel="stylesheet" href="/css/main.css"​>
</head>
<body>
    <h1>메모 목록</h1>

    <table border="1">
        <tr>
            <th>번호</th>
            <th>제목</th>
            <th>작성일</th>
        </tr>
        <tr th:each="memo, stat : ${memos}">
          <td th:text="${stat.coun​t}">번호</td>
          <td th:text="${memo.titl​e}">제목</td>
          <td th:text="${#temporal​s.format(memo.reg_da​te, 'yyyy-MM-dd HH:mm:ss')}">작성일</td​>
        </tr>
    </table>
</body>
</html>

▼ Explain

<tr th:each="memo, stat : ${memos}">
memos 리스트를 반복하면서 각 항목을 memo라는 이름으로 사용합니다.
동시에 반복 상태를 담은 stat 객체도 함께 사용할 수 있습니다. (stat.count는 현재 반복 횟수)

  <td th:text="${stat.coun​t}">번호</td>
반복 횟수를 출력합니다.
stat.count는 1부터 시작하는 현재 순번을 의미합니다.

  <td th:text="${memo.titl​e}">제목</td>
각 메모 객체의 title 값을 출력합니다.
즉, 메모의 제목이 이 칸에 표시됩니다.

  <td th:text="${#temporal​s.format(memo.reg_da​te, 'yyyy-MM-dd HH:mm:ss')}">작성일</td​>
메모의 작성일(reg_date)을 yyyy-MM-dd HH:mm:ss 형식으로 포맷해서 출력합니다.
#temporals.format()은 Thymeleaf에 내장된 날짜 포맷 함수입니다.

댓글

아직 댓글이 없습니다.