๐ŸŒฟ

Thymeleaf ์น˜ํŠธ์‹œํŠธ

๊ธฐ๋ณธ ํ‘œํ˜„์‹(${...}, @{...}), th:text, th:each ๋ฐ˜๋ณต๋ฌธ, th:if ์กฐ๊ฑด๋ฌธ, ํผ ๋ฐ”์ธ๋”ฉ, ํ…œํ”Œ๋ฆฟ ์กฐ๊ฐ(th:replace), Spring Security ๋ฐ ์‹ค๋ฌด ํŒ ๋ ˆํผ๋Ÿฐ์Šค

--views
๋‹จ์ˆ˜:
#thymeleaf#spring-boot#template#html#web#frontend#server-side-rendering#java์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-09-27

1. ๊ธฐ๋ณธ ์„ ์–ธ & 5๋Œ€ ํ‘œํ˜„์‹ ๋ฌธ๋ฒ• (Syntax & Standard Expressions)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11Ubuntu 22.04 LTS, macOS 13+, Windows 11Java ๊ตฌ๋™ ๊ฐ€๋Šฅ OS
CPU2 ์ฝ”์–ด ์ด์ƒ4 ์ฝ”์–ด ์ด์ƒํ…œํ”Œ๋ฆฟ ํŒŒ์‹ฑ, ํ‘œํ˜„์‹ ํ‰๊ฐ€(SpEL) ๋ฐ HTML ๋ Œ๋”๋ง
๋ฉ”๋ชจ๋ฆฌ (RAM)2 GB ์ด์ƒ4 GB ์ด์ƒSpring Boot ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ํž™ ๋ฉ”๋ชจ๋ฆฌ
๋””์Šคํฌ (Storage)1 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„5 GB ์ด์ƒํ…œํ”Œ๋ฆฟ ์บ์‹œ ๋ฐ ์ •์  ๋ฆฌ์†Œ์Šค(CSS/JS)
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„JDK 17 LTS+, Spring Boot 3.xSpring Boot Starter Thymeleaf ์˜์กด์„ฑ๋ชจ๋˜ ์›น ๋ธŒ๋ผ์šฐ์ € (HTML5 ํŒŒ์‹ฑ)
<!DOCTYPE html>
<!-- Thymeleaf ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์„ ์–ธ (IDE ์ž๋™์™„์„ฑ ๋ฐ ๋ฌธ๋ฒ• ์ง€์›) -->
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>ํƒ€์ž„๋ฆฌํ”„ ๊ธฐ๋ณธ</title>
</head>
<body>
    <!-- [1] ๋ณ€์ˆ˜ ์‹ ${...} : ์ปจํ…์ŠคํŠธ(Model)์— ๋‹ด๊ธด ๋ณ€์ˆ˜ ์ ‘๊ทผ -->
    <p th:text="${user.name}">ํ™๊ธธ๋™</p>

    <!-- [2] ์„ ํƒ ๋ณ€์ˆ˜ ์‹ *{...} : th:object๋กœ ์ง€์ •๋œ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ํ•„๋“œ ์ถ•์•ฝ ์ ‘๊ทผ -->
    <div th:object="${user}">
        <p th:text="*{name}">์ด๋ฆ„</p>
        <p th:text="*{email}">์ด๋ฉ”์ผ</p>
    </div>

    <!-- [3] ๋ฉ”์‹œ์ง€ ์‹ #{...} : messages.properties ๋‹ค๊ตญ์–ด ๋ฉ”์‹œ์ง€ ํ‚ค ์กฐํšŒ -->
    <p th:text="#{welcome.message}">ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค</p>

    <!-- [4] ๋งํฌ URL ์‹ @{...} : ์ปจํ…์ŠคํŠธ ํŒจ์Šค ๋ฐ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์ž๋™ ์ธ์ฝ”๋”ฉ ๊ฒฐํ•ฉ -->
    <a th:href="@{/users/{id}(id=${user.id}, tab='profile')}">ํ”„๋กœํ•„ ๋ณด๊ธฐ</a>

    <!-- [5] ์กฐ๊ฐ ์‹ ~{...} : ํ…œํ”Œ๋ฆฟ ๋ ˆ์ด์•„์›ƒ ์ปดํฌ๋„ŒํŠธ(fragment) ์‚ฝ์ž… -->
    <div th:replace="~{fragments/header :: headerFragment}"></div>
</body>
</html>

2. ํ…์ŠคํŠธ ์ถœ๋ ฅ & ์†์„ฑ ๋ฐ”์ธ๋”ฉ (Text & Attribute Binding)

<!-- [1] HTML ์ด์Šค์ผ€์ดํ”„ ์ถœ๋ ฅ (th:text) vs ์ด์Šค์ผ€์ดํ”„ ํ•ด์ œ (th:utext - ํƒœ๊ทธ ํ•ด์„) -->
<p th:text="${rawHtml}"><b>๊ตต์€ ๊ธ€์”จ</b></p>  <!-- &lt;b&gt;๊ตต์€ ๊ธ€์”จ&lt;/b&gt; ๋กœ ์ถœ๋ ฅ -->
<p th:utext="${rawHtml}"><b>๊ตต์€ ๊ธ€์”จ</b></p> <!-- ์‹ค์ œ ๋ณผ๋“œ์ฒด HTML๋กœ ๋ Œ๋”๋ง (XSS ์ฃผ์˜!) -->

<!-- [2] ๋ฆฌํ„ฐ๋Ÿด ๋Œ€์ฒด ๋ฌธ๋ฒ• (|...|) : ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ์„ ๋”ฐ์˜ดํ‘œ์™€ + ์—†์ด ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑ -->
<span th:text="|์•ˆ๋…•ํ•˜์„ธ์š”, ${user.name}๋‹˜! ์ ‘์†๊ถŒํ•œ: ${user.role}|">์ธ์‚ฌ๋ง</span>

<!-- [3] HTML ํ‘œ์ค€ ์†์„ฑ ๋™์  ๋ฐ”์ธ๋”ฉ -->
<input type="text" th:value="${user.name}" />
<img th:src="@{/images/{file}(file=${user.profileImg})}" alt="ํ”„๋กœํ•„" />
<button th:disabled="${!user.isActive}">์ „์†ก</button> <!-- false๋ฉด disabled ์†์„ฑ ์ž์ฒด ์ œ๊ฑฐ -->

<!-- [4] ํด๋ž˜์Šค ์ถ”๊ฐ€ (th:classappend) -->
<div class="card" th:classappend="${user.isAdmin} ? 'border-primary' : ''">์นด๋“œ</div>

3. ์กฐ๊ฑด๋ฌธ & ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ (Conditionals: th:if & th:switch)

<!-- [1] th:if / th:unless (if๋Š” ์ฐธ์ผ ๋•Œ ํ‘œ์‹œ, unless๋Š” ๊ฑฐ์ง“์ผ ๋•Œ ํ‘œ์‹œ) -->
<!-- null, false, 0, ๋นˆ ์ปฌ๋ ‰์…˜ ๋“ฑ์€ ๋ชจ๋‘ false๋กœ ํ‰๊ฐ€๋จ -->
<span th:if="${user.age >= 20}" class="badge">์„ฑ์ธ</span>
<span th:unless="${user.age >= 20}" class="badge">๋ฏธ์„ฑ๋…„์ž</span>

<!-- [2] ๋‹ค์ค‘ ์กฐ๊ฑด ๋…ผ๋ฆฌ ์—ฐ์‚ฐ (and, or, not / !) -->
<div th:if="${user.isActive and not user.isBlocked}">์ •์ƒ ํšŒ์›</div>

<!-- [3] th:switch / th:case (๋‹ค์ค‘ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ) -->
<div th:switch="${user.role}">
    <span th:case="'ADMIN'" class="badge-admin">์ตœ๊ณ  ๊ด€๋ฆฌ์ž</span>
    <span th:case="'MANAGER'" class="badge-manager">์šด์˜์ž</span>
    <span th:case="*" class="badge-user">์ผ๋ฐ˜ ํšŒ์›</span> <!-- *๋Š” default(๊ธฐ๋ณธ๊ฐ’) -->
</div>

4. ๋ฐ˜๋ณต๋ฌธ (Iteration: th:each & Status)

