2

私は次のようなオブジェクトを持っています:

public class FormFields extends BaseObject implements Serializable {

private FieldType fieldType; //checkbox, text, radio
private List<FieldValue> value; //FieldValue contains simple string/int information, id, value, label

//other properties and getter/setters


}

FormFields のリストをループし、fieldType がラジオ ボタンと等しくない場合は、JSP を使用してフィールド値のリストを出力しています。

 <c:forEach items=${formField.value}></c:forEach>

これはすべて良好で、正常に動作します。

これ以外に、fieldType がラジオであるかどうかをチェックします。

<form:radiobuttons path="formFields[${formFieldRow.index}].value" items="${formField.value}" itemLabel="label" cssClass="radio"/>

ただし、これにより、次のようなエラーが発生する問題が発生しています。

 Failed to convert property value of type [java.lang.String] to required type [java.util.List] for property formFields[11].value; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.example.model.FieldValue] for property value[0]: no matching editors or conversion strategy found

これをグーグルで検索し、Stack Overflow を検索して、registerCustomEditor および同様の関数への参照を見つけましたが、これを適切に解決する方法がわかりません。

カスタム プロパティ エディタはこれに対応する方法ですか? もしそうなら、それはどのように機能しますか?

4

1 に答える 1

2

何が問題なのか、あなたは正しいと思います。path="formFields[${formFieldRow.index}].value" を実行すると、フォームの各ラジオボタンから文字列値が返され、Spring はこの文字列値を各 FieldValue オブジェクトに変換してリスト値を埋める方法を認識している必要があります。 .

そのため、customEditor を作成し、initbinder でこのエディターを List クラスに関連付ける必要があります。

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(FieldValue.class, CustomEditor() ));
}

CustomEditor クラスは、次のように PropertyEditorSupport を拡張する必要があります。

public class CustomEditor extends PropertyEditorSupport{  
    public void setAsText(String text) {
        FieldValue field;
        //you have to create a FieldValue object from the string text 
        //which is the one which comes from the form
        //and then setting the value with setValue() method
        setValue(field);
    }
} 
于 2010-04-30T18:41:47.463 に答える