Thymeleaf로 tiles를 할 수 있는 thymeleaf layout dialect 설정은 다음과 같다.
1. build.gradle(pom.xml)에 자신에게 맞는 코드를 추가한다.
https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect
2. 폴더구조를 다음과 같이 설정한다.
fragments에 들어가는 파일명은 자신의 템플릿 구조에 따라 설정하면 된다.
여기서는 header(script, css 링크설정한 파일), footer, nav(상단 공통메뉴), layout(header, footer, nav 설정할 layout 파일)로만 설정하였다.
3. 자신에게 맞는 틀을 가지고 layout.html 구성한다.
layout.html은 상단에 xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 설정해준다.
작성방법은 다음과 같다.
<!--th:replace="layout 파일 경로 :: layout 파일 별칭"-->
<!--예) th:replace="fragments/nav :: nav_nav"-->
<th:block th:replace="fragments/nav :: nav_nav"></th:block>
<!--layout.html -->
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<th:block th:replace="fragments/header :: header_header"></th:block>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!--nav-->
<th:block th:replace="fragments/nav :: nav_nav"></th:block>
<!--end of nav-->
<!--content-->
<div th:layout:fragment="content"></div>
<!--end of content-->
<!--nav-->
<th:block th:replace="fragments/footer :: footer_footer"></th:block>
<!--end of nav-->
</div>
</div>
</div>
</body>
</html>
<!--footer.html -->
<!-- Footer -->
<footer class="sticky-footer bg-white" th:fragment="footer_footer">
<div class="container my-auto">
<div class="copyright text-center my-auto">
<span>Copyright © Your Website 2021</span>
</div>
</div>
</footer>
<!-- End of Footer -->
만약, 해당 구조 잡기가 힘들다면 https://github.com/pinkdoyaji 의 demo를 내려받아서 수정한다.
'Java > Spring' 카테고리의 다른 글
[Spring] Singleton (0) | 2023.02.03 |
---|---|
[Spring] IOC (0) | 2023.01.26 |
[Spring] DI (0) | 2023.01.23 |
[Spring] 정적 웹 페이지 vs 동적 웹 페이지 (0) | 2022.09.20 |
[Spring] 반복되는 코드 Lombok으로 줄여보기 (0) | 2019.08.03 |