The Init Binder is an annotation that is used to customize the request being sent to the controller. It helps to control and format requests that come to the controller as it is defined in the controller.
Annotation Type InitBinder Annotation that identifies methods that initialize the WebDataBinder which will be used for populating command and form object arguments of annotated handler methods.
WebDataBinder is used to populate form fields onto beans. An init binder method initializes WebDataBinder and registers specific handlers, etc on it. In this case the @InitBinder method initializes the binder and registers context(locale) specific handlers, editors, etc.
We will add @Initbinder annotated method to the controller,To add a initbinder method we have to declare a method with @initbinder annotation ,this method should have WebDatabinder as parameter. We will register StringTrimmerEditor as a custom editor to the databinder with String class as target source.
1) Before, you had to resort to manually parsing the date:
public void webmethod(@RequestParam("date") String strDate) {
Date date = ... // manually parse the date
}
Now you can get the parsed date directly:
public void webmethod(@RequestParam("date") Date date) {
}
2) If your jsp
page supplies a date on the form yyyy-MM-dd
you can retrieve it as a Date
object directly in your controller.
3) Spring tries against all registered editors to see if values can be converted into objects. You don't have to do anything in the object itself, that's the beauty of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With