<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
spring.thymeleaf.prefix=classpath:templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.check-template-location=truespring.thymeleaf.cache=false
html xmlns 태그 추가
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Insert title here</title></head><body>searchUser Page<a href="javascript:getUserList();">click me</a></body></html>
for문과 유사하게 작동된다.
<table id="userTable"><thead><tr><th>No</th><th>Email</th><th>Name</th><th>CreateDate</th></tr></thead><tbody><tr th:each="user : ${userList}"><td th:text="${userStat.count}"></td><td th:text="${user.email}"></td><td th:text="${user.name}"></td><td th:text="${user.createStamp}"></td></tr></tbody></table>
또한, userStat 이라는 변수를 주어서 다음과 같은 필드를 사용할 수 있다.
<form action="#" th:action="@{/user/saveUserAction}" th:object="${user}" method="post" class="register"><div><label for="email">이메일</label><input type="text" th:field="*{email}" placeholder="test@naver.com" maxlength="20"/></div></form>
*{...}
를 써서 th:object
에 선언된 객체의 필드를 쓸 수 있다.
message.properties
에 정의된 변수를 보여줄 수 있다.
[참고 URL][ https://victorydntmd.tistory.com/340]
URL을 표현할 때 쓰는 표현식
<!-- Page-relative URL. 일반적인 상대 경로 URL --><a href="login.html" th:href="@{user/login.html}">login</a><!-- Context-relative URL. context name이 URL 앞에 자동으로 붙어서 생성된다. --><a href="login.html" th:href="@{/login.html}">login</a><!-- Server-relative URL. 서버의 다른 context에 접근할 수 있다. --><a href="login.html" th:href="@{~/other/login.html}">login</a><!-- Protocol-relative URL. --><a href="login.html" th:href="@{//www.other.com/login.html}">login</a><!-- '/gtvg/order/details?orderId=3'을 생성. URL 파라메터로 사용됨. --><a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a><!-- '/gtvg/order/3/details'를 생성. orderId를 local변수로 사용하여 URL path를 생성. --><a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
화면에 값을 출력할 때 사용한다. 문자를 결합도 가능하다.
<span> th:text="'Welcome to our app, ' + ${user.name} + '!'|"></span><span> th:text="|Welcome to our app, ${user.name}!|"></span>
form 태그의 th:object
로 지정한 객체와 같이 매핑되어 컨트롤러에서 @ResponseBody
로 객체로 받을 떄 사용할 수 있다.
조건문처럼 사용한다.
<div th:if="${false}"><p>에러 발생</p></div>
조건문처럼 사용한다.
<p th:errors=*{pwCheck}>비밀번호가 일치하지 않습니다.</p>
<div th:switch="${user.role}"><p th:case="'admin'">Administrator</p><p th:case="#{roles.manager}">manager</p><p th:case="*">Administrator</p></div>
태그를 제거할 때 사용한다.
form태그에서 th:action="#"
와 같이 사용한다.
<form action="#" th:action="@{/user/saveUserAction}" th:object="${user}" method="post" class="register"><div><label for="email">이메일</label><input type="text" th:field="*{email}" placeholder="test@naver.com" maxlength="20"/></div></form>