无法将java.lang.String类型的属性值转换为所需的double类型
每当我在文本框中输入一个空字符串并尝试保存时,我都会出现此错误。我有此错误:无法将java.lang.String类型的属性值转换为所需的double类型,java,spring-mvc,Java,Spring Mvc,每当我在文本框中输入一个空字符串并尝试保存时,我都会出现此错误。我有此错误: 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
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
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"
但是,当我输入无效的数字格式(如“ddd”)时,我会出现以下错误:
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
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);
}
我有一个类numberformatil.java
,它实现了静态函数registerDoubleFormat(binder)
:
NumberFormatUtil.java
我正在使用Spring3.0.1。我对java和其他相关技术(如spring)非常陌生。请帮忙。提前谢谢。我不知道这是否是您的问题的原因,但是
str==”
是一个bug
如果要测试字符串是否为空,请使用str.isEmpty()
或str.length()==0
或甚至“”.equals(str)
=
操作符测试两个字符串是否为同一对象。这并不能满足您的需要,因为在您运行的应用程序中可能有许多不同的字符串实例代表相同的字符串。在这方面,空字符串与其他字符串没有什么不同
即使这不是问题的原因,您也应该修复此错误,并注意不要使用
==
测试字符串。(或者至少,除非您已采取特殊步骤确保它始终工作……这超出了本问答的范围。)更改setAsText()方法,如下所示
public void setAsText(String str) {
if(str == null || str.trim().equals("")) {
setValue(0d); // you want to return double
} else {
setValue(Double.parseDouble(str));
}
}
至于空字符串,我想问题是您的0被强制转换为整数,而不是双精度,因此您必须使用后缀d:0.0d 至于NumberFormatException,我不认为转换器无法转换它有任何问题。如果您想为转换错误生成一条自定义消息,那么应该按照 我认为它将类似于
typeMismatch.java.lang.Double=“无效浮点数”
并且在bean配置中有一个消息源
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>exceptions</value><!--- that means you have exceptions.properties in your class path with the typeMismatch string specified above-->
</list>
</property>
</bean>
例外情况
另外,属性编辑器的概念现在已经过时了,这是一条路要走,因为spring不会为使用这种方法编辑的任何属性创建一堆辅助对象(属性编辑器)。只是尝试了一下,但没有成功。也许问题出在别的地方。我正在尽我最大的努力去解决它。无论如何,谢谢你的建议。你应该先检查它是否为空。那么答案是什么?阅读Spring论坛,这似乎应该是可行的。我得到一个“未能将[java.lang.Double]类型的属性值转换为属性所需的[java.lang.Double]类型…”的消息,这让我震惊。