[시간표 자동생성 서비스] 3. Spring boot Thymeleaf 도입하기

0
post-thumbnail

시간표 자동생성 서비스 깃허브 리포

하.....원래는 View를 React로 연동하려 했거늘....
속세에 나가서 해야할 것 같소...
참조 링크1
참조 링크2

1. dependency 추가


implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'

2. templates 폴더 생성


3. HTML 샘플 파일 설정


<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>아아아아</title>
    </head>
    <body>
        안녕안녕!!!!!
        
    </body>
</html>

4. application.properties 설정

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=true
spring.thymeleaf.order=0
spring.thymeleaf.enabled=true

logging.level.root=WARN
logging.level.com.baeldung=TRACE

5. Controller를 RestController Annotation에서 Controller로 변경


import org.springframework.stereotype.Controller;

@Controller

변경 이유 : https://mangkyu.tistory.com/49
이렇게 또 하나 배워갑니다...

6. bootRun



이거 하나 띄우기 위해서 5시간 삽질했다...

7. View Layout 분리


CommonHead.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="commonHead">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <meta charset="utf-8">
    <meta name="title" th:content="${metaTitle}">
    <title th:text="${metaTitle}"></title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
 
</th:block>
</html>
header.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<header th:fragment="header">
    <div>
        <h3>머리 머리 머리 머리</h3>
    </div>
</header>
</html>
footer.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="footer">
    <div>
        <h3>제작자: 루핑투핑</h3>
    </div>
</footer>
</html>
defaultLayout.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      lang="ko">
<head>
    <th:block th:replace="fragments/commonHead"></th:block>
</head>

<header th:replace="~{fragments/header :: header}" />

<body>
    <div>
        <!-- 각 화면 컨텐츠 -->
        <th:block layout:fragment="content" />
    </div>
</body>

<footer th:replace="fragments/footer :: footer" />
</html>
testSample.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/defaultLayout}"
      th:with="metaTitle='게시판'">
<head>
</head>

<body>
    
<th:block layout:fragment="content">
    아아아아아아아 테스트다아아아아
</th:block>
    
<th:block layout:fragment="content2">
    2222222 테스트다아아아아2
</th:block>
    

</body>
</html>

Controller 추가

@GetMapping("/test")
public String test() {      
    return "/views/testSample";
}

bootRun

0개의 댓글