Spring的MVC defaultValue for Double

时间:2010-05-13 00:07:10

标签: java spring spring-mvc

我正在尝试构建一个这样的控制器:

@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);
}

这很好用:

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

但是在这一个userCash == null尽管默认值

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

从某些挖掘中看起来第一个像这样的编辑器绑定工作b / c:

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

问题是第二个参数(即false)定义是否允许空值。如果我将其设置为true,那么系统会将空白输入视为有效,因此我得到一个null Double类。

如果我将其设置为false,则系统会在空白输入字符串上使用:

进行阻塞
  

org.springframework.beans.TypeMismatchException:   无法转换类型的值   'java.lang.String'到必需的类型   '双';嵌套异常是   java.lang.NumberFormatException:为空   串

有谁知道如何让defaultValue适用于双打?

1 个答案:

答案 0 :(得分:0)

可能您必须使用自己的CustomEditor来实现转换逻辑。

从请求的角度来看,url喜欢/activities.do?userCash=&表示userCash参数实际上是一个空格或空字符串(因此您看到的错误)。

相关问题