I am trying to display my command objects collection field inside a list box. Inside said collection is a field, id and name. I want use the id as the html option value and the name as the option text. See the code below;
<form:select id="customCollection" path="customCollection" size="10">
<form:options items="${command.customCollection}" itemValue="id" itemLabel="name"/>
</form:select>
Name prints out fine, but value is left blank. Here is the output HTML;
<option selected="selected" value="">name-value</option>
My initial assumption was that my data was incorrect, but after putting the following code in my page;
<c:forEach items="${command.customCollection}" var="c">
${c.id} : ${c.name} <br>
</c:forEach>
both the id and the name are correctly printed out. So my data is correctly being delivering to my view. Which makes me assume I am either using form:options incorrectly or hitting some bug in form:options.
Can anyone help me out here?
EDIT:
Thanks to the help of BacMan and delfuego, I've been able to narrow down this issue to my binder.
Previously I was assigning the value in my element to the name of the row, here is my initial 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 row value is correctly inserted into the form, but I need a customEditor to convert said value into a database object.
So this is my new attempt at a binder;
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 is causing the same behavior as the previous binder and making the value not show up. Any ideas about what I am doing wrong here?
EDIT 2:
As I mentioned above, if I comment out my custom binder then the Custom object does load its id and name correctly for the view portion of the form, but then never binds back into the parent object when I attempt to save it. So I really think the issue is with my binder.
I've placed debugging statements inside my convertElement method. Everything looks like it should be worked, the dao is correctly pulling objects from the database. The only behavior that strikes me as suspect is that the convertElement method is called twice for each Custom item.