Server๐งค/JPA
[SpringBoot๊ณผ JPA ํ์ฉ1] Viewํ๊ฒฝ์ค์ , Thymeleaf์๊ฐ, ์๋ฒ์ฌ์์ ์์ด ๋ทฐํ์ผ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๋ฐฉ๋ฒ
yujindonut
2022. 8. 8. 18:12
728x90
Template Engines ์์ ๊ณ ๋ฅผ ์ ์์ (JSP์ Tymeleaf )
Spring์์๋ Tymeleaf๋ฅผ ๋ฐ๊ณ ์๋ค!
- Natural templates
์ฅ์ : ๋งํฌ์ ์ ๊นจ์ง ์๋๋ค. ๊ทธ๋ฅ ์น๋ธ๋ผ์ฐ์ ์์ ๋ฐ๋ก ์คํ์ด ๋๋ค.
๋จ์ : ํ๊ทธ๋ฅผ ๋ซ์์ฃผ๋ ์์ผ๋ก ์ฐ์ง ์์ผ๋ฉด ์๋ฌ๊ฐ ๋๋ค. <br></br> (๊ทน๋ณต๋จ), ์ฑ๋ฅ๋ ๊ฐ์ ๋จ.
thymeleaf ๋ ์๋์ผ๋ก viewName์ ๋งคํํด์ค๋ค
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
}
<!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>
์๋ฒ๋ฅผ ์ฌ์์ํ๋ฉด ๋ฐ๋ก localhost:8080/helloํ๋ฉด ์๋์ผ๋ก hello.html๋ก ๊ฐ๋ค
build.gradle์
implementation 'org.springframework.boot:spring-boot-devtools'
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด, htmlํ์ผ์ ์ปดํ์ผ๋ง ํด์ฃผ๋ฉด ์๋ฒ ์ฌ์์ ํ์ ์์ด view ํ์ผ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค!
728x90