Selection tags permit a user to select one or more items from collections variously presented as lists, menus, radio buttons, etc. Selection tags must be within the scope of an h:form tag to have their values communicated to the server.
The images in the "Images" menu items show variations in the use of
these tags.
The index.xhtml
page in the select example
demonstrates their use.
<h:selectBooleanCheckbox value="#{form.contactMe}"/>and back it with the bean property:
private boolean contactMe; public void setContactMe(boolean newValue) { contactMe = newValue; } public boolean getContactMe() { return contactMe; }
<h:selectManyCheckbox value="#{form.colors}"> <f:selectItem itemValue="Red" itemLabel="Red"/> <f:selectItem itemValue="Blue" itemLabel="Blue"/> <f:selectItem itemValue="Yellow" itemLabel="Yellow"/> <f:selectItem itemValue="Green" itemLabel="Green"/> <f:selectItem itemValue="Orange" itemLabel="Orange"/> </h:selectManyCheckbox>Note:
<h:selectOneRadio value="#{form.education}"> <f:selectItem itemValue="High School" itemLabel="High School"/> <f:selectItem itemValue="Bachelor's" itemLabel="Bachelor's"/> <f:selectItem itemValue="Master's" itemLabel="Master's"/> <f:selectItem itemValue="Doctorate" itemLabel="Doctorate"/> </h:selectOneRadio>Note that the bean property receiving the selection (in this case form.education) should be a non-array type.
Component | JSF Tag To Render It |
---|---|
<h:selectOneListbox value="#{form.year}" size="5"> <f:selectItem itemValue="1900" itemLabel="1900"/> <f:selectItem itemValue="1901" itemLabel="1901"/> ... </h:selectOneListbox> |
|
<h:selectManyListbox value="#{form.languages}"> <f:selectItem itemValue="English" itemLabel="English"/> <f:selectItem itemValue="French" itemLabel="French"/> <f:selectItem itemValue="Italian" itemLabel="Italian"/> <f:selectItem itemValue="Spanish" itemLabel="Spanish"/> <f:selectItem itemValue="Russian" itemLabel="Russian"/> </h:selectManyListbox> |
<h:selectManyList value="#{form.condiments}"> <f:selectItem itemValue="1" itemLabel="Cheese"/> <f:selectItem itemValue="2" itemLabel="Pickle"/> <f:selectItem itemValue="3" itemLabel="Mustard"/> <f:selectItem itemValue="4" itemLabel="Lettuce"/> <f:selectItem itemValue="5" itemLabel="Onions"/> </h:selectManyList>If the form bean provides an appropriate condimentItems property, the above can be replaced by:
<h:selectManyList value="#{form.condiments}"> <f:selectItems value="#{form.condimentItems}"/> </h:selectManyList>
private static SelectItem[] condimentItems = {
new SelectItem(1, "Cheese"), // value, label
new SelectItem(2, "Pickle"),
new SelectItem(3, "Mustard"),
new SelectItem(4, "Lettuce"),
new SelectItem(5, "Onions")
};
public SelectItem[] getCondimentItems() {
return condimentItems;
}
Be aware that for browsers, values are always strings.
So in the above example, when a user selects a condiment, say mustard, the string
"3" will be sent in the request.
In this case JSF will convert the string "3" to the integer 3,
but for any type other than int, Integer, or enum, you
will have to supply a converter (see chapter 7).