인프런 커뮤니티 질문&답변
@ModelAttribute에 관한 설명에 대한 질문이 있습니다.
작성
·
657
답변 6
2
둘 다 맞아요. @ModelAttribute를 어떤 객체를 리턴하는 메소드에 사용하면 말씀하신 용도로 쓰이고 메소드 매개변수에 사용하면 제가 설명한대로 쓰이는거구요.
0
0
저한테 레퍼런스 설명이 맞냐고 물으시는건가요.. 당연히 맞죠.
제가 수업에서 설명했던거 같은데 @RequestParam이랑 @ModelAttribute는 생략할 수 있습니다. 그걸 안쓰고 바인딩 받을 수 있다고 해서 @ModelAttribute에 그런 기능이 없는게 아니에요.
Note that use of @ModelAttribute is optional — for example, to set its attributes. By default, any argument that is not a simple value type( as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver is treated as if it were annotated with @ModelAttribute.
0
더 아래로 가보면
After the model attribute instance is obtained, data binding is applied. The WebDataBinder class matches Servlet request parameter names (query parameters and form fields) to field names on the target Object. Matching fields are populated after type conversion is applied, where necessary. For more on data binding (and validation), see Validation. For more on customizing data binding, see DataBinder.
라고 되어있고 @ModelAttribute가 없어도 WebDataBinder를 통해 파라미터가 매핑되고 @ModelAttribute는 정확히는 파라미터가 매핑된 객체를 모델 객체의 속성에 넣어주는것. 맞나요?
@ModelAttribute를 사용하지않아도 데이터는 객체로 바인딩 되는것을 확인했습니다.
0
제대로 찾으셨네요. 거기에 바로 이렇게 적혀 있자나요. 바로 아래 예제도 나오구요.
You can use the @ModelAttribute annotation on a method argument to access an attribute from the model or have it instantiated if not present. The model attribute is also overlain with the values of query parameters and form fields whose names match to field names. This is referred to as data binding, and it saves you from having to deal with parsing and converting individual query parameters and form fields. The following example binds an instance of Pet:





