Spring MVC defaultValue for Double

Posted by mlathe on Stack Overflow See other posts from Stack Overflow or by mlathe
Published on 2010-05-13T00:07:09Z Indexed on 2010/05/13 0:14 UTC
Read the original article Hit count: 554

Hi All, I'm trying to build a controller like this:

@RequestMapping(method = {RequestMethod.GET}, value = "/users/detail/activities.do")
public View foo(@RequestParam(value = "userCash", defaultValue="0.0") Double userCash)
{
    System.out.println("foo userCash=" + userCash);
}

This works fine:

http://localhost/app/users/detail/activities.do?userCash=123&

but in this one userCash==null despite the default value

http://localhost/app/users/detail/activities.do?userCash=&

From some digging it seems like the first one works b/c of a Editor binding like this: binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, false));

The trouble is that the second param (ie false) defines whether blank values are allowed. If i set that to true, than the system considers the blank input as valid so i get a null Double class.

If i set it to false then the system chokes on the blank input string with: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'double'; nested exception is java.lang.NumberFormatException: empty String

Does anyone know how to get the defaultValue to work for Doubles?

Thanks --Matthias

© Stack Overflow or respective owner

Related posts about spring

Related posts about mvc