Question

I am using spring mvc and am still trying to get my head around binders, formatters and converters.

I have a custom binder behind one of my controllers...

@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat df = new SimpleDateFormat(dateFormat);
    df.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
}

This particular custom binder is not specific to just the form for this controller but will be used everywhere throughout the application.

What is the best way for me to do this more generically for all of my controllers?

P.S. I have got a conversion service so I could use this if it is the right place to do it.

public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean

thanks

Was it helpful?

Solution

You can consider the use of @ControllerAdvice annotation. The classes with this annotation assist every controller.

So you can write something like this:

@ControllerAdvice
public class GlobalInitializer {

    @InitBinder
    public void globalBinder(WebDataBinder webDataBinder) {
       SimpleDateFormat df = new SimpleDateFormat(dateFormat);
       df.setLenient(false);
       webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top