[Spring + Thymeleaf] λ μ΄μμ ꡬ쑰 μ½κ² μ‘κΈ°
μΉ νλ«νΌμ μ μν΄λ³΄λ©΄, μ¬μ΄λ λ©λ΄μ μλ¨ ν€λλ κ³ μ λ μ±λ‘ κ°μ΄λ°μ μ½ν μΈ μμλ§ λ°λλ ꡬ쑰λ₯Ό μμ£Ό λ³Ό μ μλ€
μ΄μ²λΌ λ μ΄μμμ μΌκ΄μ±μ μ μ§νλ©΄μλ κ° νμ΄μ§μ μ½ν μΈ λ₯Ό ν¨μ¨μ μΌλ‘ κ΄λ¦¬νλ €λ©΄, Spring νλ‘μ νΈμμ Thymeleafμ λ μ΄μμ κΈ°λ₯μ νμ©νλ κ²μ΄ λ§€μ° μ μ©ν©λλ€. μ μ§λ³΄μμ νμ₯μ± μΈ‘λ©΄μμλ ν° μ₯μ μ κ°κ³ μλ€.
μ€μ νλ«νΌ νλ‘μ νΈλ₯Ό κ°λ°νλ©΄μ μ΄λ¬ν ꡬ쑰λ₯Ό μ μ©ν κ²½νμ΄ μμΌλ©°, κ³Όμ μ μ°μ΅ κ²Έ λΉμ·ν λ μ΄μμμ λ€μ μ€μ ν΄λ³΄λ κ³Όμ μμ λͺ κ°μ§ μ€μνμ§λ§ λμΉκΈ° μ¬μ΄ ν¬μΈνΈλ€μ λ°κ²¬νλ€
μ΄ ν¬μ€ν μμλ μ κ° μ§μ κ²ͺμ μνμ°©μ€λ₯Ό λ°νμΌλ‘, λ μ΄μμ μ€μ μ μ£Όμν μ κ³Ό ν΅μ¬ λ΄μ©μ 곡μ νκ³ μ ν©λλ€.
1. Thymeleaf μμ‘΄μ± μ£Όμ
Thymeleafλ Spring Bootμμ κ°μ₯ λ§μ΄ μ¬μ©λλ ν νλ¦Ώ μμ§ μ€ νλμ΄λ€. μ μ HTMLμ λμ μΌλ‘ λ λλ§ν΄μ£Όλ©°, λ·°(View)μ λ°μ΄ν°λ₯Ό μμ°μ€λ½κ² μ°κ²°ν μ μλλ‘ λμμ€λ€.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
2. Layout Dialect μμ‘΄μ± μΆκ°
Thymeleafλ‘ λ μ΄μμ ν νλ¦Ώμ μ€μ νλ €κ³ layout:decorateλ₯Ό μ°λλ° κ³μ μ μ©μ΄ μ λμλ€....
μ€μ β β β β
dependencies {
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.0.0'
}
3. λΆλͺ¨ ν νλ¦Ώ basic.html λ μ΄μμ μ€μ
basic.htmlμ λͺ¨λ νμ΄μ§μ κ³΅ν΅ κ΅¬μ‘°λ₯Ό μ μνλ λΆλͺ¨ λ μ΄μμμ΄λ€.
μ¦, ν€λμ μ¬μ΄λλ°λ κ³ μ μ΄κ³ , <main> λΆλΆμ μμ νμ΄μ§μμ μ½μ
λλ κ΅¬μ‘°λ‘ μ€μ νλ€.
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
.
.
<style>
.
.
</style>
</head>
<body>
<!-- μ’μΈ‘ μ¬μ΄λλ° -->
<div class="sidebar p-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link" th:href="@{/home}"><i class="bi bi-house"></i> ν</a>
</li>
<li class="nav-item">
<a class="nav-link" th:href="@{/list}"><i class="bi bi-list-ul"></i> κ²μν</a>
</li>
</ul>
</div>
<!-- ν€λ μμ -->
<div class="header">
.
.
</div>
<main class="main-content">
<div layout:fragment="home"></div>
<div layout:fragment="content"></div>
</main>
</body>
<div layout:fragment="script"></div>
</html>
4. Thymeleaf λ μ΄μμμμ κ°μ₯ μ€μν ν΅μ¬ ν¬μΈνΈ
<main class="main-content">
<div layout:fragment="home"></div>
<div layout:fragment="content"></div>
</main>
layout:fragmentλ λΆλͺ¨ ν
νλ¦Ώμμ μμ ν
νλ¦Ώμ΄ λ΄μ©μ "μ½μ
ν μμΉ"λ₯Ό μ μνλ ν΅μ¬ μ§μ μ΄λ€.
μ΄ μ΄λ¦κ³Ό μμ νμ΄μ§μ layout:fragment κ°μ΄ μ νν λ§€μΉλμ΄μΌ μ½ν
μΈ κ° μ μμ μΌλ‘ λ λλ§λλ€.
5. μμ νμ΄μ§ ꡬ쑰
β μμ νμ΄μ§ μ΅μλ¨ <html> νκ·Έμ μλμ κ°μ΄ μ§μ νμ¬ λΆλͺ¨ λ μ΄μμμ μμ λ°λλ€.
layout:decorate="~{layout/basic}"
β μμ νμ΄μ§μ <body> νκ·Έ μμμ layout:fragment=" fragment μ΄λ¦ " μμ±μΌλ‘ μμμ κ°μΈκ³ , κ·Έ μμ νμν λ΄μ©μ μμ±νλ€.
<div layout:fragment="content">
<!-- μμ νμ΄μ§μμ 보μ¬μ€ μ€μ λ΄μ© -->
</div>
β μ€μ μ½λ μμ
<!DOCTYPE html>
<html lang="ko"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{layout/basic}">
<head>
<meta charset="UTF-8">
<title>κ²μν</title>
<style>
.
.
</style>
</head>
<body>
<div layout:fragment="content">
<-- 보μ¬μ€ μ€μ λ΄μ© -->
</div>
</body>
</html>
6. Thymeleaf λ μ΄μμ κ΅¬μ± μ μμ νμ΄μ§ μμ± μ μ μμ¬ν
- λΆλͺ¨ λ μ΄μμ(basic.html)μλ <html>, <head>, <body> νκ·Έκ° ν¬ν¨λμ΄ μμ
- μμ νμ΄μ§μμλ μ½ν μΈ λ§ layout:fragmentλ‘ μμ±νλ©°, λ³λμ <body> νκ·Έ μμ±μ μλ΅νλ κ²μ΄ μΌλ°μ
- μμ νμ΄μ§μ <body> νκ·Έλ₯Ό ν¬ν¨ν κ²½μ°, λΆλͺ¨ λ μ΄μμκ³Ό μ€λ³΅λμ΄ μΆ©λμ΄ λ°μν μ μμΌλ―λ‘ κΆμ₯νμ§ μμ