<html xmlns:th="http://www.thymeleaf.org">
</html>
<!--타임리프-->
<span th:text="${data}"></span>
<span> [[${data}]]</span>
<!--렌더링 결과-->
<span>data 어트리뷰트에 할당된 내용</span>
<span>data 어트리뷰트에 할당된 내용</span>
<
>
는 HTML엔티티로 렌더링 된다
<!--타임리프-->
<span th:text="${data}"></span>
<span> [[${data}]]</span>
<span th:utext="${data}"></span>
<span> [(${data})]</span>
<!--어트리뷰트로 "<b>Hello</b>이 있을 떄 렌더링 결과-->
<span><b>Hello</b></span>
<span><b>Hello</b></span>
<span><b>Hello</b></span>
<span><b>Hello</b></span>
SpringEL
표현식을 사용하여 변수를 표현할 수 있음${data}
Object의 프로퍼티 접근법
member
의 id
필드를 조회하는 표현식List 컬렉션의 요소 접근
list
의 0번째 요소
조회하는 표현식Map 컬렉션의 밸류 접근
map
의 memberA
의 밸류를 조회하는 표현식th:with
로 지역변수 선언HttpServletRequest | ${#request} |
HttpServletResponse | ${#response} |
HttpSession | ${#session} |
ServletContext | ${#sevletContext} |
Locale | ${#locale} |
파라미터 접근
HttpServletRequest
객체에서 getParameter()로 접근해야하 함세션 접근
*스프링빈 접근
href, src, ...
같은 속성에 들어갈 URL 생성@{url/{경로변수}(쿼리 파라미터 or 경로 변수)}
<!-- 파라미터 : "param1"="aaa", "param2"="bbb" -->
<a th:href="@{/hello}"></a>
<!--href=/hello"-->
<a th:href="@{/hello(param1=${param1}, param2=${param2})}"></a>
<!--href=/hello?param1=aaa¶m2=bbb"-->
<a th:href="@{/hello/{param1}/{param2}(param1=${param1}, param2=${param2})}"></a>
<!--href=/hello/aaa/bbb"-->
<li><a th:href="@{/hello/{param1}(param1=${param1}, param2=${param2})}"></a>
<!--href=/hello/aaa?param2=bbb"-->
'~'(작은 따옴표)
를 통해서 나타타냄|~|
를 사용<!-- 파라미터 : "param1" = "AAA">
<span th:text="aaa"></span> <!-- aaa -->
<span th:text="aaa bbb"></span> <!-- 에러 -->
<span th:text="'aaa bbb'"></span> <!-- aaa bbb -->
<span th:text="'aaa' + ${param1}"></span> <!-- aaa AAA -->
<span th:text="|aaa ${param1}|"></span> <!-- aaa AAA -->
비교 연산자
>
,<
와 같이 HTML 엔티티를 사용해야될 때 주의 필요조건식(3항 연산자)
Elvis 연산자
No-Operation
th
속성을 이용하여 속성을 새로 만들거나 대체th:each="파라미터1, 파라미터2 : ${컬렉션}"
${컬렉션}
List, Map(엔티티가 담김), Iterable 구현 객체, Enumeration 구현 객체, Array 의 요소를 하나씩 꺼냄파라미터1의 명 + Stat
으로 자동 생성th:if="${불리언 값을 나타내는 조건}"
true
면 태그를 렌더링하고false
면 태그를 렌더링하지 않음th:unless
는 그 반대th:switch="${판별할 값}"
th:case="값"
의 값과 일치할 떄 태그 렌더링th:case="*"
는 모든 케이스에 해당 안될 떄 표시타임리프의 주석
<!--/* 내용 */-->
<!--/*--> 내용 <!--*/-->
<!--/*/ 내용 /*/-->
<th:block></th:block>
타임리프에서 제공하는 가상(?)의 태그→ 입력
<th:block th:each="user : ${users}">
<li th:text="${user.username}"></li>
</th:block>
→ 결과
<li>hawn</li>
<li>dooboo</li>
<li>cookie</li>
<script>
태그 안에서 타임리프를 그대로 사용하면var name = [[${user.username}]];
//var name = "dooboo";가 아니라
//var name = dooboo;로 렌더링 되어 에러
var user = [[$user]];
//var user = Controller.User(username=dooboo, age=5);로 렌더링 되어 에러
<script th:inline="javascript">
를 적용내추럴 템플릿
가능<script th:inline="javascript">
var name = [[${user.username}]];
//var name = "dooboo";로 렌더링
var user = [[$user]];
//var user = {"username":"dooboo","age":5};
//JSON객체로 변환
var natural = /*[[${user}]]*/ "타임리프 미적용 시 이 문구로 렌더링"
[# th:each="user, userStat : ${users}]
var user[[${userStat.count}]] = [[${user}]];
[/]
//var user1 = {"username":"hwan","age":29};
//var user2 = {"username":"dooboo","age":5};
//var user3 = {"username":"cookie","age":6};
</script>
th:fragment
로 조각으로 선언th:fragment="header"
로 "header"라는 조각등록th:fragment="headerParam(param1, param2)"
파라미터 값을 포함하는 조각 등록th:replace="~{경로 :: 조각명(파라미터 값, 파라미터 값)}"
th:insert="~{경로 ::조각명}"
공통 레이아웃
이 되는 레이아웃 html을 작성각 컨텐츠
영역의 태그 작성~{::태그명}
태그를 파라미터로 전달<html th:fragment="layout (title, content)" xmlns:th="http://www.thymeleaf.org">
<head>
<title th:replace="${title}">레이아웃 타이틀</title>
</head>
<body>
<header>
헤더
</header>
<nav>
네비
</nav>
<h1>제목</h1>
<div th:replace="${content}">
<p>레이아웃 컨텐츠</p>
<p>각 페이지의 컨텐츠로 대체되는 영역</p>
</div>
<footer>
레이아웃 푸터
</footer>
</body>
</html>
<html th:replace="~{template/layoutExtend/layoutFile :: layout(~{::title},~{::section})}"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>새페이지</title>
</head>
<body>
<section>
<p>Hello World</p>
</section>
</body>
</html>
<html>
<head>
<title>새페이지</title>
</head>
<body>
<header>
헤더
</header>
<nav>
네비
</nav>
<h1>제목</h1>
<section>
<p>Hello World</p>
</section>
<footer>
레이아웃 푸터
</footer>
</body>
</html>
form 태그
에 th:object
를 통하여 커맨드 객체 지정th:field="*{필드명}"
로 필드 지정th:field
에서 필드명 오타 발생 시, 에러 발생으로 디버깅 장점@Getter
public class Member{
private String name;
private int age;
}
<!--렌더링 전-->
<form th:action th:object="${member}">
<label for="name">이름</label>
<input type="text" th:field="*{name}">
<label for="age">나이</label>
<input type="text" th:field="*{age}">
</form>
<!--렌더링 후-->
<form action="">
<label for="name">이름</label>
<input type="text" id="name" name="name" value="">
<label for="age">나이</label>
<input type="text" id="age" name="age" value="">
</form>
checkbox
체크하고 submit 시 값이 'on'으로 넘어감true
로 변환null
의 값이 있고,
필드명에 해당되는 파라미터가 없으면, 스프링이
false`로 변환th:field
사용하면 _필드명
인풋 히든 자동으로 렌더링<!--렌더링 전-->
<div th:each="pet : ${pets}">
<input type="checkbox" th:field="*{pets}" th:value="${pet.key}">
<label th:for="${#ids.prev('pets')}" th:text="${pet.value}">..</label>
</div>
<!--렌더링 후-->
<div>
<input type="checkbox" id="pets1" name="pets" value="DOG">
<input type="hidden" name="_pets" value="on">
<label for="pets1">강아지</label>
</div>
<div>
<input type="checkbox" id="pets2" name="pets" value="CAT">
<input type="hidden" name="_pets" value="on">
<label for="pets2">고양이</label>
</div>
<div>
<input type="checkbox" id="pets3" name="pets" value="ECT">
<input type="hidden" name="_pets" value="on">
<label for="pets3">기타</label>
</div>
<!--
아무 것도 체크 안할 시 : _pet=on에 의해서 pets=[] 빈배열 넘어감
-->
<!--렌더링 전-->
<div th:each="pet : ${pets}">
<input type="radio" th:field="*{pets}" th:value="${pet.key}">
<label th:for="${#ids.prev('pets')}" th:text="${pet.value}">..</label>
</div>
<!--렌더링 후-->
<div>
<input type="radio" id="pets1" name="pets" value="DOG">
<label for="pets1">강아지</label>
</div>
<div>
<input type="radio" id="pets2" name="pets" value="CAT">
<label for="pets2">고양이</label>
</div>
<div>
<input type="radio" id="pets3" name="pets" value="ECT">
<label for="pets3">기타</label>
</div>
<!--
라디오버튼은 _pet 히든태그 만들지 않음 체크 안할 시 null로 넘어감
-->
<!--렌더링 전-->
<select th:field="*{pets}">
<option value="">==반려동물==</option>
<option th:each="pet : ${pets}" th:value="${pet.key}" th:text="${pet.value}">..</option>
</select>
<!--렌더링 후-->
<select id="pets" name="pets">
<option value="">==반려동물==</option>
<option value="DOG">강아지</option>
<option value="CAT">고양이</option>
<option value="ECT">기타</option>
</select>