inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 웹 MVC

@ModelAttribute에 관한 설명에 대한 질문이 있습니다.

668

최진환

작성한 질문수 2

0

@ModelAttrubte를 사용하면 파라미터로 넘어오는 값들을 취합해서 객체로 바인딩 해주신다 설명하셨는데 제가 기존에 알고 있던 지식과 달라 질문드립니다.

@ModelAttribute는 해당 파라미터를 Model 객체에 Attribute로 추가(Model 객체가 없을 시 생성 후 추가)하는것이고

DataBinder가 파라미터로 넘어오는 값들을 취합해 객체로 만들어 주는것으로 알고있습니다.

제가 알고있는것이 맞나요..?

참조:  스프링 레퍼런스

MVC spring java

답변 6

2

백기선

둘 다 맞아요. @ModelAttribute를 어떤 객체를 리턴하는 메소드에 사용하면 말씀하신 용도로 쓰이고 메소드 매개변수에 사용하면 제가 설명한대로 쓰이는거구요.

0

최진환

어.. 그럼 정리하면 @ModelAttribute는 Simple한 값이 아니면 기본적으로 붙고

모델 객체의 속성으로 해당 객체를 넣어주는 어노테이션이다.

맞을까요?

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:


0

최진환

기선님 혹시 죄송한데 관련된 자료 부탁드려도 괜찮을까요?

레퍼런스 에서 조금 유사한 내용이 있기는 한데.. 그건  모델(Model클래스 )을 객체화 한다는것으로 이해했습니다.

@ModelAttribute의 주석에서도 비슷한 내용은 못찾아서...

7:58 예제 코드 찾는 법

0

256

2

PATCH의 Idempotent에 대한 질문

0

257

1

2:51초 질문입니다.

0

324

1

만약 어플리케이션컨텍스트에 필터를 설정하지 않으면 어떻게되나요?

0

361

0

web.xml에 위치한 애플리케이션 web.xml 내에서 파싱 오류 발생

0

515

0

Spring Boot 2.6 이상 버전에서의 DispatcherServlet

0

366

1

Event`Xxx`Controller 분리할때의 기준에 관한 질문입니다.

0

634

1

11분 57초 쯤, `returnValueHandlers` 들의 네이밍컨벤션 관련 질문입니다.

0

379

1

@EnableWebmvc 설정을 하면

0

415

1

springframework와의 차이점

0

283

1

@RequestBody의 바인딩에러를 잡는 방법

1

1655

2

안녕하세요 기선님 강의를 보다 서블릿에 궁금증이 생겨 질문 드립니다

1

237

1

HelloService의 값을 전달을 못 받습니다.

0

296

1

ServletContext와 WebApplicationContext의 관계 질문

1

740

1

하나의 서비스에, 두개의 리포지토리도 연결이 되나요?

0

364

1

view name 리턴에 대해

0

290

2

Formatter print 질문 드립니다.

0

232

1

에리 메시지가 한글입니다.

0

294

1

java 클래스파일로 설정되어 있던 것을 web.xml로 바꾸는 경우

0

338

1

@RequestParam

0

255

1

@Retention 질문

0

229

1

Event.builder() 관련 mac에서 lombok 문제 있으신 분들

0

198

1

스프링 부트 사용시 WebMvcConfigurerSupport 클래스

0

253

1

http://localhost:8080/hello 입력시 오류

0

492

4