JSF Tutorial - JSF Form ListBox Example








The following sections show how to create ListBox in JSF.

The h:selectOneListbox tag renders an HTML input element of the type "select" with size specified.

The following JSF code

<h:selectOneListbox value="#{userData.data}">
   <f:selectItem itemValue="1" itemLabel="Item 1" />
   <f:selectItem itemValue="2" itemLabel="Item 2" />                   
</h:selectOneListbox>

are rendered into the following HTML tags.

<select name="j_idt6:j_idt8" size="2">  
   <option value="1">Item 1</option>
   <option value="2">Item 2</option>
</select>




Tag Attributes

AttributeDescription
idid for the tag
bindingReference to the component used in a backing bean
renderedA boolean value; false would suppress rendering
styleClassCascading stylesheet (CSS) class name
valuevalue binding
valueChangeListenerA method binding that responds to value changes
converterConverter class name
validatorClass name of a validator attached to the component
requiredA boolean; if true, marks the tag as required
accesskeygives focus to an element
acceptComma-separated list of content types for a form
accept-charsetComma- or space-separated list of character encodings for a form.
altAlternative text for nontextual elements such as images
borderPixel value for an element's border width
charsetCharacter encoding for a linked resource
coordsCoordinates for an element whose shape is a rectangle, circle, or polygon
dirDirection for text. Valid values are ltr (left to right) and rtl (right to left).
disabledDisabled state of an input element or button
hreflangBase language of a resource specified with the href attribute;
langBase language of an element's attributes and text
maxlengthMaximum number of characters for text fields
readonlyRead-only state of an input field
styleInline style information
tabindexNumerical value specifying a tab index
targetThe name of a frame in which a document is opened
titleA title used for accessibility. Browsers typically create tooltips for the title's value
typeType of a link; for example, stylesheet
widthWidth of an element
onblurEvent handler for losing focus
onchangeEvent handler for value changes
onclickEvent handler for Mouse button clicked over the element
ondblclickEvent handler for Mouse button double-clicked
onfocusEvent handler for element received focus
onkeydownEvent handler for Key pressed
onkeypressEvent handler for Key pressed and released
onkeyupEvent handler for Key released
onmousedownEvent handler for Mouse button pressed
onmousemoveEvent handler for mouse moved
onmouseoutEvent handler for mouse left
onmouseoverEvent handler for mouse moved onto
onmouseupEvent handler for mouse button released
onresetEvent handler for form reset
onselectEvent handler for Text selected
immediateProcess validation early in the life cycle




Hardcoded ListBox

The following code is from UserBean.java.

package com.java2s.common;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
  public String item;
  
  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }    
}

The following code is from demo.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
        Hard-coded with "f:selectItem" : 
       <h:selectOneListbox value="#{user.item}">
         <f:selectItem itemValue="1" itemLabel="item 1" />
         <f:selectItem itemValue="2" itemLabel="item 2" />
         <f:selectItem itemValue="3" itemLabel="item 3" />
       </h:selectOneListbox>

    <br /><br />
      <h:commandButton value="Submit" action="result" />
    <h:commandButton value="Reset" type="reset" />
      </h:form>
    </h:body>
</html>

The following code is from result.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html">
<h:body>
  <p>Selected: #{user.item}</p>
</h:body>
</html>


Download Form_ListBox_Hardcode.zip

ListBox Generated by Map

The following code is from result.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html">
<h:body>
  <p>Selected: #{user.item}</p>
</h:body>
</html>

The following code is from UserBean.java.

package com.java2s.common;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
  public String item;
  
  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }  
  //Generated by Map
  private static Map<String,Object> itemValue;
  static{
    itemValue = new LinkedHashMap<String,Object>();
    itemValue.put("Item 1", "1"); //label, value
    itemValue.put("Item 2", "2");
    itemValue.put("Item 3", "3");
  }  
  public Map<String,Object> getItemValue() {
    return itemValue;
  }    
}

The following code is from demo.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
        Generated by Map :
       <h:selectOneListbox value="#{user.item}">
         <f:selectItems value="#{user.itemValue}" />
       </h:selectOneListbox>

    <br /><br />
      <h:commandButton value="Submit" action="result" />
    <h:commandButton value="Reset" type="reset" />
      </h:form>
    </h:body>
</html>


Download Form_ListBox_Generated_by_Map.zip

ListBox Inner Class

The following code is from UserBean.java.

package com.java2s.common;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
  public String item;
  
  public String getItem() {
    return item;
  }

  public void setItem(String i) {
    this.item = i;
  }  
  public static class Item{
    public String label;
    public String value;
    
    public Item(String l, String v){
      this.label = l;
      this.value = v;
    }
    
    public String getLabel(){
      return label;
    }
    
    public String getValue(){
      return value;
    }
    
  }
  public Item[] itemList;
  
  public Item[] getItemValue() {
    itemList = new Item[3];
    itemList[0] = new Item("item - 1", "1");
    itemList[1] = new Item("item - 2", "2");
    itemList[2] = new Item("item - 3", "3");
    
    return itemList;
  }    
}

The following code is from result.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html">
<h:body>
  <p>Selected: #{user.item}</p>
</h:body>
</html>

The following code is from demo.xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
      <h:form>
        Generated by Object array and iterate with var :
       <h:selectOneListbox value="#{user.item}">
         <f:selectItems value="#{user.itemValue}" var="y"
         itemLabel="#{y.label}" itemValue="#{y.value}" />
       </h:selectOneListbox>

    <br /><br />
      <h:commandButton value="Submit" action="result" />
    <h:commandButton value="Reset" type="reset" />
      </h:form>
    </h:body>
</html>


Download Form_ListBox_Inner_Class.zip

To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml