Controller 생성 -> 메소드 매개변수로 Model 타입의 model 변수 선언
import org.springframework.ui.Model;
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!"); //key, value 형식
return "hello";
}
@Getter
@Setter
public class TestModel {
private String name;
private int age;
}
@RestController
public class TestController {
@GetMapping("/")
public String getTestPage(@ModelAttribute TestModel testModel) {
System.out.println(testModel.getname());
System.out.pringln(testModel.getAge());
return "test";
}
}
import org.springframework.web.bind.annotation.RequestParam;
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
참고
https://nancording.tistory.com/90
https://galid1.tistory.com/769
https://iamdaeyun.tistory.com/entry/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%BB%A8%ED%8A%B8%EB%A1%A4%EB%9F%AC
https://velog.io/@sloools/Spring-Boot-%EC%BB%A8%ED%8A%B8%EB%A1%A4%EB%9F%AC-ModelAttribute-%EC%96%B4%EB%85%B8%ED%85%8C%EC%9D%B4%EC%85%98