I am trying to display a collection field of command objects inside a list box. Inside the collection there is a field, identifier and name. I want to use id as the value of the html parameter and the name as the option text. See code below;
<form:select id="customCollection" path="customCollection" size="10"> <form:options items="${command.customCollection}" itemValue="id" itemLabel="name"/> </form:select>
The name prints fine, but the value remains blank. Here is the HTML output;
<option selected="selected" value="">name-value</option>
My initial assumption was that my data was incorrect, but after posting the following code on my page:
<c:forEach items="${command.customCollection}" var="c"> ${c.id} : ${c.name} <br> </c:forEach>
The identifier and name will be printed correctly. Therefore, my data is correctly delivered in my opinion. This makes me assume that I am either using the form: the parameters are incorrect, or some errors in the form fall: parameters.
Can someone help me here?
EDIT:
Thanks to the help of BacMan and delfuego, I was able to narrow this problem down to my binder.
I used to assign a value in my element to a row name, here is my original binder,
binder.registerCustomEditor(Collection.class, "customCollection", new CustomCollectionEditor(Collection.class) { @Override protected Object convertElement(Object element) { String name = null; if (element instanceof String) { name = (String) element; } return name != null ? dao.findCustomByName(name) : null; } });
When I remove this code from my initBinder method, the string value is correctly inserted into the form, but I need customEditor to convert the specified value to a database object.
So this is my new attempt at linking;
binder.registerCustomEditor(Collection.class, "customCollection", new CustomCollectionEditor(Collection.class) { @Override protected Object convertElement(Object element) { Integer id = null; if (element instanceof Integer) { id = (Integer) element; } return id != null ? dao.find(Custom.class, id) : null; } });
However, this causes the same behavior as the previous binder, and makes the value not displayable. Any ideas on what I'm doing wrong here?
EDIT 2:
As I mentioned above, if I comment on my custom binder, then the user object will load its identifier and name correctly for the part of the form submission, but then it will never be associated with the parent object when I try to save it. So I really think the problem is with my binder.
I placed debug statements inside my convertElement method. Everything looks as it should work, dao correctly pulls objects from the database. The only behavior that seems suspicious to me is that the convertElement method is called twice for each custom item.