每当我在文本框中输入一个空字符串并尝试保存它时,我都会遇到这个错误我有这个错误:
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"
我的控制器中有这个活页夹:
@InitBinder
public void initBinder(WebDataBinder binder) {
NumberFormatUtil.registerDoubleFormat(binder);
}
我有一个NumberFormatUtil.java
实现静态函数的类registerDoubleFormat(binder)
:
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)非常陌生。请帮忙。提前致谢。