์ ํธ๋ฆฌํฐ ๊ฐ์ฒด(Utility Objects)?
ํ์๋ฆฌํ ํ
ํ๋ฆฟ์์ ์ฌ์ฉํ ์ ์๋ ์ ์ฉํ ๊ธฐ๋ฅ์ ์ ๊ณตํด์ฃผ๋ ๊ฐ์ฒด์
๋๋ค.
ํํ์์์ # ๋ค์ ์ค๋ ์ด๋ฆ์ผ๋ก ํธ์ถํฉ๋๋ค.
๊ฐ์ฒด๋ช
| ์ค๋ช
|
#dates | ๋ ์ง/์๊ฐ ํฌ๋งท ๋ฐ ์กฐ์ |
#strings | ๋ฌธ์์ด ์กฐ์ |
#numbers | ์ซ์ ํฌ๋งทํ
|
#calendars | Calendar ๊ด๋ จ ๊ธฐ๋ฅ |
#lists | List ์ปฌ๋ ์
๊ด๋ จ |
#sets | Set ์ปฌ๋ ์
๊ด๋ จ |
#maps | Map ์ปฌ๋ ์
๊ด๋ จ |
#bools | Boolean ๊ด๋ จ |
#ids | HTML id ์์ฑ |
#uris | URI ๋น๋ |
#vars | ์ง์ญ ๋ณ์ ๊ด๋ จ |
#objects | ๊ฐ์ฒด ๊ฒ์ฌ (null ๋ฑ) |
๋ด์ฅ ๊ฐ์ฒด (Built-in)
Thymeleaf ๋ด๋ถ์์ ๊ธฐ๋ณธ ์ ๊ณต๋๋ ๊ฐ์ฒด๋ค:
๊ฐ์ฒด | ์์ | ์ค๋ช
|
${#ctx} | ${#ctx.locale} | ์ ์ฒด ์ปจํ
์คํธ ์ ๋ณด |
${#http} | ${#httpServletRequest.contextPath} | HTTP ๊ด๋ จ ์ ๋ณด |
${#session} | ${#session.user} | ์ธ์
์ ๋ณด |
${#request} | ${#request.param.name} | Request ์ ๋ณด |
${#response} | - | Response ๊ด๋ จ |
์์ฃผ ์ฐ๋ ์ ํธ ์์
๋ฌธ์์ด ์ฒ๋ฆฌ โ #strings
<span th:text="${#strings.toUpperCase(user.name)}"></span>
HTML
๋ณต์ฌ
ํจ์ | ์ค๋ช
|
toUpperCase() | ๋๋ฌธ์ |
toLowerCase() | ์๋ฌธ์ |
contains(str, part) | ํฌํจ ์ฌ๋ถ |
substring(str, start, end) | ๋ถ๋ถ ๋ฌธ์์ด |
์ซ์ ํฌ๋งทํ
โ #numbers
<p th:text="${#numbers.formatDecimal(price, 2, 'POINT')}"></p>
HTML
๋ณต์ฌ
ํจ์ | ์ค๋ช
|
formatInteger() | ์ ์ ํฌ๋งท |
formatDecimal() | ์์์ ํฌ๋งท |
round() | ๋ฐ์ฌ๋ฆผ |
๋ ์ง/์๊ฐ โ #dates
<p th:text="${#dates.format(order.date, 'yyyy-MM-dd')}"></p>
HTML
๋ณต์ฌ
ํจ์ | ์ค๋ช
|
format(java.util.Date, pattern) | ๋ ์ง ํฌ๋งทํ
|
parse(string, pattern) | ๋ ์ง ํ์ฑ |
๋ฆฌ์คํธ/์ปฌ๋ ์
โ #lists, #sets, #maps
<ul>
<li th:each="item : ${#lists.sort(products, 'price')}">
<span th:text="${item.name}"></span>
</li>
</ul>
HTML
๋ณต์ฌ
๊ธฐ๋ฅ | ์ |
๋ฆฌ์คํธ ์ ๋ ฌ | #lists.sort(list, 'field') |
Set ๋ณํ | #sets.newHashSet(...) |
Map ๊ฐ ํธ์ถ | ${#maps.get(map, 'key')} |
์กฐ๊ฑด/๊ฐ์ฒด ๊ฒ์ฌ โ #objects, #bools
<p th:if="${#objects.nonNull(user)}">Hello!</p>
HTML
๋ณต์ฌ
ํจ์ | ์ค๋ช
|
nonNull(obj) | Null ์๋์ง |
isEmpty(obj) | ๋น์ด์๋์ง |
isTrue(expr) | Boolean ๊ฒ์ฌ |
ID ์ ์ผ ์์ฑ โ #ids
<input th:id="${#ids.next('input_')}" />
HTML
๋ณต์ฌ
URI ๋น๋ โ #uris
<a th:href="${#uris.uri('/posts/{id}', id=post.id)}">View</a>
HTML
๋ณต์ฌ
REST API, ๋งํฌ ๋น๋์ ์ ์ฉ
์ค์ ์์ โ ์ข
ํฉ
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Example List</title>
</head>
<body>
<h2 th:text="${#strings.toUpperCase('์ํ ๋ชฉ๋ก')}"></h2>
<table>
<tr><th>์ด๋ฆ</th><th>๊ฐ๊ฒฉ</th><th>๋ฑ๋ก์ผ</th></tr>
<tr th:each="p : ${products}">
<td th:text="${p.name}"></td>
<td th:text="${#numbers.formatDecimal(p.price,2,'POINT')}"></td>
<td th:text="${#dates.format(p.createdAt, 'yyyy-MM-dd')}"></td>
</tr>
</table>
</body>
</html>
HTML
๋ณต์ฌ
์ฃผ์ ๋ฒ์ฃผ
๋ฒ์ฃผ | ์ฃผ ์ฌ์ฉ ๊ธฐ๋ฅ |
๋ฌธ์์ด | ๋/์๋ฌธ์, ํฌํจ, ์๋ฅด๊ธฐ |
์ซ์ | ํฌ๋งท, ๋ฐ์ฌ๋ฆผ |
๋ ์ง | ํฌ๋งท, ํ์ฑ |
์ปฌ๋ ์
| ์ ๋ ฌ, ํ๋ ์ ๊ทผ |
๊ฐ์ฒด | null/empty ๊ฒ์ฌ |
URI/ID | ๋งํฌ ์์ฑ, ์ ๋ํฌ ID |



