본문 바로가기
Spring Boot

230210 Spring Boot 뷰 선택-Thymleaf

Thymeleaf템플릿 사용

jsp와 문법이 크게 다르지 않다.
jsp뷰가 있으면 덮어씌워져 실행이 안된다. 
build.gradle과 application.properties에서 jsp, jstl 관련 구문 주석처리.
이전에 생성한 webapp-WEB-INF-views폴더 및 파일 전부 삭제.


maven repository에서
Spring Boot Starter Thymeleaf로 가져옴

gradle에

dependencies {
 						...
	
	//Thymeleaf를 사용
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	
}


넣고 

gradle refresh


별다른 설정 없이

src/main/resources의 
templates아래에
home.html로 파일 생성

home.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	타임리프 뷰
</body>
</html>

 



컨트롤러를 만들어놔서 자동적으로 들어간다.

package com.simple.basic.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
	
	@GetMapping("/")
	public String home() {
		return "home";
	}
	
}


templates아래에 html을 만들면, 컨트롤러를 탄다.