JSF dataTable tag is used to display data on JSF view pages. The data bound table components are responsible for displaying the relational data in a tabular format. The <h:dataTable> tag is used for displaying the data components. The <h:column> tag iterates over each record in the data source displayed in rows.

JSF dataTable

Some of the attributes of JSF dataTable tag are;

  1. id: unique identifier used to identify a component.
  2. value: The current value of the component.
  3. bgcolor: background color for the table that is displayed.
  4. border: width in pixel to be drawn around the table.
  5. cellpadding: Space between border of each cell and its contents.
  6. cellspacing: Space between left side of the table and leftmost column and also amount of space between the cells.
  7. columnClasses: List of css styles separated by comma to be applied to the columns of this table.
  8. bodyrows: List of row indices separated by comma to be applied for the “tbody” element should be started.
  9. first: zero or relative row number of the first row to be displayed.
  10. frame: code that specifies the frame to be visible around the table
  11. rows: Number of rows to be displayed identified by first property.
  12. headerClass: CSS class for the table header

JSF dataTable Example

Now let’s look at the JSF dataTable example.

The example is to display the mobile company name, price and quantity needed by the user.

Create a managed bean named Mobile.java to contain a list of mobiles along with their details.

package com.journaldev.jsf.beans;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "mobilerecords", eager = true)
public class Mobile {
	private String companyname;
	private String modelnumber;
	private String color;
	private int quantity;
	private double price;
	private static final ArrayList<Mobile> mobiles =
			new ArrayList<Mobile>(
					new Mobile("Nokia", "N213", "Black", 10, 2500),
					new Mobile("Micromax", "A114", "White", 25, 9500),
					new Mobile("MotoG", "M345", "Grey", 40, 15300),
					new Mobile("Samsung", "S-512", "Blue", 15, 11000)
	public ArrayList<Mobile> getMobiles() {
		return mobiles;
	public Mobile() {
	public Mobile(String companyname, String modelnumber, String color,
			int quantity, double price) {
		this.companyname = companyname;
		this.modelnumber = modelnumber;
		this.color = color;
		this.quantity = quantity;
		this.price = price;
	public String getCompanyname() {
		return companyname;
	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	public String getModelnumber() {
		return modelnumber;
	public void setModelnumber(String modelnumber) {
		this.modelnumber = modelnumber;
	public String getColor() {
		return color;
	public void setColor(String color) {
		this.color = color;
	public int getQuantity() {
		return quantity;
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	public double getPrice() {
		return price;
	public void setPrice(double price) {
		this.price = price;

Here we have declared a list called mobiles which contains mobile details like color, company name, price etc. and the getter setter methods for various fields used. Also, the constructors are written to set values for the fields and insert them into the list called “mobiles”.

Now create the JSF page mobiles.xhtml as;

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"
	<title>DataTable tag Example</title>
	<h3>Mobile Details</h3>
		<h:dataTable value="#{mobilerecords.mobiles}" var="mobile" border="2"
			cellspacing="1" cellpadding="1">
				<c:facet name="header">Name</c:facet>
				<c:facet name="header">Model Number</c:facet>
				<c:facet name="header">Color</c:facet>
				<c:facet name="header">Quantity</c:facet>
				<c:facet name="header">Price</c:facet>

Here we use the jsf dataTable tag to fetch the list from the Mobile bean and display each column using <h:column> tag.

We use css properties like border, cellspacing, cellpadding etc. to display the table as we want. The <c:facet> tag is used to display the column headers.

The <h:column> tag represents the data for each column and iterates until it finds the <h:column> tag for a particular row.

Once done with the above said bean class and JSF page, run the application and it should display the following output.


Below image shows the final JSF dataTable example project structure in Eclipse.


You can download the final JSF dataTable project from below link to learn more.

By admin

Leave a Reply

%d bloggers like this: