
레이아웃을 사용하는 이유
header와 footer가 고정적으로 들어가는 웹에서 반복되는 HTML 코드를 줄이기 위해서
layout을 사용하게 됩니다.
dependencie 추가
dependencies{
implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '2.4.1'
}
프로젝트 구조

fragments directory : 레이아웃에서 공통적으로 사용할 htmllayouts directory의 default_layout.html : 전체 layout 구조 세팅page directory : 각각 다른 페이지
<!-- header.html -->
<!doctype html>
<html lagn="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>Document</title>
</head>
<body>
<!-- 헤더 -->
<div th:fragment="header">
<h1>Header Section</h1>
</div>
</body>
</html>
<!-- footer.html -->
<!doctype html>
<html lagn="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title>Document</title>
</head>
<body>
<!--푸터-->
<div th:fragment="footer">
<h1>footer Section</h1>
</div>
</body>
</html>
xmlns:th="http://www.thymeleaf.org"th:fragment="fragment명"
<!-- default_layout.html -->
<!DOCTYPE html>
<html lagn="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>thymeleaf layout</title>
<meta content="width=device-width, maximum-scale=1.0, minimum-scale=1, user-scalable=yes,initial-scale=1.0"
name="viewport"/>
<!-- content script -->
<th:block layout:fragment="css"></th:block>
<!-- content script -->
<th:block layout:fragment="script"></th:block>
</head>
<body>
<!-- header fragment 사용 -->
<th:block th:replace="fragments/header :: header"></th:block>
<!-- content fragment 사용 -->
<th:block layout:fragment="content"></th:block>
<!-- footer fragment 사용 -->
<th:block th:replace="fragments/footer :: footer"></th:block>
</body>
</html>
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"를 추가<th:block layout:fragment="css"></th:block><th:block layout:fragment="script"></th:block><th:block th:replace="fragments/header :: header"></th:block><th:block th:replace="fragments/footer :: footer"></th:block><!DOCTYPE html>
<html lang="en" layout:decorate="~{layouts/default_layout}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 고유 CSS 추가 -->
<th:block layout:fragment="css">
<!-- <link rel="stylesheet" th:href="@{/css/page/home.css}" >-->
</th:block>
<!-- 고유 스크립트 추가 -->
<th:block layout:fragment="script">
<!-- <script th:src="@{/js/page/home.js}"></script>-->
</th:block>
</head>
<body>
<div layout:fragment="content">
<h1>홈페이지</h1>
</div>
</body>
</html>
layout:decorate="~{layouts/default_layout}"를 추가<th:block layout:fragment="css"><th:block layout:fragment="script"><div layout:fragment="content">