๋ฐฑ์—”๋“œ(Back-End) ๊ฐœ๋ฐœ/SpringBoot

[SpringBoot] 1.2_View ํ™˜๊ฒฝ์„ค์ • ๋ฐ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

rabo93 2025. 2. 24. 18:26
์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” Welcome page ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค

 

static/index.html์„ ์˜ฌ๋ ค๋‘๋ฉด Welcome page ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

https://docs.spring.io/spring-boot/reference/web/reactive.html#web.reactive.webflux.welcome-page

 

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

ํƒ€์ž„๋ฆฌํ”„ ์‚ฌ์ดํŠธ :  https://www.thymeleaf.org/ 

 

 

  • ๋ทฐํŽ˜์ด์ง€
<!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>

 

  • controller ํŒจํ‚ค์ง€ ์ƒ์„ฑ > HelloController ํด๋ž˜์Šค ์ž‘์„ฑ
package hello.hello_spring.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!!!");
        // data๋ผ๋Š” ๋ณ€์ˆ˜์— hello!!!๋ผ๋Š” ๊ฐ’ ๋„ฃ์–ด์„œ model๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•จ
        return "hello";
        // templates/hello.html(Thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„ ์ฒ˜๋ฆฌ)
    }
}

 

 

 

 

๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

 

์ฝ˜์†”์ฐฝ(CMD) ์‹คํ–‰ (ํ”„๋กœ์ ํŠธ๊ฐ€ C๊ฒฝ๋กœ์— ์žˆ์–ด์•ผํ•œ๋‹ค! D๋กœ ํ•˜๋‹ˆ๊นŒ ์•ˆ๋์Œ..)

C:\>cd C:\SPRINGBOOT\intelliJ\hello-spring

C:\SPRINGBOOT\intelliJ\hello-spring>gradlew

... BUILD SUCCESSFUL ...

C:\SPRINGBOOT\intelliJ\hello-spring>gradlew build

... BUILD SUCCESSFUL ...

C:\SPRINGBOOT\intelliJ\hello-spring>cd build
C:\SPRINGBOOT\intelliJ\hello-spring\build>cd libs
C:\SPRINGBOOT\intelliJ\hello-spring\build\libs>dir

C:\SPRINGBOOT\intelliJ\hello-spring\build\libs>java -jar hello-spring-0.0.1-SNAPSHOT.jar