티스토리 뷰

카테고리 없음

thymeleaf

eyoadgkn 2024. 6. 12. 09:00
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">
    &copy; 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 템플릿에 전달하여 클라이언트에게 렌더링할 수 있습니다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함