Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of init binder in spring MVC

People also ask

What is a init binder in Spring MVC?

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.

What is the use of INIT binder?

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.

What is the use of WebDataBinder?

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.

How do I use InitBinder in spring?

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.