9

テキストボックスに空の文字列を入力して保存しようとすると、このエラーが発生します。このエラーが発生します。

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や春などの他の関連技術に非常に慣れていません。助けてください。前もって感謝します。

4

3 に答える 3

5

ここのように setAsText() メソッドを変更します。

   public void setAsText(String str) { 
       if(str == null || str.trim().equals("")) {
           setValue(0d); // you want to return double
       } else {
           setValue(Double.parseDouble(str)); 
       }
  } 
于 2012-05-28T08:13:51.537 に答える
4

これが問題の原因かどうかはわかりませんstr == ""が、バグです。

String が空かどうかをテストする場合は、str.isEmpty()orstr.length() == 0または evenを使用します"".equals(str)

オペレーターは==、2 つのストリングが同じオブジェクトであるかどうかをテストします。実行中のアプリケーションには、同じ文字列を表すさまざまな String インスタンスが多数存在する可能性があるため、これは希望どおりにはなりません。この点では、空の文字列は他の文字列と変わりません。


これが問題の原因ではない場合でも、このバグを修正し、==文字列のテストに使用しないように心に留めておく必要があります。(または、少なくとも、それが常に機能することを保証する特別な手順を実行していない限り、この Q&A の範囲を超えています。)

于 2012-05-28T02:57:21.023 に答える
4

空の文字列に関しては、問題は、0がDoubleではなくIntegerにキャストされるため、接尾辞d : 0.0dを使用する必要があることだと思います。

NumberFormatException に関しては、コンバーターが変換できなかったという問題は見当たりません。変換エラーのカスタム メッセージが必要な場合は、そのメッセージをDefaultMessageCodeResolverのセマンティクスに従ってメッセージ プロパティ ファイルに配置する必要 がtypeMismatch.java.lang.Double = "invalid floating point number" あります。

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

また、プロパティ エディターの概念は現在では時代遅れになっています。コンバーターを使用した新しい API が 適しています。これは、Spring がこのアプローチで編集されるプロパティのヘルパー オブジェクト (プロパティ エディター) のヒープを作成しないためです。

于 2012-05-28T08:53:10.280 に答える