Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

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

1 min read

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);
}

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus