Jan 4th, 2009, 11:17 PM
Checkbox data binding with Object collections
I have read through most of the available material on using the 'checkboxes' input tag but am having some trouble understanding exactly what is going on which is preventing me from implementing what I would like to do.
Basically, I have a class structure like this:
class Group implements Member;
class Item implements Member;
Group contains a private List<Member> that can contain either other Group objects or Item objects. Now, I would like to implement a page to edit a Group object and add/remove Group or Item objects to that list. The approach that I am thinking right now starts like this:
1. implement formBackingObject() to retrieve the particular Group object the user would like to edit (passed in via an HTTP parameter).
2. in referenceData(), retrieve all available Group and Item objects and put them in one List, then remove the Group/Item members that the object returned by formBackingObject() does not contain
3. In my JSP, use <form:checkboxes ...> to make the list of Items or Groups available to the end user to check/uncheck as they please
4. Magic occurs
5. The next call to 'getMembers()' on the Group object (in the processFormSubmission() method) will now contain all the appropriate Objects!
As you can see, I am not sure what to do about step 4 (and questioning whether step 3 is the best approach to take). Some of what I have looked at indicates that I should potentially write my own custom data binder, and to implement itemValues that are something like "item1", "item2", "group1", "group2". The custom binder would resolve those to Item and Group objects, create a new List, and call setMembers on the command object to bind that, but that seems far more complicated.
Can any provide any insight on how I might solve this problem? I think the crux of it is my not understanding how Spring binds checkboxes to a Collection (but I have read pages 357/358 of the spring reference pdf where it describes the 3 approaches taken, it just hasn't clicked yet).
Thanks in advance.