TEMPLATE/THYMELEAF
Thymeleaf 사용법들
homoon
2023. 5. 4. 14:56
- 기본 출력 방법
// 선언
<html xmlns:th="http://www.thymeleaf.org">
// 방법1
[[${contents}]] // contents
// 방법2
<p th:text="${tit2}"></p> // tit2
// 방법3
<p th:text="|${내용1} [${내용2}]|"></p> // 내용1 [내용2]
2. 조건문
// if
<span th:if="${contents=='1'}">1</span>
<span th:if="${contents=='2'}">2</span>
// switch
<p th:switch="${contents > value}">
<span th:case="true">contents는 value보다 크다</span>
<span th:case="false">contents는 value보다 작다</span>
</p>
3. 반복문
<!-- 리스트 반복 -->
<tr th:each="content:${contents}">
<td>[[${content.a}]]</td>
<td th:text="${content.b}"></td>
</tr>
<!-- 숫자(for) 반복 -->
<th:block th:each="i : ${#numbers.sequence(start, end)}">
<a>반복 하는 내용</a>
</th:block>
4. 링크
<a th:href="@{/include}">include</a>
<a th:href="@{/layout}">layout</a>
// 주소 뒤에 ?key=value 입력 방법
// (key = value)
<a th:href="@{./modify(uid=${user.uid})}">수정</a>
<th:block th:include="/fragment/_header.html"/> //빈 블럭에 추가
<div th:insert="/fragment/_header.html"></div> // div 안에 추가
<div th:replace="/fragment/_header.html"></div> // div를 치환
5. form 태그
<form th:action="@{./modify}" th:object="${customer}" method="post">
// th:object => 전송 데이터를 담을 VO(DTO)
<input type="text" th:field="*{custId}" readonly="readonly">
// th:field
// *{} => *은 object를 뜻함
// 해당 오브젝트의 key값과 value값에 따라 {class, name}(key)그리고 th:value(value)값이
// 자동으로 설정 된다
6. 동적 클래스 추가
<a class="기본 클래스 이름" th:classappend="${조건문 ? '클래스 이름' : ''}"></a>
7. 이스케이프 처리 법
1. th:utext=”” ⇒ html 태그 분리해서 집어 넣기
2. [(${htmlTag})]