JSF Dropdown List Example





Hey guys in this post, we will discuss dropdown list in JavaServer Faces (JSF) with Example

Overview


  • To create dropdown list we use <h:selectOneMenu> tag
  • <h:selectOneMenu> takes value property which will hold the value of the selected item
  • We will use  <c:selectItem> tag to add the dropdown items
  • <c:selectItem> takes the property itemValue, which will hold the item value and takes another property itemLabel, which displays the item name

Complete example


We will create this example step by step, follow this tutorial till the end

Read More:

Create Dynamic Web Project


To create a dynamic web project, you can read the following tutorial

Create a bean


Create Register.java inside the in.bushansirgur.jsf.beans package and add the following content

package in.bushansirgur.jsf.beans;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class Register {
	
	private String email;
	
	private String password;
	
	private String role;
	
	public Register() {
	
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	
	
}

Create Input form


Create register-form.xhtml file inside the WebContent and add the following content

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
		xmlns:h="http://xmlns.jcp.org/jsf/html"
		xmlns:c="http://xmlns.jcp.org/jsf/core">

	<h:head>
		<title>Input Form</title>
	</h:head>
	
	<h:body>
		
		<h:form>
			
			Email: <h:inputText id="email" value="#{register.email}" />
			<br/><br/>
			Password: <h:inputSecret id="password" value="#{register.password}" />
			<br/><br/>
			Role: 
			<h:selectOneMenu value="#{register.role}">
				<c:selectItem itemValue="User" itemLabel="User" />
				<c:selectItem itemValue="Admin" itemLabel="Admin" />
				<c:selectItem itemValue="Super Admin" itemLabel="Super Admin" />
			</h:selectOneMenu>
			<br/><br/>
			<h:commandButton value="Login" action="dashboard" />
			
		</h:form>
		
	</h:body>

</html>

Create a response page


Create dashboard.xhtml page inside the WebContent folder and add the following content

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
		xmlns:h="http://xmlns.jcp.org/jsf/html">

	<h:head>
		<title>Dashboard</title>
	</h:head>
	
	<h:body>
		
		<div>
			<span>The Logged in user is: #{register.email}</span>
			<span style="float: right;">Role: #{register.role}</span>
		</div>
	</h:body>

</html>

Run the app


To run the application, right click on the project, select Run As -> choose Run on Server

Open the browser, navigate to the URL http://localhost:8080/JSF-hello-world/faces/register-form.xhtml
18

17
That’s it for this post. If you like this post, then please share this with your friends and collogues. Also share this post on your social media profiles as well.



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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