Spring Boot - MVC 2

이번엔 GET 방식으로 파라미터를 받아서 페이지에 띄워보도록 할게요.


hello-template

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello Page</title>
</head>
<body>
<p>타임리프 템플릿</p>
<p th:text="'hello ' + ${name}" >hello! empty</p>
</body>
</html>

 

Controller

package hello.hellospring.controller;

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

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data","spring!!!");
        return "hello";
    }

    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model){
        model.addAttribute("name", name);
        return "hello-template";
    }

    @GetMapping("hello-string")
    @ResponseBody
    public String helloString(@RequestParam("name") String name){
        return "hello " + name;
    }
}

 

name을 입력받으면 hello-mvc url에서는 hello-template 페이지를 리턴하게 되어 타임리프 템플릿 엔진으로 처리하게 되고, hello-string url에서는 hello 문자와 name만 출력하게 됩니다.

 

http://localhost:8080/hello-mvc? name=meow~~ >

 

http://localhost:8080/hello-string? name=meow~~ >

 

 

 

'Java > Springboot' 카테고리의 다른 글

Spring Boot - jsp 사용하기  (0) 2025.02.25
Spring Boot - 빌드하고 실행하기  (1) 2025.02.21
Spring Boot - API  (0) 2025.02.21
Spring Boot - MVC 1  (0) 2025.02.21
Spring Boot - 프로젝트 생성  (1) 2025.02.20