selectOneMenu Dynamique values : If you want a dynamique selectOnMenu value you have to use <f:selectItems> whose value points to a List<T> property which you preserve from the DB during bean’s (post)construction. Here’s a basic kickoff example assuming that T actually represents a String.

<h:selectOneMenu value="#{}">
    <f:selectItems value="#{bean.names}" />

With :

public class Bean {

    private String name;
    private List<String> names; 

    private NameService nameService;

    public void init() {
        names = nameService.list();

    // ... (getters, setters, etc)

Simple as that. Actually, the T‘s toString() will be used to represent both the dropdown item label and value. So, when you’re instead of List<String> using a list of complex objects like List<someEntity> and you haven’t overridden the class’ toString() method, then you would see com.example.SomeEntity@hashcode as item values. See next section how to solve it properly.

Also note that the bean for <f:selectItems> value does not necessarily need to be the same bean as the bean for <h:selectOneMenu> value. This is useful whenever the values are actually applicationwide constants which you just have to load only once during application’s startup. You could then just make it a property of an application scoped bean.

<h:selectOneMenu value="#{}">
    <f:selectItems value="#{data.names}"  />

If you have a list of Object you juste use these code :

<h:selectOneMenu value="#{bean.objectV}">
    <f:selectItems value="#{data.objects}" var="d" itemLabel="#{d.nom}" itemValue="#{}" />


  1. Pingback: JSF h:selectOneMenu Example » JavaTuto

Laisser un commentaire