-
Spring boot project - templates layout 나누기개발 공부/Spring 2022. 1. 26. 11:32반응형
페이지를 header, footer, content로 나눠서 출력하기!
탑 메뉴(=header)나 하단바(=footer)를 모든 페이지에서 같이 쓸 때 사용하기 좋음
footer.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <div class="footer"> <h2>Footer</h2> </div> </html>
header.html
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <div class="header"> <h2>Header</h2> </div> </html>
home.html (= content)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layout/default_layout}"> <th:block layout:fragment="head"> <title>Spring Boot</title> <!--/* css */--> <link th:href="@{/bootstrap/css/common.css}" rel="stylesheet" /> </th:block> <body> <th:block layout:fragment="header" th:include="@{/fragments/header}"></th:block> <div layout:fragment="content" class="content"> <h2>This is Content</h2> </div> <th:block layout:fragment="footer" th:include="@{/fragments/footer}"></th:block> </body> </html>
controller
package com.example.test.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class testController { @RequestMapping("") public String main(Model model) { //model.addAttribute("data", "hello ~!"); return "index"; } @GetMapping("home") public String home() { return "content/home"; } }
http://localhost:8080/home 으로 접속하면
해당 페이지 출력가능!
반응형'개발 공부 > Spring' 카테고리의 다른 글
Spring layout form submit 안될 때 해결 법 (0) 2022.03.14 [Spring boot project] Bootstrap(부트스트랩) 적용하기 (0) 2022.01.28 [spring boot project] Controller에서 html view로 값 전달하기 (0) 2022.01.18 [Spring Boot Project] 기본 구조, Hello World 출력 , controller에서 html 호출 (0) 2022.01.18 [STS-Spring] Run Error (0) 2022.01.13