First, you're not allowed to nest <h:selectOneMenu>
component(s) within <h:commandButton>
!
Here's a proper structure of your <h:form>
<h:form>
<h:commandButton action="sample?faces-redirect=true" value="submit" />
<h:selectOneMenu id="sampleSearch" value="#{cBean.id}">
<f:selectItem id="id" itemLable="idText" itemValue="By Text" />
<f:selectItem id="idnumeric" itemLable="idNumeric" itemValue="Number" />
<f:selectItem id="product" itemLable="Product" itemValue="Main Product" />
<f:selectItem id="lonumber" itemLable="loNumber" itemValue="LoNumber" />
<f:selectItem id="formula" itemLable="formula" itemValue="By Formula" />
</h:selectOneMenu>
</h:form>
Then, in order to get the dropdown list options from the database, you can consider using the <f:selectItems>
component (and get rid of those <f:selectItem>
s) and pass a List<T>
from the managed bean to the components value
property.
The selectOneMenu
would then look like this:
<h:selectOneMenu value="#{cBean.id}">
<f:selectItems value="#{cBean.values}"
var="item"
itemLabel="#{item.label}"
itemValue="#{item.value}"/>
</h:selectOneMenu>
As for the managed-bean, it's now supposed to provide a public List<T> getValues()
method, which will return a list with the objects that will populate the dropdown.
When T
is a complex Java object, such as Item
which has a String property of label
and value
, then you could use the var
attribute to get hold of the iteration variable which you in turn can use in itemValue
and/or itemLabel
attribtues (if you omit the itemLabel
, then the label becomes the same as the value).
Let's say:
@ManagedBean
@RequestScoped
public class CBean {
public List<Item> getValues() {
List<Item> result = new ArrayList<Item>();
//..call-back to web-service, db, etc. and populate the result variable.
return result;
}
}
The Item
class would look like this:
public class Item {
private String label;
private String value;
//getters, setters.
}
You can read more here: