HowTo: Bind an input field to a Date property using Spring's SimpleFormController

If you're using the Spring SimpleFormController for binding your Java bean object to the UI, you may come across the case where you want to create a binding between an input field (where the user inputs a date) and your bean's Date property:
<form:input path="expiryDate" size="15" />

You will notice that you cannot just do it as normal, but to let your SimpleFormController accept the binding, you have to override the following method:
@Override
protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
df.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));

super.initBinder(request,binder);
}
Kindle

Comments

0

Your ad here?