English 中文(简体)
转换 java. lang 类型 pava. string 的属性值失败
原标题:Failed to convert property value of type java.lang.String to required type double

每当我在文本框中输入空字符串并试图保存它时, 我就会有此错误 :

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) :

编号FormatUtil.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)); 
    } 
}

Im use Spring 3.0.1. Im use Spring 3.0.1. Im very new to Java 和诸如春天等其他相关技术。 请帮助。 提前感谢 。

问题回答

更改您的 SetAsText () 方法喜欢这里,

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

我不知道这是否是造成你问题的原因,但 str ================================================================================================================================================Y=================================YYY=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=Y=

如果您正在测试字符串是否为空, 请使用 str. isEmpty () str. larth () = 0 或甚至 "。 等同( str)

运算符测试, 以查看两个字符串是否是同一个对象。 这不会做你想做的事, 因为运行中的应用程序中可能有多个不同的字符串实例代表同一个字符串。 在这方面, 空字符串与其他字符串没有什么不同 。


即使这不是你问题的原因, 你也应该纠正这个错误, 并且在精神上注意不要使用 < code_ / code> 来测试字符串 。 (或者至少, 除非您采取了特殊步骤, 以确保它永远有效... 这超出了此 {% amp; A 的范围 。 )

至于空字符串,我想问题在于 您的“ 强” 0 d : < 强 > 0.00d ;

As for NumberFormatException, I don t see any problem that converter could not convert it. If you want to have a custom message for convertion errors you should put that message to your message properties file following the semantics of DefaultMessageCodeResolver I think it will be something like typeMismatch.java.lang.Double = "invalid floating point number" and have a message source in your bean configuration

    <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>

现在财产编辑者的概念也已经过时了,“a href=” http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html#core-convert” rel=“nofolt”>新的有转换器的API是前进的道路,因为春天不会为使用此方法编辑的任何财产产生大量的辅助物品(财产编辑)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...