Spring/inflearn
thymeleaf 템플릿 엔진
곽수진
2022. 8. 18. 17:52
반응형
컨트롤러에서 return 값으로 문자 반환
↓
viewResolver가 화면을 찾아서 처리
↓
스프링 부트 템플릿엔진 기본 viewName 매핑
↓
resources:templates/ + {ViewName} + .html
▶ main/java/hello.hellospring 아래 controller 패키지를 생성하고 HelloController 파일을 생성
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model){
model.addAttribute("data", "hello!!");
return "hello";
}
}
▶ @GetMapping("hello") : 경로가 localhost:8080/hello로 매핑됨
▶ resources/templates/hello.html을 생성해 html 파일 작성
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>
▶ data 값에 hello!!를 넣어 출력
spring-boot-devtools 라이브러리를 추가하면 html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경 가능
반응형