tags:

views:

52

answers:

2

I have a Collection of model objects in my backing bean, and I want to use the f:selectItems tag to display them in a h:selectOneMenu. I have read many tutorials that recommend using a collection of SelectItem objects instead of my model POJOs. However, I do not want to do this because it ties your model to a faces presentation. Is there any way to achieve this? I keep getting a Servlet Exception - "incompatible with javax.faces.model.SelectItem." I have posted a few sample snippets below:

Java:

@Component(value = "headerBean")
@Scope(value = "session")
class HeaderBean{

private Collection<ValueObject> myCollectionOfValueObjects = new ArrayList<ValueObject>();
// ...
// getter, setters, whatnot...
// ...
}

JSP:

<h:selectOneMenu id="selectMenu" value="#{headerBean.myValueObject}">
    <f:selectItems value="#{headerBean.myCollectionOfValueObjects}"/> 
</h:selectOneMenu>
A: 

You have to use SelectItem only.

Use converter for the same , here is an article

org.life.java
A: 

You need a SelectItem because this is the way JSF knows what to display and what to pass as a value.

Perhaps it would be nice if you could specify something like:

<f:selectItems collection="#{bean.colelction}" itemKey="id" itemLabel="name" /> - but you can't in JSF 1.2. It is available in JSF 2.0, however.

Bozho