ํ์๋ฆฌํ ๋ ์ด์์ ๋ค์ด์ผ๋ ํธ
โข
ํ์๋ฆฌํ ๋ ์ด์์ ๋ค์ด์ผ๋ ํธ ์์กด์ฑ
โฆ
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)
ํ์๋ฆฌํ ๋ ์ด์์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ ๋ถ๋ถ
ํ๋๊ทธ๋จผํธ ์ค์ตํ์ผ
โข
~/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
๋ณต์ฌ




