Table of contents
1.
Introduction
2.
Implementation
2.1.
Folder Structure
2.2.
Code
2.3.
Output 1(Before Clicking the Edit Button)
2.4.
Output 2(After Clicking the Edit Button)
3.
Frequently Asked Questions
3.1.
How to update a row in a JSF data table?
3.2.
How to display a data table in JSF?
3.3.
Which tag is used to make a data table in JSF?
3.4.
What is a facet tag in JSF?
4.
Conclusion
Last Updated: Mar 27, 2024
Hard

Edit data of data table in JSF

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

Introduction

Hello and welcome, readers! We hope you are doing well.  

Today, we will discuss how to edit a data table in JSF. After completing this article, you will clearly understand the JSF Data Table. So follow the article till the end. So, without further ado, let’s jump into the implementation and see how to edit a data table.

Implementation

To put the example application into action, follow these steps.

  1. Create a demo project in any IDE that supports Java Web applications, such as NetBeans. For the subsequent implementation program, we will employ the Apache Netbeans IDE.
  2. Select the web application project type from the Java with Maven category.
  3. Pick any practical name for your project and packages.
  4. Choose Java EE 7 for the web and the GlassFish Server.
  5. Select Properties after right-clicking the project name in the Project window's folder section.
  6. Under the Frameworks tab, add the Java Server Faces framework.
  7. Modify the "index.xhtml," "StudentBean.java", and other files per the code below.
  8. Compile and run the application to see if it is being compiled in the previously configured environment.
  9. Create the web application and deploy it as a war file via the GlassFish server. After execution, the program's output should be displayed in the default browser window.

Folder Structure

As previously stated, 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;
        String studentId;
        String studentName;
        BigDecimal height;
        int 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;
        }
    //declaring data items in data table    
    
    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 saveAction() {  
        for (Student student : studentList){
            student.setEditable(false);
        }
        return null;
    }
    public String editAction(Student student) {
       
        student.setEditable(true);
        return null;
    }
 
    public static class Student{
       
        String studentId;
        String studentName;
        BigDecimal height;
        int rank;
                boolean editable;

        public Student(String studentId, String studentName,
                BigDecimal height, int rank) {
            this.studentId = studentId;
            this.studentName = studentName;
            this.height = height;
            this.rank = rank;
        }
               
                public boolean isEditable() {
                  return editable;
                }
                public void setEditable(boolean editable) {
                  this.editable = editable;
                }      
        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>Add Data to Data Table</title>
    </h:head>
    <h:body>
       
        <h1>Demonstration of Add Data to Data Table in JSF</h1>
        <h:form>
            <h:dataTable value="#{student.studentList}" var="s">
                <h:column>
               
                    <f:facet name="header">Student ID</f:facet>
                                <h:inputText value="#{s.studentId}" size="10" rendered="#{s.editable}" />
                   
                                <h:outputText value="#{s.studentId}" rendered="#{not s.editable}" />
                   
                   
                </h:column>
               
                <h:column>
               
                    <f:facet name="header">Student Name</f:facet>
                    <h:inputText value="#{s.studentName}" size="20" rendered="#{s.editable}" />
                   
                                <h:outputText value="#{s.studentName}" rendered="#{not s.editable}" />
                               
                   
                </h:column>
               
                <h:column>
                   
                    <f:facet name="header">Height</f:facet>
                                <h:inputText value="#{s.height}" size="10" rendered="#{s.editable}" />
                   
                                <h:outputText value="#{s.height}" rendered="#{not s.editable}" />
                   
                   
                </h:column>
               
                <h:column>
                   
                    <f:facet name="header">Student Rank</f:facet>
                                <h:inputText value="#{s.rank}" size="5" rendered="#{s.editable}" />
                   
                                <h:outputText value="#{s.rank}" rendered="#{not s.editable}" />
                   
                   
                </h:column>
           
                        <h:column>

                            <f:facet name="header">Action</f:facet>

                            <h:commandLink value="Edit" action="#{student.editAction(s)}"
                                                   rendered="#{not s.editable}" />

                        </h:column>

            </h:dataTable>
                  <h:commandButton value="Save" action="#{student.saveAction}" />
      </h:form>
    </h:body>
</html>

Output 1(Before Clicking the Edit Button)

Output 2(After Clicking the Edit Button)

Frequently Asked Questions

How to update a row in a JSF data table?

Adding an "editable" property will allow you to track the status of each row's edits. This allows us to update a row in the JSF data table.

How to display a data table in JSF?

We use the <h:column> tag and the JSF dataTable tag to retrieve the list from the Mobile bean to display each column. For the table to appear as we desire, we use CSS properties like border, cell spacing, and padding. To display the column headers, use the <c:facet> tag.

Which tag is used to make a data table in JSF?

The h:dataTable tag is used to make a data table in JSF.

What is a facet tag in JSF?

A facet represents a named section within a container component. For a data table component, you could add a header and a footer facet.

Conclusion

In this article, we have extensively discussed how to edit data of a data table in JSF and their implementation. This blog includes code on editing data to give you a more detailed insight into its practical implementation. We hope this blog has helped you enhance your knowledge of JSF and data tables. You can also check out our articles on javaBasics Of Javajava frameworksTop 10 web development frameworksIntroduction to Spring BootJava knowledge for your first coding job, and Microservices In Java.

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