기본적인 형태는 th:action, th:each, th:text ... 등의 형태이다.
기존의 문법에 th:만 붙여주면 된다.
컨트롤러에서 5개의 글을 생성해 model로 값을 넘겨준다.
@Controller
public class HomeController {
@GetMapping("/article/list")
public String list(Model model) {
List<Article> articleList = new LinkedList<>();
for(long i = 1; i <= 10; i++) {
articleList.add(new Article(i, i + "번째 제목", i + "번째 내용", LocalDateTime.now()));
}
model.addAttribute("articleList", articleList);
return "article_list";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>article_list</title>
</head>
<body>
<h1 th:text="|글 개수 : ${#lists.size(articleList)}|"></h1>
<table>
<thead>
<tr>
<th>번호</th>
<th>제목</th>
<th>내용</th>
<th>작성일시</th>
</tr>
</thead>
<tbody>
<tr th:each="article, loop : ${articleList}">
<td th:if="${article != null}" th:text="${loop.count}"></td>
<td>
<a th:href="@{#}" th:text="${article.subject}"></a>
</td>
<td th:text="${article.content}"></td>
<!--아래와 같이 작성할 수도 있다.
<td>
<a th:href="@{#}">[[${article.subject}]]</a>
</td>
<td>[[${article.content}]]</td>
-->
<td th:text="${#temporals.format(article.createDate, 'yyyy-MM-dd HH:mm')}"></td>
</tr>
</tbody>
</table>
</body>
</html>