JSF dataTable Example

Filed Under: JSF

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)
@SessionScoped
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>(
			Arrays.asList(
					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" "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:c="http://java.sun.com/jsf/core">
<h:head>
	<title>DataTable tag Example</title>
</h:head>
<h:body>
	<h3>Mobile Details</h3>
	<h:form>
		<h:dataTable value="#{mobilerecords.mobiles}" var="mobile" border="2"
			cellspacing="1" cellpadding="1">
			<h:column>
				<c:facet name="header">Name</c:facet>
                    #{mobile.companyname}
                </h:column>
			<h:column>
				<c:facet name="header">Model Number</c:facet>
                    #{mobile.modelnumber}
                </h:column>
			<h:column>
				<c:facet name="header">Color</c:facet>
                    #{mobile.color}
                </h:column>
			<h:column>
				<c:facet name="header">Quantity</c:facet>
                    #{mobile.quantity}
                </h:column>
			<h:column>
				<c:facet name="header">Price</c:facet>
                    #{mobile.price}
                </h:column>

		</h:dataTable>
	</h:form>

</h:body>
</html>

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.

JSF dataTable

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

JSF dataTable example project

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

Comments

  1. Walter L. says:

    What’s var=”mobile”? Does it assign the table to the variable “mobile”?I do not see it mentioned on this page, and there’s no resulting page source to guess what it does. Could someone clarify? Thanks.

    1. Praveen says:

      This is like forEach() in Java. It gets assigned for all entries of the list provided in the value attribute

  2. ChethanKumar says:

    Mobile Details
    Name #{mobile.companyname} Model Number #{mobile.modelnumber} Color #{mobile.color} Quantity #{mobile.quantity} Price #{mobile.price}

    this is the outputiam getting please help me here dont know the reason. no error or exception

  3. Abdul Javeeth F says:

    Thank you for the above clarification…

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages