Pre-populating the form values in JSF with Example





Hey guys in this, we will discuss populating the form values in JSF with Example.

Overview


  • To create textbox we use <h:inputText> tag
  • To create dropdown menu we use <h:selectOneMenu> tag, we will use <c:selectItem> tag to add the dropdown options
  • To create a checkbox we use <h:selectManyCheckbox> tag. We will use  <c:selectItem> tag to add the checkbox options
  • To create radio button we use <h:selectOneRadio> tag, we will use <c:selectItem> tag to add radio button options.

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 java.util.Arrays;
import java.util.List;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class Register {
	
	private String email;
	
	private String role;
	
	private String active;
	
	private String[] permissions; 
		
	public Register() {
		this.email = "[email protected]";
		this.role = "Power Admin";
		this.active = "Inactive";
		this.permissions = new String[]{"Create", "Read"}; 
	}

	public String getEmail() {
		return email;
	}

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

	public String getRole() {
		return role;
	}

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

	public String getActive() {
		return active;
	}

	public void setActive(String active) {
		this.active = active;
	}
	
	public String[] getPermissions() {
		return permissions;
	}

	public void setPermissions(String[] permissions) {
		this.permissions = permissions;
	}	
}

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/>
			Role: 
			<h:selectOneMenu value="#{register.role}">
				<c:selectItem itemValue="Admin" itemLabel="Admin" />
				<c:selectItem itemValue="Super Admin" itemLabel="Super Admin" />
				<c:selectItem itemValue="Power Admin" itemLabel="Power Admin" />
			</h:selectOneMenu>
			<br/><br/>
			Status:
			<h:selectOneRadio value="#{register.active}">
				<c:selectItem itemValue="Active" itemLabel="Active" />
				<c:selectItem itemValue="Inactive" itemLabel="Inactive" />
			</h:selectOneRadio>
			<br/><br/>
			Permissions:
			<h:selectManyCheckbox value="#{register.permissions}">
				<c:selectItem itemValue="Create" itemLabel="Create" />
				<c:selectItem itemValue="Read" itemLabel="Read" />
				<c:selectItem itemValue="Update" itemLabel="Update" />
				<c:selectItem itemValue="Delete" itemLabel="Delete" />
				<c:selectItem itemValue="All" itemLabel="All" />
			</h:selectManyCheckbox>
			<br/><br/>
			<h:commandButton value="Register" action="dashboard" />
		</h:form>
		
	</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
07

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 *