타임리프 th:object, @ModelAttribute

mminjg·2022년 3월 1일
0
<form th:action th:object="${item}" method="post">
...
</form>
  • addForm.html에서 th:object를 적용하기 위해서 해당 오브젝트 정보를 넘겨 주어야 한다. 객체를 전달하지 않을 경우 예외가 발생한다.
// 1
@GetMapping("/add")
public String addForm(@ModelAttribute Item item) {
	return "/items/addForm";
}

// 2
@GetMapping("/add")
public String addForm(Model model) {
	model.addAttribute("item", new Item());
	return "/items/addForm";
}
  1. model.addAttribute("item", new Item())를 통해 객체를 직접 생성한다.

  2. @ModelAttribute을 사용하면 Model에 지정한 객체를 자동으로 넣어준다.

즉, 1과 2는 같다.

0개의 댓글