티스토리 뷰
thymeleaf
Thymeleaf는 Java 기반의 템플릿으로, Spring Boot 애플리케이션에서 널리 사용되는 뷰 기술입니다.
Thymeleaf는 HTML, XML, JavaScript, CSS 등의 템플릿 파일을 동적으로 렌더링하여 클라이언트에게 전달할 수 있습니다.
상단에 <html lang="en" xmlns:th="http://www.thymeleaf.org">를 작성해주어야 합니다.
<!-- 변수 표현식 -->
<p>Welcome, <span th:text="${username}">Guest</span>!</p>
<!-- 조건문 -->
<div th:if="${user.admin}">
<p>This is the admin panel.</p>
</div>
<!-- 반복문 -->
<ul>
<li th:each="product : ${products}" th:text="${product.name}"></li>
</ul>
<!-- 링크 처리 -->
<a th:href="@{/products/{id}(id=${product.id})}" th:text="${product.name}"></a>
<!-- 텍스트 처리 -->
<p th:text="${message}">This is a default message.</p>
<p th:utext="${htmlContent}">This is some HTML content.</p>
<!-- 레이아웃 -->
<!-- layout.html -->
<body>
<header th:fragment="header">
<h1>My Website</h1>
<nav>...</nav>
</header>
<div th:replace="~{content :: main-content}"></div>
<footer th:fragment="footer">
© 2023 My Website
</footer>
</body>
<!-- content.html -->
<div th:fragment="main-content">
<h2 th:text="${pageTitle}">Page Title</h2>
<p th:text="${pageContent}">Page content goes here.</p>
</div>
변수 표현식:${...}를 사용하여 모델 데이터에 접근할 수 있습니다.
조건문:th:if,th:unless속성을 사용하여 조건문을 작성할 수 있습니다.
반복문:th:each속성을 사용하여 컬렉션을 반복할 수 있습니다.
링크 처리:th:href,th:src속성을 사용하여 동적으로 URL을 생성할 수 있습니다
텍스트 처리:th:text,th:utext속성을 사용하여 텍스트를 출력할 수 있습니다.
레이아웃:th:fragment,th:replace,th:insert속성을 사용하여 레이아웃을 구성할 수 있습니다.
Thymeleaf 문법을 사용하면 동적으로 HTML 페이지를 생성할 수 있습니다. 서버에서 데이터를 처리하고, 이를 Thymeleaf 템플릿에 전달하여 클라이언트에게 렌더링할 수 있습니다.