Spring MVC defaultValue for Double

I am trying to build a controller as follows:

@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 great:

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

but in this oneCash == null, despite the default value

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

From some digging, it seems that the first works with a b / c editor, like this:

binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, false));

The problem is that the second parameter (i.e. false) determines whether null values ​​are allowed. If I set it to true, then the system considers the empty input valid, so I get a null Double class.

If I set it to false, the system slams an empty input line with:

org.springframework.beans.TypeMismatchException: 'java.lang.String' ""; java.lang.NumberFormatException:

- , defaultValue ?

+3
1

, CustomEditor, .

, url like/activities.do?userCash=& , userCash (, , ).

0

Source: https://habr.com/ru/post/1745203/


All Articles