• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSF 2 SelectOneListBox Example

March 22, 2014 //  by Amr Mohammed//  Leave a Comment

JavaServer Faces (JSF 2) provides a number of user interfaces components that cover the most common requirements, one of the most important component is a <h:selectOneListBox/> component. The selectOneListBox is a component that been used for making selection from the list. The using of selectOneListBox component can be achieved by means of using an inline selection values, a list of options defined in the managed bean or by using a pre-defined object is applicable. See SelectOneMenu and SelectManyMenu.

Also Read:

  • JSF 2 Tutorials
  • JSF Tutorials
  • Introduction to JSF

1. The Managed Bean

SelectOneListBoxBean

package net.javabeat.jsf;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SelectOneListBoxBean {

	public List<Tutorial> tutorials;
	public String selectedTuorial;

	public SelectOneListBoxBean(){
		this.tutorials = new ArrayList<Tutorial>();
		this.tutorials.add(new Tutorial(1, "JSF 2"));
		this.tutorials.add(new Tutorial(2, "JPA"));
		this.tutorials.add(new Tutorial(3, "Maven"));
		this.tutorials.add(new Tutorial(4, "Eclipse"));
		this.tutorials.add(new Tutorial(5, "Java"));
		this.tutorials.add(new Tutorial(6, "Java EE"));
	}

	public List<Tutorial> getTutorials() {
		return tutorials;
	}

	public void setTutorials(List<Tutorial> tutorials) {
		this.tutorials = tutorials;
	}

	public String getSelectedTuorial() {
		return selectedTuorial;
	}

	public void setSelectedTuorial(String selectedTuorial) {
		this.selectedTuorial = selectedTuorial;
	}

	public String submitTraining(){
		return "selectedTraining";
	}

}

2. The Tutorial Type (Class)

The provided example depends on the Tutorial type (class) for making an options. This is a third type of making a selections instead of using inline options and SelectItem as you’ve seen at the SelectOneMenu example. This use is also applicable for selectOneMenu.
Tutorial.java

package net.javabeat.jsf;

public class Tutorial {
	private int tutorialId;
	private String tutorialDescription;

	public Tutorial(int id, String desc){
		this.tutorialId = id;
		this.tutorialDescription = desc;
	}

	public int getTutorialId() {
		return tutorialId;
	}
	public void setTutorialId(int tutorialId) {
		this.tutorialId = tutorialId;
	}
	public String getTutorialDescription() {
		return tutorialDescription;
	}
	public void setTutorialDescription(String tutorialDescription) {
		this.tutorialDescription = tutorialDescription;
	}

}

3. The Views

selectOneListBox.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
	<h:form>
	<h1><h:outputText value="JavaBeat JSF 2.2 Examples" /></h1>
	<h2><h:outputText value="SelectOneListBox Example" /></h2>
	<h:selectOneListbox value="#{selectOneListBoxBean.selectedTuorial}" size="4">
		<f:selectItems value="#{selectOneListBoxBean.tutorials}" var="tutorial"
				itemValue="#{tutorial.tutorialId}" itemLabel="#{tutorial.tutorialDescription}">
		</f:selectItems>
	</h:selectOneListbox>
	<h:commandButton value="Submit Training" action="#{selectOneListBoxBean.submitTraining}"></h:commandButton>
	</h:form>
</html>

selectedTraining.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core">
	<f:view>
		<h1><h:outputText value="JavaBeat JSF 2.2 Examples" /></h1>
		<h2><h:outputText value="SelectOneListBox Example" /></h2>
		<h:form>
			<h:outputText value="Selected Tutorials: "/>
			<h:outputText value="#{selectOneListBoxBean.selectedTuorial}"/>
		</h:form>
	</f:view>
</html>

4. The Deployment Descriptor (web.xml)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5" metadata-complete="true">
	<context-param>
		<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
		<param-value>client</param-value>
	</context-param>
	<context-param>
		<param-name>javax.faces.application.CONFIG_FILES</param-name>
		<param-value>/WEB-INF/faces-config.xml</param-value>
	</context-param>
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>/faces/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
	<listener>
		<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
	</listener>
</web-app>

5. JSF 2 SelectOneListBox Demo

The below snapshots that you would be seeing provide you a running sample of using SelectOneListBox.

JSF 2 SelectOneListBox Example 1

JSF 2 SelectOneListBox Example 2

[wpdm_file id=6]

Category: JSFTag: JSF 2

About Amr Mohammed

Previous Post: « JSF 2 SelectManyMenu Example
Next Post: JSF 2 SelectManyListBox Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact