Java Spring MVC中对象列表的嵌套表单数据绑定

Java Spring MVC中对象列表的嵌套表单数据绑定,java,forms,spring-mvc,nested-forms,data-binding,Java,Forms,Spring Mvc,Nested Forms,Data Binding,我有一个这样的目标: 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

我有一个这样的目标:

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


}
我在谷歌上搜索了这个,搜索了堆栈溢出,找到了registerCustomEditor和类似函数的引用,但我不确定如何正确解决这个问题


自定义属性编辑器就是这样做的吗?如果是这样的话,它是如何工作的?

我想你的问题是对的。当您执行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);
    }
} 
 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
@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(FieldValue.class, CustomEditor() ));
}
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);
    }
}