views:

45

answers:

1

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?

A: 

Likely you'll have to use your own CustomEditor that would implement your conversion logic.

From the request point of view, the url like /activities.do?userCash=& mean that userCash param is actually a blank or empty string (hence the error you are seeing).

Eugene Kuleshov