Over a million developers have joined DZone.

PagingAndSortingRepository: How to Use It With Thymeleaf

DZone's Guide to

PagingAndSortingRepository: How to Use It With Thymeleaf

In today's tutorial, we will demonstrate how web developers can display a list of a data (in our case, client lists) in Thymeleaf with pagination.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

For this tutorial, I will demonstrate how to display a list of a business's clients in Thymeleaf with pagination.

View and Download the code from GitHub.

1 - Project Structure

We have a normal Maven project structure.

2 - Project Dependencies

Besides the normal Spring dependencies, we add Thymeleaf and hsqldb because we are using an embedded database.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <description>Michael C  Good - PagingAndSortingRepository</description>

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






3 - Models

We define the following fields for a client:

  • A unique identifier.
  • Name of the client.
  • An address of the client.
  • The amount owed on the current invoice.

The getters and setters are quickly generated in Spring Tool Suite. The @Entity annotation is needed for registering this model to @SpringBootApplication.


package com.michaelcgood.model;

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

public class ClientModel {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    public Long getId() {
        return id;
    public void setId(Long id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getAddress() {
        return address;
    public void setAddress(String address) {
        this.address = address;
    public Integer getCurrentInvoice() {
        return currentInvoice;
    public void setCurrentInvoice(Integer currentInvoice) {
        this.currentInvoice = currentInvoice;
    private String name;
    private String address;
    private Integer currentInvoice;


The PagerModel is just a POJO (Plain Old Java Object), unlike the ClientModel. There are no imports, hence no annotations. This PagerModel is purely just used for helping with the pagination on our webpage. Revisit this model once you read the Thymeleaf template and see the demo pictures. The PagerModel makes more sense when you think about it in context.


package com.michaelcgood.model;

public class PagerModel {
    private int buttonsToShow = 5;

    private int startPage;

    private int endPage;

    public PagerModel(int totalPages, int currentPage, int buttonsToShow) {


        int halfPagesToShow = getButtonsToShow() / 2;

        if (totalPages <= getButtonsToShow()) {

        } else if (currentPage - halfPagesToShow <= 0) {

        } else if (currentPage + halfPagesToShow == totalPages) {
            setStartPage(currentPage - halfPagesToShow);

        } else if (currentPage + halfPagesToShow > totalPages) {
            setStartPage(totalPages - getButtonsToShow() + 1);

        } else {
            setStartPage(currentPage - halfPagesToShow);
            setEndPage(currentPage + halfPagesToShow);


    public int getButtonsToShow() {
        return buttonsToShow;

    public void setButtonsToShow(int buttonsToShow) {
        if (buttonsToShow % 2 != 0) {
            this.buttonsToShow = buttonsToShow;
        } else {
            throw new IllegalArgumentException("Must be an odd value!");

    public int getStartPage() {
        return startPage;

    public void setStartPage(int startPage) {
        this.startPage = startPage;

    public int getEndPage() {
        return endPage;

    public void setEndPage(int endPage) {
        this.endPage = endPage;

    public String toString() {
        return "Pager [startPage=" + startPage + ", endPage=" + endPage + "]";


4 - Repository

The PagingAndSortingRepository is an extension of the CrudRepository. The only difference is that it allows you to do pagination of entities. Notice that we annotate the interface with @Repository to make it visible to @SpringBootApplication.


package com.michaelcgood.dao;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import com.michaelcgood.model.ClientModel;

public interface ClientRepository extends PagingAndSortingRepository<ClientModel,Long> {


5 - Controller

We define some variables in the beginning of the class. We only want to show 3 page buttons at a time. The initial page is the first page of results, the initial amount of items on the page is 5, and the user has the ability to have either 5 or 10 results per page.

We add some example values to our repository with the addtorepository() method, which is defined further below in this class. With theaddtorepository() method, we add several "clients" to our repository, and many of them are hat companies because I ran out of ideas.

ModelAndView is used here rather than Model. ModelAndView is used instead because it is a container for both a ModelMap and a view object. It allows the controller to return both as a single value. This is desired for what we are doing.


package com.michaelcgood.controller;

import java.util.Optional;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.michaelcgood.model.PagerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import com.michaelcgood.dao.ClientRepository;
import com.michaelcgood.model.ClientModel;

public class ClientController {

    private static final int BUTTONS_TO_SHOW = 3;
    private static final int INITIAL_PAGE = 0;
    private static final int INITIAL_PAGE_SIZE = 5;
    private static final int[] PAGE_SIZES = { 5, 10};
    ClientRepository clientrepository;

    public ModelAndView homepage(@RequestParam("pageSize") Optional<Integer> pageSize,
            @RequestParam("page") Optional<Integer> page){


        ModelAndView modelAndView = new ModelAndView("index");
        // Evaluate page size. If requested parameter is null, return initial
        // page size
        int evalPageSize = pageSize.orElse(INITIAL_PAGE_SIZE);
        // Evaluate page. If requested parameter is null or less than 0 (to
        // prevent exception), return initial size. Otherwise, return value of
        // param. decreased by 1.
        int evalPage = (page.orElse(0) < 1) ? INITIAL_PAGE : page.get() - 1;
        // print repo
        System.out.println("here is client repo " + clientrepository.findAll());
        Page<ClientModel> clientlist = clientrepository.findAll(new PageRequest(evalPage, evalPageSize));
        System.out.println("client list get total pages" + clientlist.getTotalPages() + "client list get number " + clientlist.getNumber());
        PagerModel pager = new PagerModel(clientlist.getTotalPages(),clientlist.getNumber(),BUTTONS_TO_SHOW);
        // add clientmodel
        // evaluate page size
        modelAndView.addObject("selectedPageSize", evalPageSize);
        // add page sizes
        modelAndView.addObject("pageSizes", PAGE_SIZES);
        // add pager
        modelAndView.addObject("pager", pager);
        return modelAndView;


public void addtorepository(){

        //below we are adding clients to our repository for the sake of this example
                ClientModel widget = new ClientModel();
                widget.setAddress("123 Fake Street");
                widget.setName("Widget Inc");


                //next client
                ClientModel foo = new ClientModel();
                foo.setAddress("456 Attorney Drive");
                foo.setName("Foo LLP");


                //next client
                ClientModel bar = new ClientModel();
                bar.setAddress("111 Bar Street");
                bar.setName("Bar and Food");

                //next client
                ClientModel dog = new ClientModel();
                dog.setAddress("222 Dog Drive");
                dog.setName("Dog Food and Accessories");

                //next client
                ClientModel cat = new ClientModel();
                cat.setAddress("333 Cat Court");
                cat.setName("Cat Food");

                //next client
                ClientModel hat = new ClientModel();
                hat.setAddress("444 Hat Drive");
                hat.setName("The Hat Shop");

                //next client
                ClientModel hatB = new ClientModel();
                hatB.setAddress("445 Hat Drive");
                hatB.setName("The Hat Shop B");

                //next client
                ClientModel hatC = new ClientModel();
                hatC.setAddress("446 Hat Drive");
                hatC.setName("The Hat Shop C");

                //next client
                ClientModel hatD = new ClientModel();
                hatD.setAddress("446 Hat Drive");
                hatD.setName("The Hat Shop D");

                //next client
                ClientModel hatE = new ClientModel();
                hatE.setAddress("447 Hat Drive");
                hatE.setName("The Hat Shop E");

                //next client
                ClientModel hatF = new ClientModel();
                hatF.setAddress("448 Hat Drive");
                hatF.setName("The Hat Shop F");



6 - Thymeleaf Template

In the Thymeleaf template, the two most important things to note are:

  • Thymeleaf Standard Dialect
  • JavaScript

Like in a CrudRepository, we iterate through the PagingAndSortingRepository with: th:each="clientlist : ${clientlist}" . Except, instead of each item in the repository being an Iterable, the item is a Page.

By selecting class="form-control pagination" id="pageSizeSelect", we are allowing the user to pick a page size of either 5 or 10. We defined these values in our Controller.

Next is the code that allows the user to browse the various pages. This is where our PagerModel comes in to use.

The changePageAndSize() function is the JavaScript function that will update the page size when the user changes it.

<html xmlns="http://www.w3.org/1999/xhtml"

<!-- CSS INCLUDE -->
<link rel="stylesheet"

.pagination-centered {
    text-align: center;

.disabled {
    pointer-events: none;
    opacity: 0.5;

.pointer-disabled {
    pointer-events: none;


    <div class="container-fluid">
        <!-- START PAGE SIDEBAR -->
        <!-- commented out     <div th:replace="fragments/header :: header">&nbsp;</div> -->
        <!-- END PAGE SIDEBAR -->
        <!-- PAGE TITLE -->
        <div class="page-title">
                <span class="fa fa-arrow-circle-o-left"></span> Client Viewer
        <!-- END PAGE TITLE -->
        <div class="row">
            <table class="table datatable">
                    <tr th:each="clientlist : ${clientlist}">
                        <td th:text="${clientlist.name}">Text ...</td>
                        <td th:text="${clientlist.address}">Text ...</td>
                        <td><button type="button"
                                class="btn btn-primary btn-condensed">
                                <i class="glyphicon glyphicon-folder-open"></i>
            <div class="row">
                <div class="form-group col-md-1">
                    <select class="form-control pagination" id="pageSizeSelect">
                        <option th:each="pageSize : ${pageSizes}" th:text="${pageSize}"
                            th:selected="${pageSize} == ${selectedPageSize}"></option>
                <div th:if="${clientlist.totalPages != 1}"
                    class="form-group col-md-11 pagination-centered">
                    <ul class="pagination">
                        <li th:class="${clientlist.number == 0} ? disabled"><a
                            th:href="@{/(pageSize=${selectedPageSize}, page=1)}">&laquo;</a>
                        <li th:class="${clientlist.number == 0} ? disabled"><a
                            th:href="@{/(pageSize=${selectedPageSize}, page=${clientlist.number})}">&larr;</a>
                            th:class="${clientlist.number == (page - 1)} ? 'active pointer-disabled'"
                            th:each="page : ${#numbers.sequence(pager.startPage, pager.endPage)}">
                            <a class="pageLink"
                            th:href="@{/(pageSize=${selectedPageSize}, page=${page})}"
                            th:class="${clientlist.number + 1 == clientlist.totalPages} ? disabled">
                            <a class="pageLink"
                            th:href="@{/(pageSize=${selectedPageSize}, page=${clientlist.number + 2})}">&rarr;</a>
                            th:class="${clientlist.number + 1 == clientlist.totalPages} ? disabled">
                            <a class="pageLink"
                            th:href="@{/(pageSize=${selectedPageSize}, page=${clientlist.totalPages})}">&raquo;</a>
        <!-- END PAGE CONTENT -->
        <!-- END PAGE CONTAINER -->

    <script th:inline="javascript">
        $(document).ready(function() {

function changePageAndSize() {
    $('#pageSizeSelect').change(function(evt) {
        window.location.replace("/?pageSize=" + this.value + "&page=1");


7 - Configuration

The below properties can be changed based on your preferences but were what I wanted for my environment.


# = Thymeleaf configurations 

8 - Demo

This is the homepage.

This is the second page.

I can change the amount of items on the page to 10.

The source code is on GitHub.

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

web dev ,thymeleaf ,java web application ,pagination ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}