Search code examples
jsficefacesselectonemenu

Can't bind SelectItem list to <f:selectItem


I am using IceFaces components and I am trying to fill up a select with some values that correspond to a MangedBean property.

<h:form>
    <ice:selectOneMenu size="1" style="width: 180px">
        <f:selectItem value="#{stockManagedBean.listeCategoriesItem}"></f:selectItem>
    </ice:selectOneMenu>
</h:form>

listeCategoriesItem is a property of StockManagedBean and is an ArrayList of SelectItem.

@ManagedBean
public class StockManagedBean {
    CategorieDAO categorieDAO;
    List<SelectItem> listeCategoriesItem;

    public StockManagedBean() {
        categorieDAO = new CategorieDAO();
        listeCategoriesItem = new ArrayList<SelectItem>();    
        List<Categorie> listeCategories = categorieDAO.selectAllCat();
        for(Categorie categorie: listeCategories) {
            listeCategoriesItem.add(new SelectItem(categorie.getCatId(), categorie.getCatNom()));
        }
    }

public List<SelectItem> getListeCategoriesItem() {
    return listeCategoriesItem; 
}

public void setListeCategoriesItem(List<SelectItem> listeCategoriesItem) {
    this.listeCategoriesItem = listeCategoriesItem;
}
}

I tested the values that come from my DAO and they are all correct. I also tested the values of the list in the getter and they are also correct, but when I load my html page, nothing is in the select list...


Solution

  • Use <f:selectItems> instead of <f:selectItem>. Note the s at the end of the former component.

    <ice:selectOneMenu size="1" style="width: 180px">
        <f:selectItems value="#{stockManagedBean.listeCategoriesItem}" />
    </ice:selectOneMenu>
    

    Also, it would be good to also have a field in your bean that will handle the value of the selected item in your selectOneMenu.

    <ice:selectOneMenu size="1" style="width: 180px"
        value="#{stockManagedBean.selectedCategory}">
        <f:selectItems value="#{stockManagedBean.listeCategoriesItem}" />
    </ice:selectOneMenu>
    

    And in your managed bean:

    @ManagedBean
    public class StockManagedBean {
        private String selectedCategory;
        //rest of your code
        //getters and setters...
    }