JSF selectItems Tag
This tag is used to add a set of items to the nearest enclosing parent (select one or select many) component. This tag can be used to get the list of choices from the list of objects from backing bean. So instead of writing many selectItem tag for choices, you can use selectItems tag to get the options list in the form of list of objects from backing bean.
Code Description :
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <f:view> <html> <body> <h:form> <h:outputText value="Select choices given below :"/><br><br> <h:selectManyListbox id="subscriptions" value="#{SItemsBean.options}" size="3"> <f:selectItems value="#{SItemsBean.options}" /> </h:selectManyListbox> </h:form> </body> </html> </f:view> |
Backing Bean (SItemsBean.java) : In the code below, we have taken array list of SelectItem objects. It has 5 constructors. One of its constructor accepts value as Object and label as String parameter while the another one accepts value as Object, label as String, description as String disabled as boolean parameters. description parameter is used for own purpose to describe something and disabled is used to make the option enabled or disabled by setting it "true" and "false" respectively. You can use them according to the need. You can understand it better by the code given below :
import javax.faces.model.SelectItem; import java.util.*; public class SItemsBean { private List options; public SItemsBean() { options = new ArrayList(); SelectItem option = new SelectItem("ch1", "choice1", "This bean is for selectItems tag", true); options.add(option); option = new SelectItem("ch2", "choice2"); options.add(option); option = new SelectItem("ch3", "choice3"); options.add(option); option = new SelectItem("ch4", "choice4"); options.add(option); option = new SelectItem("ch5", "choice5); options.add(option); } public void setOptions(List opt) { options = opt; } public List getOptions() { return options; } } |
Rendered Output :
Html Source Code :
<html> <body> <form id="_id0" method="post" action="/coretag/pages/selectItems.jsf" enctype="application/x-www-form-urlencoded"> Select choices given below :<br><br> <select id="_id0:subscriptions" name="_id0:subscriptions" multiple size="3"> <option value="ch1" disabled="disabled">choice1</option> <option value="ch2">choice2</option> <option value="ch3">choice3</option> <option value="ch4">choice4</option> <option value="ch5">choice5</option> </select> <input type="hidden" name="_id0" value="_id0" /></form> </body> </html> |
This tag contains some attributes :
id : This is used to uniquely identify the table component. This must be unique within the closest parent component.
binding : It is a value binding expression that is used to link component to a property in a backing bean.
value : This is the value binding expression that indicates to the list
or array of selectItem instances which contains the information about the option.