PrimeFaces ships with a DataTable component that displays data in tabular format.

The following example shows how to implement a PrimeFaces DataTable in combination with Spring Data JPA, Spring Boot, and Maven.

General Project Setup

Tools used:

  • Spring Data JPA 1.11
  • Spring Boot 1.5
  • PrimeFaces 6.1
  • JoinFaces 2.4
  • Maven 3.5

The example is based on a previous Hello World Primefaces Tutorial.

Instead of hard coding the data that will be displayed in the DataTable, we will fetch it from a H2 database. By adding the h2 Maven dependency to the POM file, Spring Boot will auto-configure an embedded H2 database that we can use.

In addition the Spring Data JPA project is used in order to reduce the amount of boilerplate code required to implement the data access layer for the persistence store (in this case H2). The spring-boot-starter-data-jpa includes spring-data-jpa and other needed dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


  <description>JSF - PrimeFaces DataTable Example</description>

    <relativePath /> <!-- lookup parent from repository -->


    <!-- spring-boot -->
    <!-- h2 -->
    <!-- joinfaces -->

      <!-- spring-boot-maven-plugin -->

Prepare the Database

Similar to the PrimeFaces ShowCase example we will be displaying different cars in the DataTable. The below class represents a car with a basic structure.

We will use the Java Persistence API to define how the Car class maps to a relational database table.

The class is annotated with the @Entity annotation, which marks the class as an entity class. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table.

So for the Car class, a table named CAR will be created with the id, brand, year and color fields as columns.

The id filed is annotated with @Id to mark it as a primary key. The @GeneratedValue annotation specifies that the primary key will be automatically generated.

package com.codenotfound.primefaces.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name = "Car")
public class Car {

  private Long id;

  private String brand;
  private int year;
  private String color;

  public Car() {}

  public Car(Long id, String brand, int year, String color) { = id;
    this.brand = brand;
    this.year = year;
    this.color = color;

  public Long getId() {
    return id;

  public void setId(Long id) { = id;

  public String getBrand() {
    return brand;

  public void setBrand(String brand) {
    this.brand = brand;

  public int getYear() {
    return year;

  public void setYear(int year) {
    this.year = year;

  public String getColor() {
    return color;

  public void setColor(String color) {
    this.color = color;

Next we create a Spring Data Repository which will auto-generate the implementation for our Car domain object. To do so extend the JpaRepository and pass the domain class to manage in addition to the id type of the domain class as type arguments.

We annotated the below interface with @Repository which is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. It is also a specialization of @Component annotation which means that Spring will automatically create a Bean for this class in case a component scan is performed.

As our SpringPrimeFacesApplication is annotated with @SpringBootApplication an implicit component scan is performed at startup.

package com.codenotfound.primefaces.repository;

import org.springframework.stereotype.Repository;

import com.codenotfound.primefaces.model.Car;

public interface CarRepository extends JpaRepository<Car, Long> {

In addition to setting up the H2 database, Spring Boot will also initialize it with data if the needed script is found on the classpath.

Simply add an data.sql file under src/main/resources/META-INF/resources that contains the data to be inserted.

INSERT INTO car (brand, year, color) VALUES
  ('Audi', 1992, 'Red'),
  ('Fiat', 2001, 'Red'),
  ('Mercedes', 1991, 'Brown'),
  ('Fiat', 1962, 'Black'),
  ('Renault', 1997, 'Brown'),
  ('Renault', 1967, 'Maroon'),
  ('Renault', 1986, 'Yellow'),
  ('BMW', 1970, 'Maroon'),
  ('Fiat', 1990, 'Silver'),
  ('Renault', 1972, 'Black');

Create the PrimeFaces DataTable Component

We create a cars.xhtml page under src/main/resources/META-INF/resources. It contains a <dataTable> element on which we specify the list of objects that need to be displayed using the value attribute. In this example we use the cars field on the carsView Managed Bean that we will create further below.

The var attribute specifies the name of the variable created by the data table that represents the current item in the value. We use this name in order to specify what field from the object needs to be displayed in each column.

For example in the first column we specify #{} so that the id of the current Car object is shown.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""

  <title>PrimeFaces DataTable Example</title>


  <p:dataTable var="car" value="#{}">
    <p:column headerText="Id">
      <h:outputText value="#{}" />

    <p:column headerText="Year">
      <h:outputText value="#{car.year}" />

    <p:column headerText="Brand">
      <h:outputText value="#{car.brand}" />

    <p:column headerText="Color">
      <h:outputText value="#{car.color}" />


The only thing left to do is to create a Bean that can be used by the above JSF page in order to access to the JpaRepository so that data can be fetched from the database.

We create a CarsView class in which we auto-wire the CarRepository. The cars field is then initialized using the findAll() method on the repository which retrieves all available cars.

Note that we use @Named instead of @ManagedBean. The reason for this is that it is preferred to choose one framework to manage our beans.

package com.codenotfound.primefaces.view;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;

import com.codenotfound.primefaces.model.Car;
import com.codenotfound.primefaces.repository.CarRepository;

public class CarsView implements Serializable {

  private static final long serialVersionUID = 1L;

  private CarRepository carRepository;

  private List<Car> cars;

  public void init() {
    cars = carRepository.findAll();

  public List<Car> getCars() {
    return cars;

Testing the PrimeFaces DataTable Example

Let’s test our PrimeFaces DataTable example by running following Maven command:

mvn spring-boot:run

Once Spring Boot has started, open a web browser and enter the following URL: http://localhost:9090/codenotfound/cars.xhtml.

A list of different cars should be rendered as shown below.

jsf primefaces datatable example

github mark If you would like to run the above code sample you can get the full source code here.

Displaying the content of a database table using the PrimeFaces DataTable component can be quickly achieved using Spring Data an Spring Boot.

Hope you enjoyed this post. Drop a line in case you have some questions or would like to see another example.

Leave a Comment