Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 双精度的Spring MVC默认值_Java_Spring_Spring Mvc - Fatal编程技术网

Java 双精度的Spring MVC默认值

Java 双精度的Spring MVC默认值,java,spring,spring-mvc,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); } binde

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

@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);
}
binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, false));
这很好:

但是在这个例子中,尽管有默认值,userCash==null

从一些挖掘来看,第一个编辑器的b/c绑定方式似乎是这样的:

@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);
}
binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, false));
问题在于第二个参数(即false)定义了是否允许空白值。如果将其设置为true,则系统将空输入视为有效,因此我得到空的双类。

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

”阻塞。 org.springframework.beans.TypeMismatchException: 无法转换类型的值 “java.lang.String”转换为所需类型 “双倍”;嵌套异常是 java.lang.NumberFormatException:空 串


有人知道如何让defaultValue用于Double吗?

您可能需要使用自己的CustomEditor来实现转换逻辑


从请求的角度来看,像/activities.do?userCash=&这样的url意味着userCash参数实际上是一个空白或空字符串(因此您看到了错误)。

这听起来像是一个bug,或者至少对我来说是一个合理的增强请求。你考虑过在吉拉开一张票吗?