spring boot

sangeun·2026년 5월 24일

spring initializr

GENERATE이후 압축을 푼다.

컴파일 후 localhost 8080에 에러 페이지가 나온다.

<!DOCTYPE HTML>
<html>
<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>

static에 해당 코드를 입력하면 사이트에 그대로 출력된다.
추가로 hello를 클릭하면 Error Page로 들어가게 된다.

위의 것은 정적엔진이나, thymeleaf 템플릿 엔진을 활용해서 개선해본다.

hello.hello_spring에 controller라는 패키지를 만들어

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!!");
        return "hello";
    }
    //웹 어플리케이션에서 hello들어오면 해당 메서드 실행
}

resources에서 templates에서 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>

hello에 반응하여 {data}에 반영된다.

0개의 댓글