Spring MVC - Binding a Date Field?

Spring makes use of ServletRequestDataBinder to bind its values. The process can be described as follows.

Up vote 9 down vote favorite 1 share g+ share fb share tw.

For request parameters representing string, number, and boolean values, the Spring MVC container can bind them to typed properties out of the box. How do you have the Spring MVC container bind a request parameter representing a Date? Speaking of which, how does the Spring MVC determine the type of a given request parameter?

Thanks! Java spring spring-mvc link|improve this question edited Sep 14 '10 at 6:49Sean Patrick Floyd54.6k539113 asked Sep 14 '10 at 0:02Tom Tucker1,3791527 98% accept rate.

Spring makes use of ServletRequestDataBinder to bind its values. The process can be described as follows /** * Bundled Mock request */ MockHttpServletRequest request = new MockHttpServletRequest(); request. AddParameter("name", "Tom"); request.

AddParameter("age", "25"); /** * Spring create a new command object before processing the request * * By calling .class.newInstance(); */ Person person = new Person(); ... /** * And Then with a ServletRequestDataBinder, it bind the submitted values * * It makes use of Java reflection To bind its values */ ServletRequestDataBinder binder = ServletRequestDataBinder(person); binder. Bind(request); Behind the scenes, DataBinder instances internally makes use of a BeanWrapperImpl instance which is responsible for set up the values of the command object. With getPropertyType method, it retrieves the property type If you see the submitted request above (of course, by using a mock), Spring will call BeanWrapperImpl beanWrapper = new BeanWrapperImpl(person); Clazz requiredType = beanWrapper.

GetPropertyType("name"); And Then beanWrapper. ConvertIfNecessary("Tom", requiredType, methodParam) How does Spring MVC container bind a request parameter representing a Date? If you have human-friendly representation of data which needs special conversion, you must register a PropertyEditor For instance, java.util.

Date does not know what 13/09/2010 is, so you tell Spring Spring, convert this human-friendly date by using the following PropertyEditor binder. RegisterCustomEditor(Date. Class, new PropertyEditorSupport() { public void setAsText(String value) { try { setValue(new SimpleDateFormat("dd/MM/yyyy").

Parse(value)); } catch(ParseException e) { setValue(null); } } public String getAsText() { return new SimpleDateFormat("dd/MM/yyyy"). Format((Date) getValue()); } }); When calling convertIfNecessary method, Spring looks for any registered PropertyEditor which takes care of converting the submitted value. To register your PropertyEditor, you can either Spring 3.0 @InitBinder public void binder(WebDataBinder binder) { // as shown above } Old-style Spring 2.

X @Override public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { // as shown above }.

Thank you so much. – Tom Tucker Sep 14 '10 at 5:25 1 Spring 2.5. X has a @InitBinder as well: static.springsource.org/spring/docs/2.5.... – Richards Mar 27 '11 at 0:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions