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

发布于 2024-08-30 08:08:24 字数 1242 浏览 3 评论 0原文

我有一个像这样的对象:

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 和类似函数的引用,但我不确定如何正确解决这个问题。

自定义属性编辑器是解决此问题的方法吗?如果是这样,它将如何运作?

I have an object like so:

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


}

I loop through a list of FormFields and if the fieldType does not equal a radio button I am outputting the list of field values in a JSP using

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

which is all good and works fine.

Outside of this I have a check for if the fieldType is a radio, in which I use:

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

However this is causing me problems where I get errors like such:

 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

I've googled this and searched Stack Overflow and found references to registerCustomEditor and similar functions, but I am unsure of how to properly solve this.

Is the custom property editor the way to go with this? If so, how would it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-09-06 08:08:24

我认为你说的对,问题出在哪里。当您执行 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);
    }
} 

I think you're right in what's the problem. When you do path="formFields[${formFieldRow.index}].value" you're returning a String value from each radiobutton of the form and Spring should know how to convert this String value into each FieldValue object to fill the List value.

So you need to create your customEditor and in your initbinder associate this editor to the List class:

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

and your CustomEditor class should extends PropertyEditorSupport like this:

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);
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文