Yes, you can return a list or POJO array instead of SelectItems. To do this, you will need a converter, but this is not very important. So, first the converter:
@FacesConverter(forClass=Hero.class) public class HeroConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { return new Hero(value); } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { return ((Hero)value).getName(); } }
Now, if you return the Hero es list to <f:selectItems> , you have the parameters in HTML, where the label is Hero.toString() , and the value is returned from HeroConverter.getAsString() .
One more thing. If you provide some value for this selection, JSF converts it to an object and checks (using the equals () method) if this object was in the list of objects to select. So, in the case above, you need to override equals() in Hero to check if the names match. Another solution is not to create a new instance in getAsObject , but to save the list of available Hero es and return this list to <f:selectionItems> and return the object from this list to getAsObject() .
amorfis
source share