<!-- th:each="๋‹จ์ผ์š”์†Œ๋ณ€์ˆ˜, ์ƒํƒœ๋ณ€์ˆ˜ : ${์ปฌ๋ ‰์…˜}" -->
<table>
    <thead>
        <tr>
            <th>No</th>
            <th>์ˆœ๋ฒˆ(Count)</th>
            <th>์ด๋ฆ„</th>
            <th>์ƒํƒœ</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user, stat : ${users}" th:class="${stat.even} ? 'table-even' : 'table-odd'">
            <td th:text="${stat.index}">0</td>      <!-- 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค -->
            <td th:text="${stat.count}">1</td>      <!-- 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์นด์šดํŠธ -->
            <td th:text="${user.name}">ํ™๊ธธ๋™</td>
            <td>
                <!-- stat ์ƒํƒœ ํ”„๋กœํผํ‹ฐ -->
                <span th:if="${stat.first}">[์ฒซ๋ฒˆ์งธ]</span>
                <span th:if="${stat.last}">[๋งˆ์ง€๋ง‰]</span>
                <span th:text="|์ด ${stat.size}๋ช… ์ค‘|">์ด ์ธ์›</span>
            </td>
        </tr>
    </tbody>
</table>

6. ํผ ๋ฐ”์ธ๋”ฉ & ์œ ํšจ์„ฑ ๊ฒ€์ฆ (Form Binding & Validation)

<!-- Spring MVC @ModelAttribute("userForm")์™€ ์—ฐ๋™ -->
<form th:action="@{/users/new}" th:object="${userForm}" method="post">
    <!-- 1. th:field="*{ํ•„๋“œ๋ช…}" : id, name, value๋ฅผ ์ž๋™ ๋™๊ธฐํ™” -->
    <div class="form-group">
        <label for="username">์‚ฌ์šฉ์ž๋ช…</label>
        <input type="text" th:field="*{username}" class="form-control"
               th:errorclass="field-error" /> <!-- ์—๋Ÿฌ ์กด์žฌ ์‹œ field-error ํด๋ž˜์Šค ์ž๋™ ์ถ”๊ฐ€ -->
        
        <!-- Bean Validation ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ -->
        <p th:if="${#fields.hasErrors('username')}" 
           th:errors="*{username}" class="error-msg">์ด๋ฆ„ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€</p>
    </div>

    <!-- 2. ๋ผ๋””์˜ค ๋ฒ„ํŠผ / ์ฒดํฌ๋ฐ•์Šค ๋ฐ”์ธ๋”ฉ (์„ ํƒ ์ƒํƒœ ์ž๋™ ์ฒดํฌ) -->
    <div class="form-group">
        <label th:each="role : ${roles}">
            <input type="radio" th:field="*{role}" th:value="${role.name()}" />
            <span th:text="${role.description}">์—ญํ• </span>
        </label>
    </div>

    <button type="submit">๊ฐ€์ž…ํ•˜๊ธฐ</button>
</form>

7. ๋ ˆ์ด์•„์›ƒ ์กฐ๊ฐ ๋ถ„ํ•  & ๋ชจ๋“ˆํ™” (Fragments: th:replace)

<!-- [1] ์กฐ๊ฐ ์ •์˜ ํŒŒ์ผ: templates/fragments/common.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <!-- ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š” ํ—ค๋” ์กฐ๊ฐ ์ •์˜ -->
    <header th:fragment="mainHeader(pageTitle)">
        <h1 th:text="${pageTitle}">๊ธฐ๋ณธ ํƒ€์ดํ‹€</h1>
        <nav><a th:href="@{/}">ํ™ˆ</a> | <a th:href="@{/about}">์†Œ๊ฐœ</a></nav>
    </header>

    <!-- ํ‘ธํ„ฐ ์กฐ๊ฐ ์ •์˜ -->
    <footer th:fragment="mainFooter">
        <p>&copy; 2026 LuckyTechWorld. All rights reserved.</p>
    </footer>
</body>
</html>
<!-- [2] ์กฐ๊ฐ ์‚ฌ์šฉ ํŒŒ์ผ: templates/index.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <!-- th:replace: ํ˜„์žฌ ํƒœ๊ทธ(div)๋ฅผ ์กฐ๊ฐ ๋‚ด์šฉ์œผ๋กœ ์™„์ „ํžˆ ๊ต์ฒด -->
    <div th:replace="~{fragments/common :: mainHeader('๋ฉ”์ธ ํŽ˜์ด์ง€')}"></div>

    <main>
        <h2>ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!</h2>
    </main>

    <!-- th:insert: ํ˜„์žฌ ํƒœ๊ทธ(div) ๋‚ด๋ถ€์— ์ž์‹ ์š”์†Œ๋กœ ์กฐ๊ฐ์„ ์‚ฝ์ž… -->
    <div th:insert="~{fragments/common :: mainFooter}"></div>
