Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

공부하자

[Spring Boot] 7. 타임리프 (4) 본문

공부/Spring Boot

[Spring Boot] 7. 타임리프 (4)

strongstar 2018. 2. 1. 17:25

* 참고 도서 : 쇼다 츠야노 (2017). 스프링 부트 프로그래밍 입문. 길벗

* 이전 포스팅 : [Spring Boot] 6. 타임리프 (3)

 


1. 템플릿 프래그먼트

 

<tag th:fragment="fragmentName">

<tag th:include="templateName::fragmentName">

 

ex.

common.html 에 <nav th:fragment="header">..</nav> 기술.

index.html 에서 <nav th:include="common::header"></nav> 로 사용.

 

- part.html

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
<style th:fragment="frgStyle">
.frg {
    color: blue;    
}
</style>
</head>
<body>
 
    <h1>Fragment Sample</h1>
    
    <div th:fragment="frgDiv">
        <h2>frg</h2>
        <p class="frg">frg message..</p>
    </div>
    
</body>
</html>
cs

 

- index.html

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
<style th:include="part::frgStyle"></style>
</head>
<body>
 
    <h1>Strongstar Tistory</h1>
    
    <div th:include="part::frgDiv">
        <h2>index</h2>
        <p>index message..</p>
    </div>
    
</body>
</html>
cs

 

- TestController.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.star.springboot;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class TestController {
    
    @RequestMapping("/")
    public ModelAndView index(ModelAndView mav) {
        mav.setViewName("index");
        return mav;
    }
    
}
cs

 

- 폴더구조

 

 

- 실행 결과

 

 


 

책 기준으로 4.2장까지 끝났습니다.

그리고 한동안 스프링부트 관련 포스팅은 하지 않을 예정입니다.

 

Comments