Jsf datatable update row example

 

Example:

test.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>JSF edit data to data table example.</title>
</h:head>
<h:body>
	<h2>JSF edit data to data table example.</h2>
	<h:form>
		<h:dataTable 
		    value="#{test.students}" var="student">
			<h:column>
				<f:facet name="header">Name</f:facet>
				<h:inputText value="#{student.name}"
					rendered="#{student.editable}" />
				<h:outputText value="#{student.name}"
					rendered="#{!student.editable}" />
			</h:column>
			<h:column>
				<f:facet name="header">Class</f:facet>
				<h:inputText value="#{student.stuClass}"
					rendered="#{student.editable}" />
				<h:outputText value="#{student.stuClass}"
					rendered="#{!student.editable}" />
         </h:column>
			<h:column>
				<f:facet name="header">RollNo</f:facet>
				<h:inputText value="#{student.rollNo}"
					rendered="#{student.editable}" />
				<h:outputText value="#{student.rollNo}"
					rendered="#{!student.editable}" />
         </h:column>
			<h:column>
				<f:facet name="header">Age</f:facet>
				<h:inputText value="#{student.age}"
					rendered="#{student.editable}" />
				<h:outputText value="#{student.age}"
					rendered="#{!student.editable}" />
         </h:column>
         <h:column>
            <f:facet name="header">Edit</f:facet>
            <h:commandButton value="Edit" 
               action="#{test.editStudent}" 
               rendered="#{!student.editable}">        
               <f:setPropertyActionListener 
                  target="#{test.student}" 
                  value="#{student}" />
            </h:commandButton>
         </h:column>
		</h:dataTable>
		<h:commandButton value="Save"
          action="#{test.saveStudents}" />
	</h:form>
</h:body>
 
</html>

Test.java

/**
 * Managed Bean.
 * @author w3schools
 */
public class Student {
	private String name;
	private String stuClass;
	private String rollNo;
	private int age;
	private boolean editable;
 
	public Student(String name, 
			String stuClass, String rollNo, int age) {
		this.age = age;
		this.name = name;
		this.rollNo = rollNo;
		this.stuClass = stuClass;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getStuClass() {
		return stuClass;
	}
	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isEditable() {
		return editable;
	}
	public void setEditable(boolean editable) {
		this.editable = editable;
	}
}

Student.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
/**
 * Backing bean.
 * @author w3schools
 */
@ManagedBean(name = "test")
@SessionScoped
public class Test {	
	private Student student;
 
	private static final List<Student> studentList = 
		new ArrayList<Student>(Arrays.asList(
			new Student("Vivek","MCA 3rd","MCA/07/40",29),
			new Student("Sunil","MCA 3rd","MCA/07/41",33),
			new Student("Bharat","MCA 3rd","MCA/07/42",27),
			new Student("Richi","MCA 3rd","MCA/07/43",28),
			new Student("Sahdev","MCA 3rd","MCA/07/44",28)));
 
	public List<Student> getStudents(){
		return studentList;
	}
 
	public String editStudent(){
	    student.setEditable(true);
	    return null;
   }
 
	public String saveStudents(){
		for (Student student : studentList){
			student.setEditable(false);
        }		
        return null;
	}
 
	public Student getStudent() {
		return student;
	}
 
	public void setStudent(Student student) {
		this.student = student;
	}
}

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>
         javax.faces.webapp.FacesServlet
	<servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

URL:

http://localhost:7001/JSFExample43/faces/test.xhtml

Output:

JSF example43-1
 
To edit a row, Click on “Edit” button.
JSF example43-2
 
Update row, Click on “Save” button.
JSF example43-3
 
Download this example.