When your jsf page needs to use values backed by an entity, here is how I do it in a simple way by writting a custom tag. All I need to do in jsf is declare it like this:
<x:xSelectItems value="#{myEntity.booklist}" xLabelMethod="getTitle" xValueMethod="getId" />
or like this if your list is a type of string:
<x:xSelectItems value="#{myEntity.booklist}" xLabelMethod="toString" xValueMethod="toString" />
you then write a UI component which extends UISelectItems and it has a method to convert your data list to SelectItem:
private List convertToSelectItems(List a_List,
String labelString, String valueString) throws Exception {
List result = new ArrayList();
for (Object item : a_List) {
Method labelMethod = item.getClass().getDeclaredMethod(labelString);
Object label = labelMethod.invoke(item);
Method valueMethod = item.getClass().getDeclaredMethod(valueString);
Object value = valueMethod.invoke(item);
result.add(new SelectItem(value, label.toString()));
}
return result;
}
This way, you don't have to do anything with your entity bean. Hope it's helpful for you.