SpringBoot
[SpringBoot] Thymeleaf 사용
꼬스차
2021. 4. 19. 09:46
Spring Boot에 Thymeleaf 템플릿 엔진을 적용해 보겠습니다.
Thymeleaf 란?
컨트롤러가 전달하는 데이터를 이용하여 HTML을 꾸밀수 있도록하는 뷰 템플릿(View Template)입니다.
Thymeleaf 연동 및 설정
Dependency 추가
Gradle의 경우 build.gradle
dependencies { ... implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' ... }
Maven의 경우 pom.xml
<dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ... </dependencies>
src/main/resources/application.properties
# Thymeleaf spring.thymeleaf.cache=false
아래 파일들 생성
src/main/java/com/td/controller/TodoController.java
package com.td.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TodoController { @GetMapping("/todo") public void todo(Model model) { model.addAttribute("title", "Todo!!"); } }
src/main/resources/templates/sample.html
<html xmls:th="http://www.thymeleaf.org"> <head> <title>Thymeleaf3</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1 th:text="${title}">Todo View Page</h1> </body> </html>
Run as - Spring Boot App
localhost:8080/sample 접속