views:

101

answers:

1

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!

+3  A: 

How does the Spring MVC determine the type of a given request parameter ?

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 <COMMAND_CLASS>.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 is 13/09/2010, 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
}
Arthur Ronald F D Garcia
Thank you so much.
Tom Tucker