본문 바로가기
백엔드/Spring(Boot)

String Boot 기본 Controller

by 김어찐 2021. 8. 3.
728x90

1. GetMapping 어노테이션으로 URL 매칭한다.

2. html에 "data"라는 변수로 "hello!!" 값을 전송

3. hello.html로 전송(return 값)

 * 기본 템플릿 경로(src/main/resources/templates/(hello).html

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("hello") // URL 매칭
    public String hello(Model model)
    {
        model.addAttribute("data","hello!!");
        return "hello"; // src/main/resources/templates 폴더의 html 이름
    }
}

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}">안녕하세요 손님</p>
</body>
</html>

 

결과 html 페이지

728x90

'백엔드 > Spring(Boot)' 카테고리의 다른 글

SpringBoot @PostMapping  (0) 2021.08.04
스프링 부트 테스트  (0) 2021.08.03
스프링 AOP  (0) 2021.08.03
@ResponseBody 설명  (0) 2021.08.03
gradlew.bat build 오류  (0) 2021.08.03