gpt4 book ai didi

java - 无法将 java.lang.String 类型的属性值转换为所需的类型 double

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:00 25 4
gpt4 key购买 nike

每当我在文本框中输入一个空字符串并尝试保存它时,我都会遇到这个错误 我遇到了这个错误:

Failed to convert property value of type java.lang.String to 
required type double for property customerAcctSetting.maxAllowableAmount;
nested exception is java.lang.IllegalArgumentException: Cannot convert value of
type [java.lang.String] to required type [double] for
property maxAllowableAmount:
PropertyEditor [bp.ar.util.NumberFormatUtil$CustomerDoubleEditor] returned
inappropriate value

但是当我输入一个无效的数字格式如“ddd”时我有这个错误:

Failed to convert property value of type java.lang.String to required
type double for property customerAcctSetting.maxAllowableAmount;
nested exception is java.lang.NumberFormatException: For input string: "ddd"

我的 Controller 中有这个 Binder :

@InitBinder
public void initBinder(WebDataBinder binder) {
NumberFormatUtil.registerDoubleFormat(binder);
}

我有一个实现静态函数registerDoubleFormat(binder)的类NumberFormatUtil.java:

NumberFormatUtil.java

public static void registerDoubleFormat (WebDataBinder binder) {
binder.registerCustomEditor(Double.TYPE, new CustomerDoubleEditor());
}

private static class CustomerDoubleEditor extends PropertyEditorSupport{
public String getAsText() {
Double d = (Double) getValue();
return d.toString();
}

public void setAsText(String str) {
if( str == "" || str == null )
setValue(0);
else
setValue(Double.parseDouble(str));
}
}

我使用的是 Spring 3.0.1。我对 java 和其他相关技术(如 spring)非常陌生。请帮忙。提前致谢。

最佳答案

像这里一样改变你的 setAsText() 方法,

   public void setAsText(String str) { 
if(str == null || str.trim().equals("")) {
setValue(0d); // you want to return double
} else {
setValue(Double.parseDouble(str));
}
}

关于java - 无法将 java.lang.String 类型的属性值转换为所需的类型 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com