JSF 2 DataTable Delete Row Example

In JSF 2.2 the manipulation of Data Table has been never easier and specifically when it comes to finding out which row the user selected for removal. In JSF 2.2 the removing happened without using of passed parameters or binding the DataTable component into its corresponding Java Object – Like HTMLDataTable – for being manipulated from there. Simply what you have to do is to identify your Plain Old Java Object (POJO) by overriding the equals and pass the object being removed into the remove method. This tutorial highlights the simple example for how to delete a row in datatable.

1. Student POJO


package net.javabeat.jsf.data;

public class Student {
	private String name;
	private String major;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;

	public String getMajor() {
		return major;
	public void setMajor(String major) {
		this.major = major;

	public boolean equals(Object obj){
		// The identification here is through of using the name of the student
		if(obj instanceof Student){
				return true;
		return false;

2. Managed Bean


package net.javabeat.jsf;

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

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

import net.javabeat.jsf.data.Student;

public class RegisterBean {
	private Student student = new Student();
	private List<Student> students = new ArrayList<Student>();
	public Student getStudent() {
		return student;
	public void setStudent(Student student) {
		this.student = student;
	public List<Student> getStudents() {
		return students;
	public void setStudents(List<Student> students) {
		this.students = students;

	public String addStudent(){
		this.student = new Student();
		return "";

	public String delete(Student std){
		return "";

3. The View


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml"
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 DataTable - Deleting Rows Example</h2>
			<h:panelGrid columns="2" border="1">
				<h:outputText value="Enter Student Name:"/>
				<h:inputText value="#{registerBean.student.name}"/>
				<h:outputText value="Enter Student Major:"/>
				<h:inputText value="#{registerBean.student.major}"/>
			<h:commandButton value="Add Student" action="#{registerBean.addStudent}"/>
			<h:dataTable value="#{registerBean.students}" var="student" border="1">
					<f:facet name="header">
						<h:outputText value="Student Name"/>
					<h:outputText value="#{student.name}"/>
					<f:facet name="header">
						<h:outputText value="Student Major"/>
					<h:outputText value="#{student.major}"/>
					<f:facet name="header">
						<h:outputText value="Delete Student"/>
					<h:commandLink value="Delete" action="#{registerBean.delete(student)}"/>

3. Faces Configuration File


<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"

4. The Deployment Descriptor


<?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"
	id="WebApp_ID" version="2.5" metadata-complete="true">
		<description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>

5. JSF DataTable Delete Row Demo

The below snapshot, shows you the using of the newly feature added for removing the rows from a data table using the passing pojo java object directly into the method that responsible of removal operation.

JSF 2 Deleting Rows Example 2

  • The admins can add whatever students they want.

JSF 2 Deleting Rows Example 2

  • The admins can remove whatever students they want.
  • The deletion process has assumed that the uniqueness is covered through the name of the student. However, you have ability to consider whatever kind of identification. But consider the needs to override the equals method in the Student.

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!