템플릿 상속하기

2026. 2. 7. 20:57·Spring/튜토리얼
반응형

1️⃣ 상속하는(부모) 템플릿

여기서 중요할 점은 2개다.

  • 부모 html 파일 이름, 여기서는 layout.html
  • <th:block ~ >  태그안에 있는 layout:fragment의 키값이다. 여기서는 content
<!-- layout.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Title</title>
    <!--부트스트랩 연결-->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <!-- 로컬 CSS 연결-->
    <link rel="stylesheet" th:href="@{/style.css}">
</head>
<body>

<!-- 상속받은 html 내용 -->
<th:block layout:fragment="content"></th:block>

<!--부트스트랩 연결-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
</body>
</html>

 

2️⃣ 상속받는(자식) 템플릿

  • html 태그, layout:decorate="~{상속받을 html파일}"을 넣어준다.
  • 위 th:block 태그의 layout:fragment 값과 실제 본문을 연결해준다.
<!DOCTYPE html>
<!-- layout.html을 상속받겠다. -->
<html lang="en" layout:decorate="~{layout}">
<head>
    <meta charset="UTF-8">
    <title>Hello Template</title>
</head>
<body>

<!-- layout.html의 fragment="content" 쪽에 삽입되겠다-->
<div layout:fragment="content" class="container my-3">
    <div class ="table">
        <table class="table">
            <thead class="table-dark">
            <tr>
                <th>번호</th>
                <th> 제목 </th>
                <th> 작성 일시</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="question, loop : ${questionList}">
                <td th:text="${loop.count}"></td>
                <td> <a th:href="@{|/question/detail/${question.id}|}" th:text="${question.subject}"></a></td>
                <td th:text="${#temporals.format(question.createDate, 'yyyy-MM-dd HH:mm')}"></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>

 


출처

반응형

'Spring > 튜토리얼' 카테고리의 다른 글

template에 css 적용하기 with 부트스트랩  (0) 2026.02.07
서비스 알아보기  (0) 2026.01.04
리다이렉트 하기  (0) 2026.01.04
Thymeleaf  (0) 2025.12.25
레포지토리  (0) 2025.12.06
'Spring/튜토리얼' 카테고리의 다른 글
  • template에 css 적용하기 with 부트스트랩
  • 서비스 알아보기
  • 리다이렉트 하기
  • Thymeleaf
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (325) N
      • CS (30)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (6)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (41)
        • swift (34)
        • python (6)
        • Kotlin (1)
      • iOS (133) N
        • UIKit (37)
        • Combine (1)
        • SwiftUI (33) N
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (11)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (20)
        • 어노테이션 (6)
        • 튜토리얼 (13)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
      • AI (9)
        • 이론 (9)
        • MCP (0)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    property
    CS
    SwiftUI
    boostcamp
    Swift
    투포인터
    IOS
    concurrency
    dp
    AVFoundation
    GIT
    lifecycle
    Tuist
    백준
    UIKit
    dispatch
    dfs
    프로그래머스
    Spring
    protocol
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
템플릿 상속하기
상단으로

티스토리툴바