how to populate h:selectOneMenu from the database table in jsf ?

18,350

you can use a list of SelectItem for this. you will need a method that generates a list of selectitems like the following one in you managed bean,

public List<SelectItem> getAllCatagories(){

   List<SelectItem> items = new ArrayList<SelectItem>();
   List<Category> categoryList = dao.getAllCategory();
    for(Category category: categotyList){
       items.add(new SelectItem(category.getCategoryId(), category.getName()));
   }
   return items;
}

and use it like this

<h:selectOneMenu value="#{controllerBean.selectedCategory}" >
            <f:selectItems value="#{controllerBean.allCategories}"/>
</h:selectOneMenu>
Share:
18,350
Hirren Gamit
Author by

Hirren Gamit

Updated on June 25, 2022

Comments

  • Hirren Gamit
    Hirren Gamit almost 2 years

    I have a category table

     categoryId
     catName
     description
     image 
    

    I want to populate the <h:selectOneMenu> with the itemLabel categoryName and it's value with categoryId.

    It should be done with the ManagedBean how can i do this ??