๐Ÿ”™๋’ค๋กœ๊ฐ€๊ธฐ

Thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„

ํƒ€์ž„๋ฆฌํ”„๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ทฐ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋‹ค.

jpabook.jpashop.HelloController

package jpabook.jpashop;

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){
        //data๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ hello!!๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋ธ์— ๋‹ด๋Š”๋‹ค.
         model.addAttribute("data", "hello!!");
        // ํ™”๋ฉด ์ด๋ฆ„
         return "hello";
     }
}

thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„ ๋™์ž‘ ํ™•์ธ(hello.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>

์œ„์น˜: resource/template/hello.html

indexํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ : static/index.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>
Hello
<a href="/hello">hello</a>
</body>
</html>

์ฐธ๊ณ : spring-boot-devtools ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด html ํŒŒ์ผ์„ ์ปดํŒŒ์ผ๋งŒ ํ•ด์ฃผ๋ฉด ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ์—†์ด ViewํŒŒ์ผ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‹จ์ถ•ํ‚ค : Ctrl + Shift + F9(ํ˜„์žฌ ํŒŒ์ผ๋งŒ ๋ฆฌ์ปดํŒŒ์ผ)

build.gradle์— ์˜์กด์„ฑ ์ถ”๊ฐ€

developmentOnly 'org.springframework.boot:spring-boot-devtools'

application.properties์— ์‚ฌ์šฉ ์—ฌ๋ถ€ ์ถ”๊ฐ€

spring.devtools.livereload.enabled=true
spring.devtools.restart.enabled=true