01.Thymeleaf
Thymeleaf
๊ธฐ๋ณธ ๊ธฐ๋ฅ
์ฌ์ฉ ์ ์ธ
<html xmlns:th="http://www.thymeleaf.org"></html>์์ฑ ๋ณ๊ฒฝ
th:href="@{/css/bootstrap.min.css}"URL ๋งํฌ ํํ์
th:href="@{/css/bootstrap.min.css}" <!-- --> th:href="@{/basic/items/{itemId}(itemId=${item.id})}" th:href="@{/basic/items/{itemId}(itemId=${item.id}, query='test')}" th:href="@{|/basic/items/${item.id}|}"์์ฑ ๋ณ๊ฒฝ
th:onclick="|location.href='@{/basic/items/add}'|"๋ฐ๋ณต ์ถ๋ ฅ
<tr th:each="item : ${items}"></tr>๋ณ์ ํํ์
<td th:text="${item.price}">10000</td>์์ฑ ๋ณ๊ฒฝ
<input type="text" id="price" name="price" value="10000" th:value="${item.price}" />
๊ธฐ๋ณธ ํํ์
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax
ํ
์คํธ
ํ ์คํธ ์ถ๋ ฅ
๊ธฐ๋ณธ์ ์ผ๋ก escape ๋ฅผ ์ ๊ณต
escape : HTML ์์ ์ฌ์ฉํ๋ ํน์ ๋ฌธ์๋ฅผ HTML ์ํฐํฐ๋ก ๋ณ๊ฒฝํ๋ ๊ฒ
HTML ์ํฐํฐ :
<๋ฌธ์๋ฅผ ํ๊ทธ์ ์์์ด ์๋ ๋ฌธ์๋ก ํํํ๋ ๋ฐฉ๋ฒ
Unescape
th:text-->th:utext[[...]]-->[(...)]
SpringEL ํํ์
Object
${user.username}= userA${user['username']}= userA${user.getUsername()}= userA
List
${users[0].username}= userA${users[0]['username']}= userA${users[0].getUsername()}= userA
Map
${userMap['userA'].username}= userA${userMap['userA']['username']}= userA${userMap['userA'].getUsername()}= userA
Safe Navigation Operator
errors
?.์ errors ๊ฐ null ์ผ๋ NullPointerException ๋์ , null ์ ๋ฐํํ๋ ๋ฌธ๋ฒ ์ฐธ๊ณ
์ง์ญ๋ณ์
๊ธฐ๋ณธ ๊ฐ์ฒด
Thymeleaf ๊ธฐ๋ณธ ๊ฐ์ฒด
${#request}${#response}${#session}${#servletContext}${#locale}
ํธ์ ๊ฐ์ฒด
HTTP ์์ฒญ ํ๋ผ๋ฏธํฐ ์ ๊ทผ:
${param.paramData}HTTP ์ธ์ ์ ๊ทผ:
${session.sessionData}์คํ๋ง ๋น ์ ๊ทผ:
${@helloBean.hello('Spring!')}
์ ํธ๋ฆฌํฐ ๊ฐ์ฒด์ ๋ ์ง
#message : ๋ฉ์์ง, ๊ตญ์ ํ ์ฒ๋ฆฌ
#uris : URI ์ด์ค์ผ์ดํ ์ง์
#dates : java.util.Date ์์ ์ง์
#calendars : java.util.Calendar ์์ ์ง์
#temporals : ์๋ฐ8 ๋ ์ง ์์ ์ง์
#numbers : ์ซ์ ์์ ์ง์
#strings : ๋ฌธ์ ๊ด๋ จ ํธ์ ๊ธฐ๋ฅ
#objects : ๊ฐ์ฒด ๊ด๋ จ ๊ธฐ๋ฅ ์ ๊ณต
#bools : boolean ๊ด๋ จ ๊ธฐ๋ฅ ์ ๊ณต
#arrays : ๋ฐฐ์ด ๊ด๋ จ ๊ธฐ๋ฅ ์ ๊ณต
#lists , #sets , #maps : ์ปฌ๋ ์ ๊ด๋ จ ๊ธฐ๋ฅ ์ ๊ณต
#ids : ์์ด๋ ์ฒ๋ฆฌ ๊ด๋ จ ๊ธฐ๋ฅ ์ ๊ณต, ๋ค์์ ์ค๋ช
Reference
URL ๋งํฌ
๋จ์ URL
/hello
query parameter
/hello?param1=data1¶m2=data2
path variable
/hello/data1/data2
query parameter + path variable
/hello/data1?param2=data2
Reference
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
๋ฆฌํฐ๋ด
๋ฌธ์: 'hello' (๋ฌธ์ ๋ฆฌํฐ๋ด์ ํญ์ ์์๋ฐ์ดํ๋ก ๊ฐ์ธ์ผ ํจ)
์ซ์: 10
๋ถ๋ฆฐ: true , false
null: null
์ฐ์ฐ
์ฐ์ ์ฐ์ฐ
๋น๊ต ์ฐ์ฐ
์กฐ๊ฑด์
Elvis ์ฐ์ฐ์
No-Operation
์์ฑ ๊ฐ ์ค์
์์ฑ ์ค์
์์ฑ ์ถ๊ฐ
checked ์ฒ๋ฆฌ
๋ฐ๋ณต
๋ฐ๋ณต
์ํ ์ ์ง
index : 0๋ถํฐ ์์
count : 1๋ถํฐ ์์
size : ์ ์ฒด ์ฌ์ด์ฆ
even , odd : ํ/์ง์ ์ฌ๋ถ
first , last :์ฒ์/๋ง์ง๋ง ์ฌ๋ถ
current : ํ์ฌ ๊ฐ์ฒด
์กฐ๊ฑด๋ถ ํ๊ฐ
if, unless
switch
์ฃผ์
ํ์ค HTML ์ฃผ์
ํ์๋ฆฌํ๊ฐ ๋ ๋๋งํ์ง ์๊ณ ์ ์ง
ํ์๋ฆฌํ ํ์ ์ฃผ์
๋ ๋๋ง์์ ์ฃผ์ ๋ถ๋ถ์ ์ ๊ฑฐ (ํ์๋ฆฌํ ์ฃผ์)
ํ์๋ฆฌํ ํ๋กํ ํ์ ์ฃผ์
ํ์๋ฆฌํ ๋ ๋๋ง์ ๊ฑฐ์ณ์ผ๋ง ์ด ๋ถ๋ถ์ด ์ ์ ๋ ๋๋ง (HTML ์์๋ง ์ฃผ์ ์ฒ๋ฆฌ)
๋ธ๋ก
th:each ๋ก ํด๊ฒฐ์ด ์ด๋ ค์ธ ๋ ์ฌ์ฉ
JavaScript Inline
javascript inline
each
ํ
ํ๋ฆฟ ์กฐ๊ฐ
/resources/templates/template/fragment/footer.html/resources/templates/template/fragment/fragmentMain.html
ํ
ํ๋ฆฟ ๋ ์ด์์
๋์ ์ธ ๋ ์ด์์
/resources/templates/template/layout/base.html๋ ์ด์์์ด๋ผ๋ ํฐ ํ
/resources/templates/template/layout/layoutMain.htmlํ ์์ ํ์ํ ์ฝ๋ ์กฐ๊ฐ๋ค์ ์ ๋ฌ
::title ์ ํ์ฌ ํ์ด์ง์ title tag ๋ค์ ์ ๋ฌ
::link ์ ํ์ฌ ํ์ด์ง์ link tag ๋ค์ ์ ๋ฌ
๋ฉ์ธ ๋ ์ด์์
/resources/templates/template/layoutExtend/layoutFile.html๊ธฐ๋ณธ ๋ ์ด์์(header, footer) ํ์ ์ ์งํ๊ณ title, content ๋ง ๋ณ๊ฒฝ
/resources/templates/template/layoutExtend/layoutExtendMain.html๊ธฐ๋ณธ ๋ ์ด์์ ํ๋ก ๊ต์ฒดํ๋๋ฐ ํ๋๋ฐ title, content ๋ ์ ๋ฌ
Form
์
๋ ฅ ํผ ์ฒ๋ฆฌ
th:object: ์ปค๋งจ๋ ๊ฐ์ฒด๋ฅผ ์ง์ \*{...}: ์ ํ ๋ณ์ ์ (th:object ์์ ์ ํํ ๊ฐ์ฒด์ ์ ๊ทผ)th:field: HTML ํ๊ทธ์ id , name , value ์์ฑ์ ์๋์ผ๋ก ์์ฑ๋ ๋๋ง ์ /ํ
์ฒดํฌ ๋ฐ์ค
๋จ์ผ
Register
ํ์๋ฆฌํ๊ฐ ์๋์ผ๋ก
<input type="hidden" name="_open" value="on"/>์์ฑ์ฒดํฌ ๋ฐ์ค๋ฅผ ์ฒดํฌํ ๊ฒฝ์ฐ on ์ ์ ๋ฌํ์ง๋ง, ์ฒดํฌํ์ง ์์ ๊ฒฝ์ฐ ๊ฐ ์์ฒด๋ฅผ ์ ๋ฌํ์ง ์์ -> ์ด ๊ฒฝ์ฐ hidden type ์ _name input ํ๊ทธ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด, false ๋ก ๊ฐ์ ์ ๋ฌ (_open=on)
View
๋ฉํฐ
(์ฐธ๊ณ ) @ModelAttribute ๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋น Controller ํธ์ถ ์ regions() ์์ ๋ฐํํ ๊ฐ์ด ์๋์ผ๋ก Model์ ํญ์ ๋ด๊ธฐ๊ฒ ๋จ
Register
ํ์๋ฆฌํ๋ each๋ก ์ฒดํฌ๋ฐ์ค ์์ฑ ์ ๋์ ์ผ๋ก id์ ์๋ฒ์ ๋งค๊ฒจ์ค๋ค.
#ids๋ ๋์ ์ผ๋ก ์์ฑ๋ id๋ฅผ ์ธ์result
View
ํ์๋ฆฌํ๋
th:field์ ์ง์ ํ ๊ฐ๊ณผth:value์ ๊ฐ์ ๋น๊ตํด์ ์ฒดํฌ๋ฅผ ์๋์ผ๋ก ์ฒ๋ฆฌ
๋ผ๋์ค ๋ฒํผ
๋ผ๋์ค ๋ฒํผ์ ํญ์ ํ๋์ ๊ฐ์ด ์ ํ๋์ด์ผ ํ๋ฏ๋ก ํ๋ ๋ฒํผ์ ๋ฐ๋ก ์์ฑํ์ง ์์.
์
๋ ํธ ๋ฐ์ค
Register
Reference
Last updated