How can I make initBinder()
method to start every time form loads and submits. This example has responsibility to convert date from String to java.util.Date
.
In my servlet-context.xml:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.example.web.ExampleBindingInitializer" />
</property>
</bean>
Here is my implementation of WebBindingInitializer:
public class ExampleBindingInitializer implements WebBindingInitializer {
private ExampleService exampleService;
@Autowired
public ExampleBindingInitializer(ReservationService reservationService) {
this.reservationService = reservationService;
}
public void initBinder(WebDataBinder binder, WebRequest request) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
I didn't made any modifications in controller where ExampleService methods are called. Where I'm wrong?
When I put initBinder()
method with @InitBinder
annotation to my controller, everything works fine. That doesn't satisfy me beacause I want to have that in external class.