-
ํ์๋ฆฌํ Thymeleaf์นดํ ๊ณ ๋ฆฌ ์์ 2022. 4. 18. 11:22
๐ก ํ์๋ฆฌํ?
- ์ปจํธ๋กค๋ฌ๊ฐ ์ ๋ฌํ๋ ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด ๋์ ์ผ๋ก ํ๋ฉด์ ๋ง๋ค์ด์ฃผ๋ ์ญํ ์ ํ๋ ๋ทฐ ํ ํ๋ฆฟ ์์ง
๐ ํ์๋ฆฌํ ํน์ง
- ์๋ฒ ์ฌ์ด๋ HTML ๋ ๋๋ง (SSR)
: ์๋ฒ๋ก๋ถํฐ ์์ ํ๊ฒ ๋ง๋ค์ด์ง htmlํ์ผ์ ๋ฐ์์ ํ์ด์ง ์ ์ฒด๋ฅผ ๋ ๋๋ง ํ๋ ๋ฐฉ์
- ๋ค์ธ๋ด ํ ํ๋ฆฟ
: ํ์๋ฆฌํ๋ก ์์ฑํ ํ์ผ์ HTML์ ์ ์งํ๊ธฐ ๋๋ฌธ์ ์น ๋ธ๋ผ์ฐ์ ์์ ํ์ผ์ ์ง์ ์ด์ด๋
ํ์ธํ ์ ์๊ณ , ์๋ฒ๋ฅผ ํตํด ๋ทฐ ํ ํ๋ฆฟ์ ๊ฑฐ์น๋ฉด ๋์ ์ผ๋ก ๋ณ๊ฒฝ๋ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๋ค.
: ์์ HTML์ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ๋ทฐ ํ ํ๋ฆฟ๋ ์ฌ์ฉ
- ์คํ๋ง ํตํฉ ์ง์
๐ก ์ฌ์ฉํ๊ธฐ
๐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
- Gradle -> build.gradle
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
- Maven -> pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
๐ ํ์๋ฆฌํ ์ฌ์ฉ ์ ์ธ
<html xmlns:th="http://www.thymeleaf.org">
๐ ๋ฌธ๋ฒ
๋ฌธ๋ฒ ์ญํ ์์ th:text ํ๊ทธ ์์ฑ์ ๋ฐ์ดํฐ ์ถ๋ ฅ th:text="${data}" ์ง์ ๋ฐ์ดํฐ ์ถ๋ ฅ [[${data}]] ${...} ๋ณ์ ์ฌ์ฉ ${data} th:with ์ง์ญ ๋ณ์ ์ ์ธ
(์ ์ธ์ ํ๊ทธ ์์์๋ง ์ฌ์ฉ๊ฐ๋ฅ)th:with="first=${user[0]}"
th:text="${first.username}"@{...} URL ์์ฑ th:href="@{/hello}" ' ' ๋ฌธ์ ๋ฆฌํฐ๋ด th:text="'hello world!'" | | ๋ฌธ์ ๋ฆฌํฐ๋ด th:text="|hello ${data}|" th:each ๋ฐ๋ณต th:each="user : ${users}"
th:text="${user.username}"
th:text="${user.age}"th:if
th:unless์กฐ๊ฑด์
์กฐ๊ฑด์ด false๋ฉด ํด๋น ๋ถ๋ถ ์์ฒด๋
๋ ๋๋ง ๋์ง ์๊ณ ์ฌ๋ผ์ง๋คth:if="${user.age lt 20}"
th:unless="${user.age ge 20}"th:inline="javascript" ์๋ฐ์คํฌ๋ฆฝํธ์์ ํ์๋ฆฌํ ์ฌ์ฉ <script th:inline="javascript">
</script>* ๋น๊ต์ฐ์ฐ
: > (gt), < (lt), >= (ge), <= (le), ! (not), == (eq), != (neq, ne)
๐ ์์ฑ ์ค์
- th:*๋ก ์์ฑ์ ์ ์ฉํ๋ฉด ๊ธฐ์กด ์์ฑ์ ๋์ฒดํ๋ค, ๊ธฐ์กด ์์ฑ์ด ์์ผ๋ฉด ์๋ก ๋ง๋ ๋ค
<input type="text" name="useA" th:name="cho"/> ๋ ๋๋ง ํ -> <input type="text" name="cho">
- checked ์ฒ๋ฆฌ
: checked ์์ฑ์ ๊ฐ๊ณผ ์๊ด์์ด checked๋ผ๋ ์์ฑ๋ง ์์ด๋ ์ฒดํฌ ๋๋ค => th:checked ์ฌ์ฉ
: th:checked="false"์ธ ๊ฒฝ์ฐ ์์ฑ ์์ฒด๋ฅผ ์ ๊ฑฐํ๋ค
<input type="checkbox" name="active" th:checked="false"/> -> <input type="checkbox" name="active"/>
๐ ์ฃผ์
- ํ์ค HTML ์ฃผ์
: <!-- <h2 th:text="${data}">html data</h2> -->
: ํ์๋ฆฌํ๊ฐ ๋ ๋๋ง ํ์ง ์๊ณ , ๊ทธ๋๋ก ๋จ๊ฒจ๋๋ค
- ํ์๋ฆฌํ ํ์ ์ฃผ์
: <!-- /* [[${data}]] */ -->
: ๋ ๋๋ง์์ ์ฃผ์ ๋ถ๋ถ์ ์ ๊ฑฐํ๋ค
- ํ์๋ฆฌํ ํ๋กํ ํ์ ์ฃผ์
: <!--/*/ <h2 th:text="${data}">html data</h2> /*/-->
-> ๋ ๋๋ง ํ <h2>Spring!</h2>
: HTML ํ์ผ์ ๊ทธ๋๋ก ์ด์ด๋ณด๋ฉด ์ฃผ์์ฒ๋ฆฌ๊ฐ ๋์ง๋ง, ํ์๋ฆฌํ๋ฅผ ๋ ๋๋ง ํ ๊ฒฝ์ฐ์๋ง ๋ณด์ธ๋ค
๐ก ํ์๋ฆฌํ ์คํ๋ง ํตํฉ
๐ ์ ๋ ฅ ํผ ์ฒ๋ฆฌ
- th:object
ex) th:object="${item}" => ์ ํ ๋ณ์ ์ *{...} ์ ์ฉํ ์ ์๋ค
- th:field
: HTMl ํ๊ทธ์ id, name, value ์์ฑ์ ์๋์ผ๋ก ์ฒ๋ฆฌํด์ค๋ค
<input type="text" th:field="*{itemName}"> -> <input type="text" id="itemName" name="itemName" value="">
* *{itemName}์ ${item.itemName}๊ณผ ๊ฐ๋ค
-> th:object๋ก item์ ์ ํํ๊ธฐ์ ์ ํ ๋ณ์ ์ ์ ์ฉํ ์ ์๋ค
๐ checkbox
- HTML checkbox๋ ์ ํ์ด ์๋๋ฉด ํด๋ผ์ด์ธํธ์๊ฒ ์๋ฒ๋ก ๊ฐ ์์ฒด๋ฅผ ๋ณด๋ด์ง ์๋๋ค
: ์ํ๋ ๊ฐ false => ํ๋ ํ๋ ์ฌ์ฉ(๋ฌด์กฐ๊ฑด ์ ์ก๋๋ค) <input type="hidden" name="_open" value="on"/>
-> _open์ด on์ธ๊ฑธ๋ณด๊ณ open์ด ์ฒดํฌ ์๋๊ฑธ ์ ์ ์์ open=false
- ํ์๋ฆฌํ๊ฐ ์ ๊ณตํ๋๊ฑธ ์ฌ์ฉํด๋ณด์
<input type="checkbox" id="open" th:field="*{open}">
=> ์๋์ผ๋ก ํ๋ ํ๋๋ฅผ ์์ฑํด์ค๋ค
- ๋ฐ๋ณต ๋๋ฆด๋ ๋ฉํฐ ์ฒดํฌ๋ฐ์ค์ id๊ฐ์ด ๊ฐ์ผ๋ฉด ์๋๋ค (name์ ์ค๋ณต ์๊ด์์)
=> ids.prev(...) ์ฌ์ฉํ๋ฉด ๋์ ์ผ๋ก ์์ฑ๋๋ id๊ฐ
* ๋ผ๋์ค ๋ฒํผ์ ์ด๋ฏธ ์ ํ๋์ด ์๋ค๋ฉด, ์์ ์์๋ ํญ์ ํ๋๋ฅผ ์ ํํ๋๋ก ๋์ด ์์ผ๋ฏ๋ก
์ฒดํฌ ๋ฐ์ค์ ๋ฌ๋ฆฌ ๋ณ๋์ ํ๋ ํ๋๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์๋ค