728x90
반응형
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 접속
728x90
반응형
'SpringBoot' 카테고리의 다른 글
[SpringBoot] Spring Data JPA (0) | 2021.02.22 |
---|---|
[SpringBoot] DataBase 연동 (0) | 2021.02.20 |
[SpringBoot] 프로젝트 구조 (0) | 2021.02.20 |
[SpringBoot] 프로젝트 생성 및 실행 (0) | 2021.02.20 |
[SpringBoot] 스프링 부트(Spring Boot) 란 (0) | 2021.02.20 |