</body>
</html>

8. Spring Security ์—ฐ๋™ (Securing Thymeleaf)

<!-- build.gradle: implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' -->
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

    <!-- [1] ๋กœ๊ทธ์ธ ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ๋ถ„๊ธฐ -->
    <div sec:authorize="isAuthenticated()">
        <!-- ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž ์ด๋ฆ„ ์ถœ๋ ฅ -->
        <span sec:authentication="name">ํ™๊ธธ๋™</span>๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!
        <a th:href="@{/logout}">๋กœ๊ทธ์•„์›ƒ</a>
    </div>

    <div sec:authorize="isAnonymous()">
        <a th:href="@{/login}">๋กœ๊ทธ์ธ</a>
        <a th:href="@{/register}">ํšŒ์›๊ฐ€์ž…</a>
    </div>

    <!-- [2] ์—ญํ• (Role) ๋ฐ ๊ถŒํ•œ(Authority) ์ธ๊ฐ€ ์ œ์–ด -->
    <div sec:authorize="hasRole('ADMIN')">
        <a th:href="@{/admin/dashboard}">๊ด€๋ฆฌ์ž ์ฝ˜์†” ๋ฐ”๋กœ๊ฐ€๊ธฐ</a>
    </div>
</html>

9. ๊ธฐ๋ณธ ๋‚ด์žฅ ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ฐ์ฒด (Standard Utility Objects)

<!-- [1] #strings : ๋ฌธ์ž์—ด ์กฐ์ž‘ -->
<p th:if="${#strings.isEmpty(user.bio)}">์ž๊ธฐ์†Œ๊ฐœ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</p>
<p th:text="${#strings.toUpperCase(user.name)}">LUCKY</p>
<p th:text="${#strings.abbreviate(user.description, 20)}">๊ธด ๊ธ€ ์ค„์ž„๋ง...</p>

<!-- [2] #temporals : Java 8 LocalDateTime ๋‚ ์งœ/์‹œ๊ฐ„ ์„œ์‹ํ™” -->
<!-- thymeleaf-extras-java8time ํ•„์š” (Boot 3 ๊ธฐ๋ณธ ๋‚ด์žฅ) -->
<p th:text="${#temporals.format(post.createdAt, 'yyyy-MM-dd HH:mm:ss')}">2026-09-27 12:00:00</p>
<p th:text="${#temporals.day(post.createdAt)}">27</p>

<!-- [3] #numbers : ์ˆซ์ž ๋ฐ ํ™”ํ ํฌ๋งทํŒ… -->
<p th:text="${#numbers.formatDecimal(price, 0, 'COMMA', 2, 'POINT')}">1,250.00</p>
<p th:text="${#numbers.formatInteger(1234567, 3, 'COMMA')}">1,234,567</p>

<!-- [4] #lists : ์ปฌ๋ ‰์…˜ ํฌ๊ธฐ ๋ฐ ํฌํ•จ ์—ฌ๋ถ€ -->
<span th:text="|์ด ${#lists.size(items)}๊ฐœ์˜ ์ƒํ’ˆ|">์ด 5๊ฐœ</span>
<div th:if="${#lists.contains(user.roles, 'VIP')}">VIP ํ˜œํƒ ์ œ๊ณต</div>

10. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ธ๋ผ์ธ (th:inline=โ€œjavascriptโ€)

<script th:inline="javascript">
    /* [1] ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋ธŒ๋ผ์šฐ์ € JS ๊ฐ์ฒด/JSON์œผ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ์ž๋™ ์ง๋ ฌํ™” */
    const currentUser = /*[[${user}]]*/ null;
    console.log("๋กœ๊ทธ์ธ ์œ ์ €:", currentUser.name, currentUser.id);

    /* [2] ๋ฌธ์ž์—ด ์ด์Šค์ผ€์ดํ”„ ๋ฐ ๊ฐ’ ํ• ๋‹น */
    const serverMessage = /*[[${message}]]*/ "๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€";

    /* [3] ์ธ๋ผ์ธ ์กฐ๊ฑด์‹ ํ™œ์šฉ */
    /*[# th:if="${user.isAdmin}"]*/
    console.log("๊ด€๋ฆฌ์ž ๊ถŒํ•œ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰");
    /*[/]*/
</script>

11. Spring Boot application.yml ์„ค์ • (Configuration)

spring:
  thymeleaf:
    prefix: classpath:/templates/    # ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ ์ €์žฅ ๊ฒฝ๋กœ
    suffix: .html                    # ํŒŒ์ผ ํ™•์žฅ์ž
    mode: HTML                       # ํ…œํ”Œ๋ฆฟ ๋ชจ๋“œ (HTML5)
    encoding: UTF-8                  # ํŒŒ์ผ ์ธ์ฝ”๋”ฉ
    cache: false                     # โš ๏ธ ๋กœ์ปฌ ๊ฐœ๋ฐœ ์‹œ ํ…œํ”Œ๋ฆฟ ์‹ค์‹œ๊ฐ„ ๋ฐ˜์˜ (๋ฐฐํฌ ์‹œ์—๋Š” true)
    check-template-location: true    # ํ…œํ”Œ๋ฆฟ ๋””๋ ‰ํ† ๋ฆฌ ์กด์žฌ ์—ฌ๋ถ€ ์‹œ์ž‘ ์‹œ ํ™•์ธ
    servlet:
      content-type: text/html

12. ์‹ค๋ฌด ํŒ & ์ฃผ์˜์‚ฌํ•ญ (Best Practices)

# [1] ๋‚ด์ถ”๋Ÿด ํ…œํ”Œ๋ฆฟ (Natural Templates) ์›์น™
- Thymeleaf ํŒŒ์ผ์€ ์ˆœ์ˆ˜ HTML ๋ฌธ๋ฒ•์„ ์œ ์ง€ํ•˜๋ฏ€๋กœ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„ ์—†์ด ์ง์ ‘ ์—ด์–ด๋„ ๊นจ์ง€์ง€ ์•Š๊ณ  UI ํผ๋ธ”๋ฆฌ์‹ฑ ๊ฒ€์ˆ˜๊ฐ€ ๊ฐ€๋Šฅํ•จ.
- ์ •์  ๋””์ž์ด๋„ˆ ์ž‘์—…๋ฌผ๊ณผ ๋ฐฑ์—”๋“œ ํ…œํ”Œ๋ฆฟ์˜ ๊ฐ„๊ทน์„ ํš๊ธฐ์ ์œผ๋กœ ์ค„์—ฌ์คŒ.

# [2] ํƒ€์ž„๋ฆฌํ”„ ์ „์šฉ ์ฃผ์„ ๋ฌธ๋ฒ•
<!--/* ์ด ์ฃผ์„์€ ์„œ๋ฒ„ ๋ Œ๋”๋ง ์‹œ ๋ธŒ๋ผ์šฐ์ € HTML ์†Œ์Šค์—์„œ ์™„์ „ํžˆ ์‚ญ์ œ๋จ (๋ณด์•ˆ ๋ฐ ๋ฏผ๊ฐ ์ •๋ณด ๋ณดํ˜ธ) */-->
<!-- ์ด ์ฃผ์„์€ ์ผ๋ฐ˜ HTML ์ฃผ์„์œผ๋กœ ๋ธŒ๋ผ์šฐ์ € [ํŽ˜์ด์ง€ ์†Œ์Šค ๋ณด๊ธฐ]์— ๊ทธ๋Œ€๋กœ ๋…ธ์ถœ๋จ -->

# [3] ํƒ€์ž„๋ฆฌํ”„ SpEL ์ธ์ ์…˜ ๋ณด์•ˆ ์ฃผ์˜
- ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์„ th:inline์ด๋‚˜ th:text ํ‘œํ˜„์‹ ํŒŒ์‹ฑ ๋Œ€์ƒ์œผ๋กœ ๋™์  ์กฐ๋ฆฝํ•˜์ง€ ๋ง ๊ฒƒ.
- ํ•ญ์ƒ ์‚ฌ์ „ ๊ฒ€์ฆ๋œ Model Attribute ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•  ๊ฒƒ.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500