views:

453

answers:

1

Hi,

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?

A: 

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);
    }
} 
Javi