@InitBinder

Today we look at @InitBinder annotation.

This can be used to set and invoke a custom Validator (Note: Don’t try and mix this with @Valid / JSR303 – the Bean Validation annotations).

In essence it allows us to replace this code:

public String handleRequest(SomeFormObject form, BindingResult result){
    someValidator.validate(form, result); //manually invoke
}

with:

@InitBinder
public void initBinder(WebDataBinder binder){
    binder.setValidator(validator); // set & let spring invoke
}

Note: If we need to use a custom Validator and Bean Validation annotations, then we need to use the first method.

Advertisements
Posted in Spring, Spring MVC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: