In JSF, “h:dataTable” tag is used to display data in a HTML table format. The following JSF 2.0 example show you how to use “h:dataTable” tag to loop over an array of “order” object, and display it in a HTML table format.

1. Project Folder

Project folder structure of this example.


2. Managed bean

A managed bean named “order”, initialized the array object for later use.

package com.mkyong;
import java.math.BigDecimal;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
public class OrderBean implements Serializable{
	private static final long serialVersionUID = 1L;
	private static final Order[] orderList = new Order[] {
		new Order("A0001", "Intel CPU", 
				new BigDecimal("700.00"), 1),
		new Order("A0002", "Harddisk 10TB", 
				new BigDecimal("500.00"), 2),
		new Order("A0003", "Dell Laptop", 
				new BigDecimal("11600.00"), 8),
		new Order("A0004", "Samsung LCD", 
				new BigDecimal("5200.00"), 3),
		new Order("A0005", "A4Tech Mouse", 
				new BigDecimal("100.00"), 10)
	public Order[] getOrderList() {
		return orderList;
	public static class Order{
		String orderNo;
		String productName;
		BigDecimal price;
		int qty;
		public Order(String orderNo, String productName, 
                                BigDecimal price, int qty) {
			this.orderNo = orderNo;
			this.productName = productName;
			this.price = price;
			this.qty = qty;
		//getter and setter methods

3. CSS

Create a CSS file to style the table layout.


	background:none repeat scroll 0 0 #E5E5E5;
	border-bottom:1px solid #BBBBBB;
	background:none repeat scroll 0 0 #FFFFFFF;
	border-top:1px solid #BBBBBB;
	background:none repeat scroll 0 0 #F9F9F9;
	border-top:1px solid #BBBBBB;

4. h:dataTable

A JSF 2.0 xhtml page to show the use of “h:dataTable” tag to loop over the array of “order” object. This example should be self-explanatory.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""   
    	<h:outputStylesheet library="css" name="table-style.css"  />
    	<h1>JSF 2 dataTable example</h1>
    		<h:dataTable value="#{order.orderList}" var="o"
    				<!-- column header -->
    				<f:facet name="header">Order No</f:facet>
    				<!-- row record -->
    				<f:facet name="header">Product Name</f:facet>
    				<f:facet name="header">Price</f:facet>
    				<f:facet name="header">Quantity</f:facet>

Generate this HTML output

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
	   <link type="text/css" rel="stylesheet" 
	      href="/JavaServerFaces/faces/javax.faces.resource/table-style.css?ln=css" />
	<h1>JSF 2 dataTable example</h1>
	<table class="order-table"> 
		<th class="order-table-header" scope="col">Order No</th> 
		<th class="order-table-header" scope="col">Product Name</th> 
		<th class="order-table-header" scope="col">Price</th> 
		<th class="order-table-header" scope="col">Quantity</th> 
		<tr class="order-table-odd-row"> 
			<td>Intel CPU</td> 
		<tr class="order-table-even-row"> 
			<td>Harddisk 10TB</td> 
		<tr class="order-table-odd-row"> 
			<td>Dell Laptop</td> 
		<tr class="order-table-even-row"> 
			<td>Samsung LCD</td> 
		<tr class="order-table-odd-row"> 
			<td>A4Tech Mouse</td> 

6. Demo

URL : http://localhost:8080/JavaServerFaces/default.xhtml

Download It – (11KB)


  1. JSF <h:dataTable /> JavaDoc
  2. JSF <h:column /> JavaDoc
  3. JSF <f:facet /> JavaDoc
  4. JSF 2 resource bundles example
Tags :
Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Related Posts

Popular Posts