

@Controller
@RequestMapping("/sample")
@Log4j2
public class SampleController {
@GetMapping("/ex1")
public String ex1() {
return "/sample/ex1";
}
/*
@GetMapping("/ex1")
public void ex1() {
log.info("ex1..............");
}
*/
}
🚀 thymeleaf의 기본 사용법 1
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<style>
.target {
background-color: orangered;
}
</style>
<ul>
<li th:each="dto, state : ${list}" th:class="${dto.sno % 5 == 0} ? 'target'">
[[${state.index}]] --- [[${dto}]] --- [[${state.count}]]
</li>
<br>
<th:block th:each="dto, state : ${list}">
<li th:class="${dto.sno % 5 == 0} ? 'target'">
[[${state.index}]] --- [[${dto}]] --- [[${state.count}]]
</li>
</th:block>
</ul>
</body>
</html>
xmlns:th="http://www.thymeleaf.org"
jsp에서 처럼 스크립트릿이나 jstl로 인하여 기본적인 html의 구조가 깨지는 경우가 거의 없다.
th:text, th:value 속성으로 태그에 글자를 넣을 수도 있고, [[ ]]도 이용가능하다.
태그 '안에' th:each, th:if등을 이용할 수 있고 th:class에 삼항연산자를 결합하여 class속성 부여할지 말지도 결정할 수 있다.
반복문에 변수 하나를 더 지정할 수 있는데, 상태 객체라 하여 순번과 관련된 기능들을 이용할 수 있다.
유용한 th:block : 화면에서 태그로 생성되지 않고 루프 처리 같은 용도로 사용된다.
🚀 thymeleaf의 기본 사용법 2
<li th:each="dto : ${list}" th:if="${dto.sno < 10}">
<a th:href="@{/sample/exView(sno=${dto.sno})}">[[${dto}]]</a>
</li>
<br/>
<li th:each="dto : ${list}" th:if="${dto.sno >= 10}">
<a th:href="@{/sample/exView/{sno}(sno=${dto.sno})}">
[[${dto}]]---[[ ${#temporals.format(dto.regTime, 'yyyy/MM/dd')}]]
</a>
</li>
위의 li태그는 리스트를 돌며 sno가 10미만인 dto들에게 링크를 거는 것이다.
이렇게 링크를 처리하면 아래 그림처럼 나온다. (쿼리스트링)



🚀 thymeleaf의 기본 사용법 3 - include 처리
- 다음 장에서 사용될 예제를 만든다.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="setContent(content)">
<head>
...
</head>
<body>
...
<div class="container-fluid">
<th:block th:replace="${content}"></th:block>
</div>
...
</body>
</th:block>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<th:block th:replace="~{layout/basic :: setContent(~{this::content})}">
<th:block th:fragment="content">
<h1>exSidebar Page</h1>
</th:block>
</th:block>
th:replace 속성은 어떤 조각(fragment)을 가져오는 것이다.
어떤 경로의 html파일 중 th:fragment 속성이 있는 것을 지칭할 수 있다.
html파일 자체를 가지고 올 수도 있다.
파라미터로 받아올 수도 있다.
exSidebar에 content라는 이름의 조각이 있다.
해당 조각을 basic.html에 넘겨줌과 동시에
exSidebar 내용을 content를 준 basic으로 갈아버린다.
그 basic에는 content를 가지고 있다.
멋지게 만든 레이아웃에 content를 넣는다.