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 ??
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 ??
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>
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;
}
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);
}
}
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!