Spring MVC 中嵌套表单数据与对象列表的绑定
我有一个像这样的对象:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你说的对,问题出在哪里。当您执行 path="formFields[${formFieldRow.index}].value" 时,您将从表单的每个单选按钮返回一个字符串值,Spring 应该知道如何将此字符串值转换为每个 FieldValue 对象以填充列表值。
因此,您需要创建您的 customEditor 并在 initbinder 中将此编辑器关联到 List 类:
并且您的 CustomEditor 类应该像这样扩展 PropertyEditorSupport :
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:
and your CustomEditor class should extends PropertyEditorSupport like this: