All the questions asking: how can I bind POJOs to h:selectXX with f:selectItems end up with answer "use a converter". However, it seems it is possible to go without the converter - see:
Facelet:
<h:selectManyListbox value="#{pojoBean.selected}">
<f:selectItems value="#{pojoBean.allItems}" var="i" itemValue="#{i}" itemLabel="#{i.txt}" />
</h:selectManyListbox>
Bean:
public class PojoBean {
List<MyItem> selected;
List<MyItem> allItems;
POJO:
public class MyItem {
private String txt;
...}
Note that this seems to work only with h:selectManyListbox, when the value/s being selected end up in a list, not in a single property.
Question - why doesn't it work with h:selectOneMenu and etc?
Likely your MyItem
class has already the toString()
overridden which returns the txt
and you were plain printing selected
as follows to determine the selected values:
System.out.println(selected);
Try to cast each item of selected
back to MyItem
:
for (MyItem myItem : selected) {
System.out.println(myItem);
}
You'll see that it fails with ClassCastException
, because it's actually a String
. So yes, you still need a converter here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.