π·Thymeleaf - Getting started with the Standard dialects in 5 minutes
β 1. Standard dialects?
- Thymeleafλ μΌμ’
μ μλ° λΌμ΄λΈλ¬λ¦¬λ‘ , μ΄ν리μΌμ΄μ
μ μν΄ μμ±λ HTML, XML, JavaScript, CSS λ° μΌλ° ν
μ€νΈλ₯Ό νμνκΈ° μν΄ ν
νλ¦Ώ νμΌμ λ³νν΄μ£Όλ ν
νλ¦Ώ μμ§μ΄λ€
- Thymeleafλ νμ₯μ΄ μ©μ΄νκ³ μ¬μ©μκ° μνλ μ΄λ¦μΌλ‘ μμ λ§μ ν
νλ¦Ώ μμ±, νκ·Έλ₯Ό μ μνκ³
μνλ ꡬ문μμ μνλ ννμκ³Ό λ‘μ§μ μ μ©ν μ μλ€
β 2. Standard Expression syntax
- Thymeleafμμ μ¬μ©νλ ννμ
β ${...} : Variable expressions(λ³μ ννμ)
β *{...} : Selection expressions(μ ν ννμ)
β #{...} : Message (i18n) expressions(λ©μΈμ§ ννμ)
β @{...} : Link (URL) expressions(λ§ν¬(URL) ννμ)
β ~{...} : Fragment expressions (μ‘°κ° ννμ)
β 2.1 Variable expressions(λ³μ ννμ)
- λ³μ ννμμ OGNL ννμ (λλ Thymeleafλ₯Ό Springκ³Ό ν΅ν©νλ κ²½μ° Spring EL)μ΄λ©° 컨ν
μ€νΈ λ³μμμ μ€νλλ©° Spring μ©μ΄λ‘ model Attribute λΌκ³ λν©λλ€.
- ${session.user.name}
- <span th:text="${book.author.name}">
- ννμμ λ€μκ³Ό λμΌν©λλ€
- ((Book)context.getVariable("book")).getAuthor().getName()
-μΆλ ₯λΏλ§ μλλΌ μ‘°κ±΄, λ°λ³΅κ³Ό κ°μ λ³μ ννμμ μ°Ύμ μ μμ΅λλ€.
- <li th:each="book : ${books}">
- μ¬κΈ°μ $ {books}λ 컨ν
μ€νΈμμ booksλΌλ λ³μλ₯Ό μ ννκ³ th : each 루νμμ λ°λ³΅
β 2.2 Selection expressions(μ ν ννμ)
- μ ν ννμ μ ν ννμμ μ 체 컨ν
μ€νΈ λ³μ λ§΅ λμ μ΄μ μ μ νν κ°μ²΄μμ μ€νλλ€λ μ μ μ μΈνλ©΄ λ³μ ννμκ³Ό κ°μ΅λλ€.
- *{customer.name}
- κ·Έλ€μ΄ μλνλ κ°μ²΄λ th : object μμ±μ μν΄ μ§μ λ©λλ€
- <div th:object="${book}">
...
<span th:text="*{title}">...</span>
...
</div>
- { // th:object="${book}" final Book selection = (Book) context.getVariable("book"); // th:text="*{title}" output(selection.getTitle()); }
β 2.3 Message (i18n) expressions(λ©μΈμ§ ννμ)
- λ³μ ννμ. κ°μ²΄ ννμμΌλ‘ λ΄λ λλ€
- λ©μμ§ ννμ μ μ¬μ©νλ©΄ μΈλΆ μμ€ (.properties νμΌ)μμ λ‘μΌμΌ νΉμ λ©μμ§λ₯Ό κ²μνμ¬ ν€λ‘ μ°Έμ‘°νκ³ (μ νμ μΌλ‘) λ§€κ° λ³μ μΈνΈλ₯Ό μ μ© ν μ μμ΅λλ€..
- #{main.title}
- #{message.entrycreated(${entryId})}
- λ€μκ³Ό κ°μ ν
νλ¦Ώμμ μ°Ύμ μ μμ΅λλ€.
- <table>
...
<th th:text="#{header.address.city}">...</th>
<th th:text="#{header.address.country}">...</th>
...
</table>
- 컨ν
μ€νΈ λ³μμ κ°μΌλ‘ λ©μμ§ ν€λ₯Ό κ²°μ νκ±°λ λ³μλ₯Ό λ§€κ° λ³μλ‘ μ§μ νλ €λ κ²½μ° λ©μμ§ ννμ λ΄μμ λ³μ ννμμ μ¬μ©ν μ μμ΅λλ€.
- #{${config.adminWelcomeKey}(${session.user.name})}
β 2.4 Link (URL) expressions(λ§ν¬(URL) ννμ)
- λ§ν¬ ννμμ URLμ μμ±νκ³ μ μ©ν 컨ν
μ€νΈ λ° μΈμ
μ 보λ₯Ό μΆκ°νκΈ°μν κ²μ
λλ€(μΌλ°μ μΌλ‘ URL μ¬ μμ±μ΄λΌκ³ νλ νλ‘μΈμ€)
- <a th:href="@{/order/list}">...</a>
- <a href="/myapp/order/list">...</a>
- μΈμ
μ μ μ§ν΄μΌνκ³ μΏ ν€κ° νμ±νλμ§ μμ κ²½μ° (λλ μλ²κ° μμ§ μμ§ λͺ»νλ κ²½μ°)
- <a href="/myapp/order/list;sessionid=23fa31abd41ea093">...</a>
- URLμ λ§€κ° λ³μλ₯Ό μ¬μ©ν μλ μμ΅λλ€.
- <a th:href="@{/order/details(id=${orderId},type=${orderType})}">...</a>
- // κ²°κ³Ό
<!-- Note ampersands (&) should be HTML-escaped in tag attributes... -->
<a href="/myapp/order/details?id=23&type=online">...</a>
- λ§ν¬ ννμμ μλμ μΌ μ μμΌλ©°,μ΄ κ²½μ° μ ν리μΌμ΄μ
컨ν
μ€νΈκ° URL μμ λΆμ§ μμ΅λλ€
- <a th:href="@{../documents/report}">...</a>
- λν μλ²μ μλμ μ
λλ€ (λ€μ λ§νλ©΄ μ λμ¬κ° λΆμ μ ν리μΌμ΄μ
컨ν
μ€νΈ μμ).
- <a th:href="@{~/contents/main}">...</a>
- νλ‘ν μ½ κΈ°μ€ (μ λ URLκ³Ό λμΌνμ§λ§ λΈλΌμ°μ λ νμλλ νμ΄μ§μμ μ¬μ©λλ κ²κ³Ό λμΌν HTTP λλ HTTPS νλ‘ν μ½μ μ¬μ©ν¨)
- <a th:href="@{//static.mycompany.com/res/initial}">...</a>
- <a th:href="@{//static.mycompany.com/res/initial}">...</a>
β 2.5 Fragment expressions (μ‘°κ° ννμ)
- μ‘°κ° μμ νκ·Έ μ‘°κ°μ νννκ³ ν
νλ¦Ώμμ μ½κ² μ΄λν μμλ λ°©λ²μ
λλ€. μ΄λ¬ν μ λλΆμ μ‘°κ°μ 볡μ νκ³ λ€λ₯Έ ν
νλ¦Ώμ μΈμλ‘ μ λ¬ν μ μμ΅λλ€.
- κ°μ₯ μΌλ°μ μΈ μ©λλ th:insert or th:replace:
- <div th:insert="~{commons :: main}">...</div>
- κ·Έλ¬λ λ€λ₯Έ λ³μμ λ§μ°¬κ°μ§λ‘ μ΄λμλ μ¬μ©ν μ μμ΅λλ€.
- <div th:with="frag=~{footer :: #main/text()}">
<p th:insert="${frag}"></p>
</div>
β 2.6 Literals and operations
- λ€μν μ νμ 리ν°λ΄ λ° μ°μ°μ μ¬μ©ν μ μμ΅λλ€.
- 리ν°λ΄ :
ν
μ€νΈ 리ν°λ΄ : 'one text', 'Another one!', ...
μ«μ 리ν°λ΄ : 0, 34, 3.0, 12.3, ...
λΆμΈ 리ν°λ΄ : true,false
λ 리ν°λ΄ : null
리ν°λ΄ ν ν° : one, sometext, main, ...
- ν
μ€νΈ μμ
:
λ¬Έμμ΄ μ°κ²° : +
리ν°λ΄ λ체 : |The name is ${name}|
- μ°μ μ°μ° :
μ΄μ§ μ°μ°μ : +, -, \*, /,%
λΉΌκΈ° κΈ°νΈ (λ¨ν μ°μ°μ) : -
- λΆμΈ μ°μ° :
μ΄ν μ°μ°μ : and,or
λΆμΈ λΆμ (λ¨ν μ°μ°μ) : !,not
- λΉκ΅μ νλ± :
λΉκ΅κΈ° : >, <, >=, <=( gt, lt, ge, le)
κ°μ μ°μ°μ : ==, !=( eq, ne)
- μ‘°κ±΄λΆ μ°μ°μ :
κ·Έλ λ€λ©΄ : (if) ? (then)
κ·Έλ μ§ μμ κ²½μ° : (if) ? (then) : (else)
κΈ°λ³Έ: (value) ?: (defaultvalue)
β 2.7 Expression preprocessing
- μμ λν΄ μμμΌ ν λ§μ§λ§ μ¬νμ λ€μκ³Ό κ°μ΄ μ¬μ΄ μ μ§μ λ μ μ μ²λ¦¬ λΌλ κ²μ΄ μλ€λ κ²μ
λλ€ __.
- #{selection.__${sel.code}__}
β 3. Some basic attributes
- <p th:text="#{msg.welcome}">Welcome everyone!</p>
- <li th:each="book : ${books}" th:text="${book.title}">En las Orillas del Sar</li>
- <form th:action="@{/createOrder}"></form>
- <input type="button" th:value="#{form.submit}" />
- <a th:href="@{/admin/users}"></a>