Hi everyone!
First, a big thank you to Cagatay! I was about to agree with Simon that
f:selectItems couldn't do this (judging from the documentation), but it
seems that I didn't even try it without the other attributes. I just
tried it out and this actually works like expected (selectedRoles being
a Long[] and allRoles a Map<String, Long>):
<h:selectManyCheckbox id="selectedRoles" value="#{myBean.selectedRoles}">
<f:selectItems value="#{myBean.allRoles}"/>
</h:selectManyCheckbox>
The same with t:selectItems does not work, even if the documentation
states that it should. ;-)
Simon Kitching wrote on 14.03.2009 20:41:
Method createSelectItems contains:
List items = new ArrayList();
if (value instanceof SelectItem[]) {
return (SelectItem[]) value;
}
else if (value instanceof Collection) {
....
}
else if (value instanceof Map) {
Map map = (Map) value;
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();)
{
Entry currentItem = (Entry) iter.next();
putIteratorToRequestParam(currentItem.getValue());
SelectItem selectItem = createSelectItem();
removeIteratorFromRequestParam();
items.add(selectItem);
}
}
return (SelectItem[]) items.toArray(new SelectItem[0]);
So clearly, when evaluating the value expression returns a Map, then the
component returns an array of SelectItem objects that have been built
from the map values.
It should do that, but I think building the select items from the map is
either broken or the documentation states too much. As you can see in
above code, only the value of the map entry is used at all, the key
isn't. This:
putIteratorToRequestParam(currentItem.getValue());
SelectItem selectItem = createSelectItem();
removeIteratorFromRequestParam();
only works if a variable name is set (var attribute), otherwise you get
the NullPointerException I included in my last mail. If you set a var
name, then the createSelectItem method creates a select item. It only
has the map value to work with, though. You could put an object in the
map as value and do something like label="#{myObject.name}"
value="#{myObject.value}". But I don't see a way to use the map's key
explicitely, and there's no mechanisms that detects the missing var
attribute and just uses the keys and values of the map as default
strategy (as far as I can see).
If f:selectItems can do this and the documentation of t:selectItems says
this is possible, I think t:selectItems should be able to do this as
well. Personally, I'm happy to use f:selectItems, but if you agree I
could create a bug report for this. I just did try this with the latest
nightly snapshot of tomahawk12-1.1.9 and see the same behavior there,
too (the NullPointerException if used withut other attributes).
And in any case thanks to both Simon and Cagatay for answering my mails
and providing me with the needed input to solve the problem! ;-)
Greetings,
Johannes