InitBinders

Typical web forms have dates, currencies, and amounts. The values in the forms need to be bound to the form backing objects. Customization of how binding happens can be introduced using the @InitBinder annotation.

Customization can be done in a specific controller or a set of controllers using Handler Advice. This example shows how to set the default date format to use for form binding:

    @InitBinder     protected void initBinder(WebDataBinder binder) {       SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");       binder.registerCustomEditor(Date.class, new CustomDateEditor(       dateFormat, false));     }

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.