νμ리ν λ μ΄μμ λ€μ΄μΌλ νΈ
β’
νμ리ν λ μ΄μμ λ€μ΄μΌλ νΈ μμ‘΄μ±
β¦
build.gradle
β’
νλκ·Έλ¨ΌνΈ
β’
λ μ΄μμ
νμ리ν λ μ΄μμ λ€μ΄μΌλ νΈ μμ‘΄μ±
// νμ리ν λ€μ΄μΌλ νΈ λ μ΄μμ
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
Java
볡μ¬
build.gradle
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.aloha'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// νμ리ν λ€μ΄μΌλ νΈ λ μ΄μμ
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
}
tasks.named('test') {
useJUnitPlatform()
}
Java
볡μ¬
νλκ·Έλ¨ΌνΈ (Fragment)
νμ리ν λ μ΄μμμ 곡ν΅μ μΌλ‘ μ¬μ©ν λΆλΆ
UI μμ 곡ν΅μ μΌλ‘ μ¬μ©λλ κ΅¬μ± μμλ€μ λ³λμ HTML νμΌλ‘ λ§λ λ€. μ£Όλ‘ ν€λ, νΈν°, μ¬μ΄λλ° λ±μ κ΄λ¦¬ν μ μμ΅λλ€.
νλκ·Έλ¨ΌνΈ μ€μ΅νμΌ
β’
~/fragment/header.html
β’
~/fragment/footer.html
β’
~/fragment/link.html
β’
~/fragment/script.html
~/fragment/header.html
<!-- νλκ·Έλ¨ΌνΈ : λ μ΄μμμμ 곡ν΅μ μΌλ‘ μ¬μ©ν UI -->
<!-- th:fragment="νλκ·Έλ¨ΌνΈ λͺ
" -->
<th:block th:fragment="header">
<header>
<ul>
<li><a href="/user">μ¬μ©μ</a></li>
<li><a href="/admin">κ΄λ¦¬μ</a></li>
</ul>
<ul>
<li><a href="/login">λ‘κ·ΈμΈ</a></li>
<li><a href="/join">νμκ°μ
</a></li>
</ul>
</header>
</th:block>
HTML
볡μ¬
~/fragment/footer.html
<th:block th:fragment="footer">
<footer>
<p>copyright ALL RIGHT RESERVED</p>
</footer>
</th:block>
HTML
볡μ¬
~/fragment/link.html
<th:block th:fragment="link">
<link rel="stylesheet" href="/css/style.css">
</th:block>
HTML
볡μ¬
~/fragment/script.html
<th:block th:fragment="script">
<!-- js κ°μ Έμ€λ μμ-->
<!-- jQuery -->
<!-- jQuery κΈ°λ° λΌμ΄λΈλ¬λ¦¬ -->
<!-- μλ°μ€ν¬λ¦½νΈ λΌμ΄λΈλ¬λ¦¬ -->
<!-- μ¬μ©μ μ μ μλ°μ€ν¬λ¦½νΈ -->
<script src="/js/script.js" ></script>
</th:block>
HTML
볡μ¬
λ μ΄μμ (Layout)
νΉμ νμ΄μ§μ UIλ₯Ό νλμ ν
νλ¦ΏμΌλ‘ 미리 κ΅¬μ± ν΄λμ μμ
λ μ΄μμ ν
νλ¦Ώμ νλκ·Έλ¨ΌνΈλ‘ ꡬμ±νκ³ , κ° νμ΄μ§λ§λ€ λ€λ₯΄κ² 보μ¬μ€ 컨ν
μΈ μμμ μ§μ ν΄μ 미리 μ§μ¬μ§ λ μ΄μμμ λ§λ€κΈ° μν΄ μ¬μ©νλ μμ
~/layout/main_layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- link -->
<th:block th:replace="~{fragment/link::link}"></th:block>
</head>
<body>
<!-- header -->
<th:block th:replace="~{fragment/header::header}"></th:block>
<!-- β content -->
<!-- layout:fragment="λ μ΄μμμ νλ κ·Έλ¨ΌνΈ μμ μ΄λ¦" -->
<th:block layout:fragment="content"></th:block>
<!-- footer -->
<th:block th:replace="~{fragment/footer::footer}"></th:block>
<!-- script -->
<th:block th:replace="~{fragment/script::script}"></th:block>
</body>
</html>
HTML
볡μ¬
index.html
μ¬κΈ°μλ νλκ·Έλ¨ΌνΈλ₯Ό κ°μ Έμμ κ³΅ν΅ λ μ΄μμμ μ μ©ν©λλ€.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<th:block th:replace="~{fragment/link::link}"></th:block>
</head>
<body>
<!-- th:replace : νλκ·Έλ¨ΌνΈλ₯Ό κ°μ Έμ€λ μμ± -->
<!-- th:replace="~{ν΄λλͺ
/νμΌλͺ
::νλκ·Έλ¨ΌνΈλͺ
}" -->
<th:block th:replace="~{fragment/header::header}"></th:block>
<div class="container">
<h3>CONTENT</h3>
</div>
<th:block th:replace="~{fragment/footer::footer}"></th:block>
<th:block th:replace="~{fragment/script::script}"></th:block>
</body>
</html>
HTML
볡μ¬
main.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{/layout/main_layout}"
>
<!-- β layout:decorate="~{/λ μ΄μμ κ²½λ‘/λ μ΄μμ νμΌλͺ
}" -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>λ©μΈ νλ©΄</title>
</head>
<!-- β layout:fragment="λ μ΄μμ νλ κ·Έλ¨ΌνΈ μ΄λ¦" -->
<body layout:fragment="content">
<div class="container">
<h3>CONTENT</h3>
</div>
</body>
</html>
HTML
볡μ¬
style.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
color: black;
text-decoration: none;
}
ul { list-style-type: none; }
header {
height: 80px;
background-color: lightslategray;
display: flex;
justify-content: space-between;
align-items: center;
}
header ul {
display: flex;
height: 100%;
padding: 0 20px;
}
header ul li { min-width: 100px; }
header ul li a {
display: inline-block;
min-width: 120px;
line-height: 80px;
text-align: center;
color: white;
}
header ul li a:hover {
font-weight: bold;
}
.container {
border: 1px solid black;
max-width: 1024px;
min-height: 300px;
margin: 0 auto;
padding: 12px;
}
footer {
background-color: black;
color: white;
min-height: 100px;
text-align: center;
}
footer p {
line-height: 100px;
}
CSS
볡μ¬