2

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 ??

Giann
  • 3,142
  • 3
  • 23
  • 33
Hirren Gamit
  • 99
  • 2
  • 3
  • 10
  • 3
    Put your mouse above the `[selectonemenu]` tag which you (correctly!) placed on the question until a black info box shows up and then click therein the *info* link. – BalusC Dec 14 '12 at 14:19

4 Answers4

1

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>
nayef
  • 408
  • 2
  • 4
  • 10
  • [It's bad practice to perform business job in getter methods](http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times/2090062#2090062). – BalusC Dec 22 '12 at 15:24
  • then where do you suggest i should have done it? in another method? and then call that inside getter? – nayef Dec 23 '12 at 17:33
  • Just in bean's (post)constructor. See also the link behind the comment. – BalusC Dec 23 '12 at 17:36
  • thanx. that helped. what happens if in a conversational scoped work flow i load a list from db initially, but during the workflow new rows are inserted in db? the list wont be updated as the initial list will be used. whereas if i loaded it everytime the getter is called then this problem wont happen. your answer will be very much appreciated @BalusC – nayef Dec 24 '12 at 10:28
  • ^ I have the same problem. My data is updated later. – Makky Apr 17 '13 at 14:12
0

You have to make use of the f:selectItems tag:

<h:selectOneMenu value="#{yourBean.itemValue}">                     
    <f:selectItems value="#{yourBean.yourItems}" />
</h:selectOneMenu>

Then, YourBean needs to have a Map field, storing the values fetched from the database where the key of the map (which I suggest to be a String) is the label and the value is the associated object.

Map<String,YourObject> yourItems = new HashMap<String,YourObject>();

public Map<String,YourObject> getYourItems() {
    return yourItems;
}
Fritz
  • 9,987
  • 4
  • 30
  • 49
0

try something like this

xhtml

    <h:form>
        <h:panelGrid>
            <h:selectOneMenu value="#{myMB.id}">
                <f:selectItem itemLabel="Please select one" itemValue="#{null}" />
                <f:selectItems value="#{myMB.items}" />
            </h:selectOneMenu>
        </h:panelGrid>
        <h:commandButton action="#{myMB.go}" value="Go"/>
    </h:form>

mb

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

@ManagedBean
@ViewScoped
public class MyMB implements Serializable{

    private static final long serialVersionUID = 1L;
    private List<SelectItem> items = new ArrayList <SelectItem> ();
    private Long id;

    @PostConstruct
    public void init(){
        SelectItem si = new SelectItem();
        si.setLabel("My Label");
        si.setValue(666L);

        items.add(si);
    }

    public List<SelectItem> getItems() {
        return items;
    }

    public void setItems(List<SelectItem> items) {
        this.items = items;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void go(){
        System.out.println(id);
    }
}
0

You can just use a loop to get data from your list like this:

<h:outputText value="Role :" />
<p:selectOneMenu value="#{myBean.id_Role}">
    <c:forEach var="role" items="#{myBean.rolesList}">
        <f:selectItem itemLabel="${role.name_Role}" itemValue="${role.id_Role}" />
    </c:forEach>
</p:selectOneMenu>

And in your bean:

private List<Role> rolesList;
//And then fill the list from database 

In my case I use hibernate to get data from the database, it worked for me.

I hope it's helpful!

cнŝdk
  • 31,391
  • 7
  • 56
  • 78