It's a very common problem.
To set a List<Size>
in Product instance from the post data, that is a string colon separated list with selected sizes, all you need to do is tell the framework how to convert a size String
to a Size
instance. The most common approach is registering a PropertyEditor
on WebDataBinder
class SizePropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
Size size = stringToSize(text); // write this code
setValue(size);
}
}
and register the property editor in Controller using @InitBinder
annotation
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Size.class, new SizePropertyEditor());
}
For a more generic approach see ConversionService.