Table of contents
1.
Introduction 
2.
Implementation
2.1.
Folder Structure
2.2.
Code
2.3.
Output 1 (Before Clicking the Delete Button)
2.4.
Output 2 (After Clicking the Delete Button)
3.
Frequently Asked Questions
3.1.
What is the <h:dataTable> tag in JavaServer Faces?
3.2.
What are the available validation tags in JavaServer Faces?
3.3.
What is the f:validateRequired tag in JavaServer Faces?
3.4.
What is the f:converter tag in JSF (JavaServer Faces)?
4.
Conclusion
Last Updated: Mar 27, 2024
Hard

Delete Data of a DataTable in JSF

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

JSF provides a rich control named DataTable to render and format HTML tables.

  1. To display data, a data table can loop through a set of values or an array of values.
  2. DataTable offers attributes to modify its data easily.

The <h:dataTable> tag is used to create a data table in JSF. 

Implementation

Follow these steps to put the example application into practice.

  1. Create a demo project in any IDE that supports Java Web applications, such as NetBeans. We will use the Apache Netbeans IDE for the following implementation program.
  2. Choose the project type as a web application under the Java with Maven category.
  3. Pick any practical name for your project and packages.
  4. Select Java EE 7 web along with the GlassFish Server.
  5. Right-click the project name in the project window's folder section, then select Properties.
  6. Add the Java Server Faces framework under the frameworks tab.
  7. Make a Java-managed bean class file called "StudentBean.java" in the "source packages->com.mycompany.myproject" folder.
  8. Modify the “index.xhtml,” "StudentBean.java", and other files as given below.
  9. To check if the application is being compiled in the previously configured environment, compile and run it.
  10. Create the web application and deploy it using the GlassFish server as a war file. The default browser should display the output after program execution.

Folder Structure

As was mentioned above, the folder structure should resemble the image below.

(Folder structure of JSF web application)

Code

StudentBean.java:

package com.mycompany.demoproject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="student")
@SessionScoped
public class StudentBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private static final ArrayList<Student> studentList =
        new ArrayList<>(Arrays.asList(
       
        new Student("200465", "Arpita Priyadarsini",
                new BigDecimal("156.60"), 1),
        new Student("200466", "Bijay Kumar",
                new BigDecimal("175.00"), 2),
        new Student("200467", "Fiza Afreen",
                new BigDecimal("168.00"), 8),
        new Student("200468", "Anoushka Karkoon",
                new BigDecimal("163.70"), 3),
        new Student("200469", "Arpita Kumari",
                new BigDecimal("170.00"), 10)
    ));
     
    public ArrayList<Student> getStudentList() {
 
        return studentList;
 
    }
   
    public String deleteAction(Student student) {
       
        studentList.remove(student);
        return null;
    }
 
    public static class Student{
       
        String studentId;
        String studentName;
        BigDecimal height;
        int rank;

        public Student(String studentId, String studentName,
                BigDecimal height, int rank) {
            this.studentId = studentId;
            this.studentName = studentName;
            this.height = height;
            this.rank = rank;
        }
       
        public String getStudentId() {
            return studentId;
        }
        public void setStudentId(String studentId) {
            this.studentId = studentId;
        }
        public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        public BigDecimal getHeight() {
            return height;
        }
        public void setHeight(BigDecimal height) {
            this.height = height;
        }
        public int getRank() {
            return rank;
        }
        public void setRank(int rank) {
            this.rank = rank;
        }
    }
}

index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Delete Data from Data Table</title>
    </h:head>
    <h:body>
       
        <h1>Demonstration of Data from Data Table in JSF</h1>
        <h:form>
            <h:dataTable value="#{student.studentList}" var="s">
                <h:column>
               
                    <f:facet name="header">Student ID</f:facet>
                    #{s.studentId}
                   
                </h:column>
               
                <h:column>
               
                    <f:facet name="header">Student Name</f:facet>
                    #{s.studentName}
                   
                </h:column>
               
                <h:column>
                   
                    <f:facet name="header">Height</f:facet>
                    #{s.height}
                   
                </h:column>
               
                <h:column>
                   
                    <f:facet name="header">Student Rank</f:facet>
                    #{s.rank}
                   
                </h:column>
           
                <h:column>
                   
                    <f:facet name="header">Action</f:facet>
                   
                    <h:commandLink value="Delete" action="#{student.deleteAction(s)}" />
                   
                </h:column>

            </h:dataTable>
           
        </h:form>
    </h:body>
       
</html>

Output 1 (Before Clicking the Delete Button)

Output 2 (After Clicking the Delete Button)

Frequently Asked Questions

What is the <h:dataTable> tag in JavaServer Faces?

It is used to create a data table that can be updated dynamically.

What are the available validation tags in JavaServer Faces?

The JavaServer Faces technology provides common classes and associated tags to validate element data. For example, validateBean, validateDoubleRange, validateLength, validateLongRange, validateRegex, and validateRequired.

What is the f:validateRequired tag in JavaServer Faces?

It ensures that the local value is not empty on an EditableValueHolder component.

What is the f:converter tag in JSF (JavaServer Faces)?

It is a tag for a core converter. It is used to extend the parent component with any converter.

Conclusion

In this article, we have extensively discussed how to delete data from a data table in JSF. This blog covered JSF, the data table, and its implementation. We hope this blog has helped you enhance your knowledge of how to delete data from a data table in JSF. You can also check out our articles on javaBasics Of Javajava frameworksTop 10 web development frameworksJava knowledge for your first coding jobMicroservices In Java, and Introduction to Spring Boot.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more!

Happy Reading!

Live masterclass