본문 바로가기
개발/spring

spring get데이터 받기 처리

by 향유 2022. 11. 24.

 

컨트롤러

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;

@Controller
public class HelloController {

    // /hello 로 get 주소 입력시 아래 소스 매핑하라는 명령어
    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data","hello!!"); //모델에 정보담아서 세팅
        return "hello"; //hello.html로 model 데이터 전달 명령어
    }

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


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

 

웹에 http://localhost:8080/hello-mvc?name=ssee22 url 접근시 hello-template로 값 넘겨줌

 

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="'hello '+${name}">안녕하세요. empty</p>
</body>
</html>

댓글