0

Spring docs says this about InitBinder

Annotation that identifies methods which initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.

What i got from this this method is used for initializing the WebDataBinder which is used to populate model object so that it can be used further in handler method. But i am not sure what does "... of annotated handler methods" mean here?"

Update :- i believe it means after going thru Sotirios Delimanolis answer

InitBinder Annotation identifies methods which initialize the WebDataBinder which will be used for model object that is further passes to handler method annotated with @RequestMapping

user3198603
  • 5,528
  • 13
  • 65
  • 125

1 Answers1

1

A handler method is typically meant to distinguish methods annotated with @RequestMapping within a @Controller or @RequestMapping annotated type (see RequestMappingHandlerMapping).

For example,

@RequestMapping("/example")
public String setItemValue(@ModelAttribute Item item) {
    item.setValue(42);
    return "some-view";
}

An @InitBinder method could be used to help populate the WebDataBinder which will create the argument to be passed to the setItemValue handler method.

Here's some extra reading on @ModelAttribute. It is not necessarily required (just being explicit).

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • i believe you mean "@InitBinder method could be used to help populate the argument to be passed to the populateItem handler method." Correct? If yes Is it mandatory to annotate that particular argument with @ModelAttribute. I think no. – user3198603 Dec 23 '14 at 17:46
  • @user3198603 Edited with a link. – Sotirios Delimanolis Dec 23 '14 at 17:48
  • Thanks sotirios. Please see my update also. Your edition says the same stuff i mentioned in my edit. Thanks – user3198603 Dec 23 '14 at 17:51
  • @user3198603 Do you have another question or is something unclear? – Sotirios Delimanolis Dec 23 '14 at 17:54
  • One more quick thing, InitBinder method will be called before every call of handler method annotated with @RequestMapping. Correct ? – user3198603 Dec 23 '14 at 17:57
  • @user3198603 It will only be invoked if your handler method has appropriate model attributes/command object parameters. – Sotirios Delimanolis Dec 23 '14 at 18:01
  • what do yo mean by "appropriate model attributes/command object parameters" ? Can you explain it bit more? – user3198603 Dec 23 '14 at 18:07
  • @user3198603 If you have a handler method with a parameter annotation with `@ModelAttribute`, then an `@InitBinder` method will be invoked. If, however, your handler method doesn't have any such parameters, there's no point for the `@InitBinder` method to be invoked. – Sotirios Delimanolis Dec 23 '14 at 18:09
  • Another thing i found out that under initbinder method we generally register validator or customEditor . But Javadoc says we populate model object under it ? – user3198603 Dec 23 '14 at 18:12
  • @user3198603 I've reworded my answer. `@InitBinder` doesn't work on the model attribute. It works on the `WebDataBinder` which works on the model attribute. – Sotirios Delimanolis Dec 23 '14 at 18:36