This article was last updated on : November 4th, 2016 at 10:32 am

JSF 1.2 Select One Listbox (h:selectOneListbox)

Tutorial JSF (1.2) – Learn Select One Listbox with example

Describing Example :
1. selectOneListbox in JSF allows single selection from a listbox or list of values. In this example we will demonstrate how to work with Select One Listbox in JSF 1.2. We will take one example to see Select One Listbox (h:selectOneListbox) in JSF.

2. Let us take an example suppose we are creating our user account on any Job Portal website like There will be any field to select our the highest qualification. It means there will be more than one qualification list but user should be allowed to select one value from the list box. In this case JSF UI component – Select One Listbox h:selectOneListbox will be used.

3. We have these files while doing this example

  1.  qualificationForm.jsp
  2. showQualificationSelected.jsp
  4. SelectOneListboxView.javas
  5. faces-config.xml
  6. web.xml

4. Eclipse image for this project.

This example is showing the eclipse image of project

Simply in this Select One Listbox() in JSF-1.2 example we will show JSF- Select One Listbox in one page and in the next page we will display the value selected by user.
Please see the image of Select One Listbox on UI.

This image is showing how select one listbox looks on UI

Please follow these procedures to do this Example

1. Creating a dynamic web application :

In this step we have to create a dynamic web application with name “jsfSelectOneListboxExample”.

2. Create JSP Pages :
Now we have to create two JSP pages

  1.  qualificationForm.jsp – In this page we will show Select One List Box with values “B.Tech.”, “M.Tech.”, “PHD” and “Other”. User will be allowed to select one value of them.
  2. showQualificationSelected.jsp – In this page we will show the qualification values selected in the previous page.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<[email protected] uri="" prefix="h"%>
<[email protected] uri="" prefix="f"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Select One Listbox h:selectOneListbox JSF-1.2 Example</title>
<h:outputText value="Please Select Your Qualification :"></h:outputText>
<f:selectItems value="#{jsfSelectOneListboxMBean.selectOneListboxView.qualificationList}"/>
<h:commandButton value="Show Qualification" action="#{jsfSelectOneListboxMBean.showHighestQualification}"></h:commandButton>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<[email protected] uri="" prefix="h"%>
<[email protected] uri="" prefix="f"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Select One Listbox - h:selectOneListbox - JSF-1.2 Example</title>
<h:panelGrid columns="2" cellpadding="1" cellspacing="1">
<h:outputText value="Your Qualification is : "></h:outputText>
<h:outputText style="color:red;"

Points Of Discussion About JSP’s :

Value Binding In JSF :
In the JSP pages you can see value expressions like “#{jsfSelectOneListboxMBean.selectOneListboxView.qualificationList}”

“jsfSelectOneListboxMBean” is used for managed bean for this example.  It is representing class. Please see faces-config.xml file to see this configuration. We will configure this managed bean in faces-config.xml file inside .

“selectOneListboxView” is representing View Model. In this example is View Model. View Model is used to get & set values of the form field attribute. “selectOneListboxView” has been declared as an instance variable in class. Please see class.

“qualificationList” and “qualificationSelected” are variables which have been declared in We have get and set methods for these attributes in We can get attribute value by calling get method and we can assign value to the attribute by using set methods of corresponding attributes.

Method or Action In JSF (Page Submit) :
In qualificationForm.jsp you can see this action  call

“showHighestQualification” is an action method has been written in class. When user will click on “Show Qualification” command button on first output screen, “showHighestQualification” method will be executed.   Please see this method carefully. The return type of this method is String. If the process executed successfully this method will return a string “showTechnologies”.

“showTechnologies” string will be returned to faces-config.xml. When our faces-config.xml will receive “showTechnologies”. It will look into
tag. For outcome “showTechnologies” we have configured our faces-config.xml to render page   showQualificationSelected.jsp.

3. Create Managed Bean For this Example :

In JSF, Managed Bean works as controller. In this example our managed bean is Please see faces-config.xml file to see how can we configure managed bean in JSF framework.
Please find the code for managed bean below.

package com.sitenol.selectOneListbox;
public class JSFSelectOneListboxManagedBean {
private SelectOneListboxView selectOneListboxView;
public SelectOneListboxView getSelectOneListboxView() {
if(selectOneListboxView == null)
selectOneListboxView = new SelectOneListboxView();
return selectOneListboxView;
public void setSelectOneListboxView(SelectOneListboxView selectOneListboxView) {
this.selectOneListboxView = selectOneListboxView;
public String showHighestQualification(){
return "showQualification";

4. Create View Model For this Example :
Inside Managed Bean you can see
private SelectOneListboxView selectOneListboxView;

package com.sitenol.selectOneListbox;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
public class SelectOneListboxView {
private String qualificationSelected;
private List<SelectItem> qualificationList;
public String getQualificationSelected() {
return qualificationSelected;
public void setQualificationSelected(String qualificationSelected) {
this.qualificationSelected = qualificationSelected;
public List<SelectItem> getQualificationList() {
if(qualificationList == null || qualificationList.size() == 0){
qualificationList = new ArrayList<SelectItem>();
qualificationList.add(new SelectItem("B.Tech.", "B.Tech."));
qualificationList.add(new SelectItem("M.Tech.", "M.Tech."));
qualificationList.add(new SelectItem("PHD", "PHD"));
qualificationList.add(new SelectItem("Other", "Other"));
return qualificationList;
public void setQualificationList(List<SelectItem> qualificationList) {
this.qualificationList = qualificationList;

5. Create faces-config.xml file :
Please see below code for the faces-config.xml file for example “Tutorial – Select One Listbox() in JSF-1.2”.


<?xml version="1.0" encoding="UTF-8"?>

6. Create web.xml file :
Please find the web.xml file code  for example “Tutorial – Select One Listbox() in JSF-1.2”.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
<servlet-name>Faces Servlet</servlet-name>
<servlet-name>Faces Servlet</servlet-name>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'

7. Deploy Application and Run Server :
In this step you we have to deploy jsfSelectOneListboxExample.war file to server after proper deployment we have to start the server.

8. Run The Application :
After successfully war deployment. Please start the server. After that open Internet Explorer and hit this “http://localhost:8080/jsfSelectOneListboxExample/qualificationForm.faces”.
So now we can see output of our Select One Listbox() in JSF-1.2.
Out put screen will be like this.
First Screen

This image is showing the first output screen

In this screen we have options (“B.Tech.”, “M.Tech.”, “PHD” and “Other”) for selection. We can select only one value from list.  After that we can click on “showHighestQualification” button.
We will get output screen like Second Screen.
Second Screen

this image is showing the final output screen

This screen is showing the qualification value selected on First Screen.

Share this post:

Related Posts


  1. Avatar for Tarun Singh
    shubhangi garg says:

    I tried this example, But listbox is showing empty. It could not take any value .

    • Avatar for Tarun Singh
      Tarun Singh says:

      Hello Shubhangi, Please check if you have missed file of this example. I have created getQualificationList() list inside Even though it is not working for you, I will send example code to your email id. Each and every example available on this website have been executed.

  2. Avatar for Tarun Singh
    Nyoman says:

    I think you need to delete nborejpct/ folder and build.xml file and create new web application with existing sources. (to be on the save side, delete the stuff with the IDE not running)BTW in Maven it would mean changing packaging from “jar” to “war” for the most part.

Leave a Comment