macob
Oct 8th, 2005, 10:41 AM
I have a collection of a custom class (with it's own custom PropertyEditor) in my command class.
My JSP has a multi-select box. I want the values selected to be converted into a List of my custom class objects. I followed this posting:
http://forum.springframework.org/showthread.php?t=17646
Which works fine when using an array as the command property, but fails when using a collection.
My Command Class looks like this:
public class MyBackingObject {
List myClasses;
// accessors
}
My Form Controller registers the custom property editor like this:
...
binder.registerCustomEditor(
MyClass.class,
"myClasses",
new MyClassPropertyEditor());
...
According to the JavaDoc for DataBinder.registerCustomEditor:
If the field denotes an array or Collection, the PropertyEditor will get applied either to the array/Collection itself (the PropertyEditor has to create an array or Collection value) or to each element (the PropertyEditor has to create the element type), depending on the specified required type.
I interpreted this as meaning that since the field I specified ("myClasses") is a collection, but the class I specified is not a collection "MyClass.class", then DataBinder would use the PropertyEditor to create the elements. But this is not the case. DataBinder basically ignores my customEditor because the type registered "Role.class" doesn't match the actual type "List.class". The list ends up getting populated with Strings, and I end up with a ClassCastException.
So my question is, am I interpreting the JavaDoc right?
Note: as a work around, I created a custom CustomCollectionEditor that takes an instance of a PropertyEditor which it uses to populate a collection.
public class MyCollectionPropertyEditor extends CustomCollectionEditor {
PropertyEditor propEditor;
public CollectionPropertyEditor(Class collectionType, PropertyEditor propEditor) {
super(collectionType);
this.propEditor = propEditor;
}
protected Object convertElement(Object element) {
Object returnMe = null;
if(element instanceof String) {
//not thread safe, is that a problem?
propEditor.setAsText((String)element);
returnMe = propEditor.getValue();
}
else {
returnMe = element;
}
return returnMe;
}
}
And I register like this:
...
binder.registerCustomEditor(
List.class,
"myClasses",
new MyCollectionPropertyEditor(
List.class, new MyClassPropertyEditor()));
...
Any opinions on this solution?
My JSP has a multi-select box. I want the values selected to be converted into a List of my custom class objects. I followed this posting:
http://forum.springframework.org/showthread.php?t=17646
Which works fine when using an array as the command property, but fails when using a collection.
My Command Class looks like this:
public class MyBackingObject {
List myClasses;
// accessors
}
My Form Controller registers the custom property editor like this:
...
binder.registerCustomEditor(
MyClass.class,
"myClasses",
new MyClassPropertyEditor());
...
According to the JavaDoc for DataBinder.registerCustomEditor:
If the field denotes an array or Collection, the PropertyEditor will get applied either to the array/Collection itself (the PropertyEditor has to create an array or Collection value) or to each element (the PropertyEditor has to create the element type), depending on the specified required type.
I interpreted this as meaning that since the field I specified ("myClasses") is a collection, but the class I specified is not a collection "MyClass.class", then DataBinder would use the PropertyEditor to create the elements. But this is not the case. DataBinder basically ignores my customEditor because the type registered "Role.class" doesn't match the actual type "List.class". The list ends up getting populated with Strings, and I end up with a ClassCastException.
So my question is, am I interpreting the JavaDoc right?
Note: as a work around, I created a custom CustomCollectionEditor that takes an instance of a PropertyEditor which it uses to populate a collection.
public class MyCollectionPropertyEditor extends CustomCollectionEditor {
PropertyEditor propEditor;
public CollectionPropertyEditor(Class collectionType, PropertyEditor propEditor) {
super(collectionType);
this.propEditor = propEditor;
}
protected Object convertElement(Object element) {
Object returnMe = null;
if(element instanceof String) {
//not thread safe, is that a problem?
propEditor.setAsText((String)element);
returnMe = propEditor.getValue();
}
else {
returnMe = element;
}
return returnMe;
}
}
And I register like this:
...
binder.registerCustomEditor(
List.class,
"myClasses",
new MyCollectionPropertyEditor(
List.class, new MyClassPropertyEditor()));
...
Any opinions on this solution?