The f:selectItem and f:selectItems tags represent components that can be nested inside a SelectOne or a SelectMany component. A f:selectItem tag is associated with a SelectItem instance, which contains the value, label, and description of a single item in a SelectOne or SelectMany component. The SelectItems instance represents a set of SelectItem instances, containing the values, labels, and descriptions of the entire list of items
You can use either a set of f:selectItem tags or a single f:selectItems tag within your SelectOne or SelectMany component tag.
The advantages of using the f:selectItems tag are as follows:
An item can be represented by using different data structures, including Array, Map and Collection. The data structure is composed of SelectItem instances.
The value of f:selectItems tag can even represent a generic collection of POJOs.
Different lists can be concatenated together into a single SelectMany or SelectOne component and group the lists within the component.
Values can be generated dynamically at runtime.
The advantages of using f:selectItem are as follows:
Items in the list can be defined from the page.
Less code is needed in the bean for the selectItem properties.
For more information on writing component properties for the SelectItems components, see Writing Bean Properties. The rest of this section shows you how to use the f:selectItems and f:selectItem tags.
The following example from Rendering Components for Selecting Multiple Values shows how to use the h:selectManyCheckbox tag:
<h:selectManyCheckbox id="newsletters" layout="pageDirection" value="#{cashier.newsletters}"> <f:selectItems value="#{newsletters}"/> </h:selectManyCheckbox>
The value attribute of the f:selectItems tag is bound to the backing bean newsletters , which is configured in the application configuration resource file.
You can also create the list corresponding to a SelectMany or SelectOne component programmatically in the backing bean. See Writing Bean Properties for information on how to write a backing bean property corresponding to a SelectMany or SelectOne component.
The arguments to the SelectItem constructor are as follows:
An Object representing the value of the item
A String representing the label that displays in the SelectMany component on the page
A String representing the description of the item
SelectItems Properties describes in more detail how to write a backing bean property for a SelectItems component.
The f:selectItem tag represents a single item in a list of items. Here is the example from Displaying a Menu Using the h:selectOneMenu Tag once again:
<h:selectOneMenu id="shippingOption" required="true" value="#{cashier.shippingOption}"> <f:selectItem itemValue="2" itemLabel="#{bundle.QuickShip}"/> <f:selectItem itemValue="5" itemLabel="#{bundle.NormalShip}"/> <f:selectItem itemValue="7" itemLabel="#{bundle.SaverShip}"/> </h:selectOneMenu>
The itemValue attribute represents the default value of the SelectItem instance. The itemLabel attribute represents the String that appears in the drop-down menu component on the page.
The itemValue and itemLabel attributes are value-binding-enabled, meaning that they can use value-binding expressions to refer to values in external objects. They can also define literal values, as shown in the example h:selectOneMenu tag.