Struts displaytag tutorial: Sort / Pagination data using displaytag in Struts

Struts display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model. The library provides a significant amount of functionality while still being easy to use. Displaytag can handle column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table in a customizable XHTML style.

In the following example we will see how to dispaly data using display tag and to do pagination and sorting. We will use Eclipse as an IDE for our example.

Step 1: Create Eclipse dynamic web project and copy JAR files

Start Eclipse and goto File -> New -> Project -> Dynamic Web Project
struts dynamic web project

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from


Step 2: Create Action, Form and Bean class

Once the project is created, create 3 java files ForbesData, UserAction and UserForm in package net.viralpatel.struts.displaytag.

Copy following content into file.

package net.viralpatel.struts.displaytag;

import java.util.ArrayList;

public class ForbesData {
	private int rank;
	private String name;
	private int age;
	private double netWorth;

	public ForbesData() {

	public ForbesData(int rank, String name, int age, double netWorth) {
		this.rank = rank; = name;
		this.age = age;
		this.netWorth = netWorth;
	public ArrayList loadData() {
		ArrayList userList = new ArrayList();
		userList.add(new ForbesData(1, "William Gates III", 53, 40.0));
		userList.add(new ForbesData(2, "Warren Buffett", 78, 37));
		userList.add(new ForbesData(3, "Carlos Slim Helu & family", 69, 35));
		userList.add(new ForbesData(4, "Lawrence Ellison", 64, 22.5));
		userList.add(new ForbesData(5, "Ingvar Kamprad & family", 83, 22));
		userList.add(new ForbesData(6, "Karl Albrecht", 89, 21.5));
		userList.add(new ForbesData(7, "Mukesh Ambani", 51, 19.5));
		userList.add(new ForbesData(8, "Lakshmi Mittal", 58, 19.3));
		userList.add(new ForbesData(9, "Theo Albrecht", 87, 18.8));
		userList.add(new ForbesData(10, "Amancio Ortega", 73, 18.3));
		userList.add(new ForbesData(11, "Jim Walton", 61, 17.8));
		userList.add(new ForbesData(12, "Alice Walton", 59, 17.6));
		userList.add(new ForbesData(12, "Christy Walton & family", 54, 17.6));
		userList.add(new ForbesData(12, "S Robson Walton", 65, 17.6));
		userList.add(new ForbesData(15, "Bernard Arnault", 60, 16.5));
		userList.add(new ForbesData(16, "Li Ka-shing", 80, 16.2));
		userList.add(new ForbesData(17, "Michael Bloomberg", 67, 16));
		userList.add(new ForbesData(18, "Stefan Persson", 61, 14.5));
		userList.add(new ForbesData(19, "Charles Koch", 73, 14));
		userList.add(new ForbesData(19, "David Koch", 68, 14));
		userList.add(new ForbesData(21, "Liliane Bettencourt", 86, 13.4));
		userList.add(new ForbesData(22, "Prince Alwaleed Bin Talal Alsaud", 54, 13.3));
		return userList;
	public int getRank() {
		return rank;
	public void setRank(int rank) {
		this.rank = rank;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;
	public double getNetWorth() {
		return netWorth;
	public void setNetWorth(double netWorth) {
		this.netWorth = netWorth;

Copy following content into

package net.viralpatel.struts.displaytag;

import java.util.ArrayList;

public class UserForm extends org.apache.struts.action.ActionForm {

	private ArrayList forbesList;

	public ArrayList getForbesList() {
		return forbesList;

	public void setForbesList(ArrayList forbesList) {
		this.forbesList = forbesList;

Copy following content into

package net.viralpatel.struts.displaytag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UserAction extends Action {
    private final static String SUCCESS = "success";
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        UserForm userForm = (UserForm) form;
        ForbesData actorData = new ForbesData();
        return mapping.findForward(SUCCESS);

Step 3: Create JSPs, struts-config.xml and web.xml

Create index.jsp and user.jsp in WebContent folder and struts-config.xml and web.xml in WebContent/WEB-INF folder.

Copy following content into appropriate files.


<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>


<%@taglib uri="" prefix="display" %>

        The World's Billionaires 2009

The World's Billionaires 2009 - Forbes List







Step 4: Execute the project

We are done with the project. Now execute the project in eclipse or create a WAR file and run it in Tomcat.



  • naresh 11 December, 2013, 23:06

    viralpatel nice tutorial,,,

    plz send me this project code plz……..

    • naga 14 August, 2014, 12:56

      please tell me how to change the css of the previous and next buttons.

  • Rajesh 13 December, 2013, 13:19

    Example is good please send me the source code which contains pagination, please

  • Nitin 20 December, 2013, 10:16

    Viral if my Object size is too large say 5000 book objects than java throws outofmemory exception
    Because display tag library loads all object first time(Eager loading) and then apply Pagination….if you have solution pls tell me Thnx in advance…

    • jose 20 March, 2014, 2:18


      I have the same question, do you get the answer? help please!


  • samkhan 3 January, 2014, 1:38

    please send me the code

  • Amir 31 January, 2014, 2:33

    Great example, can you please send me the project and the jars links where got them from?

  • fighou zoubair 6 February, 2014, 18:28

    why you didn’t include source file bro :(

  • Scratlin 22 March, 2014, 11:41

    Trillions of Thanks to u for ur great efforts.
    Keep posting n keep helping :) :)

  • Ali 25 March, 2014, 19:05

    Hello Viral Sir,
    Its a Really great job done by You .
    If you don’t mind will you please send me the full code of Pagination Project

  • Ali 26 March, 2014, 12:41

    Hello Viral Sir,
    We need the source code of StrutsDisplayTag.
    It is very necessary to us.
    Because we have so many errors.
    That’s why we need code sir.
    So please as early as possible upload the full code of the project.

  • REST 13 April, 2014, 4:28

    Friend, do you indicate in the tutorial, but when I run it shows “HTTP Status 503 – Servlet action is not available at this time” and I made sure if running other examples and work normally. There’s something extra we need to do?

  • Alain 29 May, 2014, 19:47

    Great Jobs,
    Please send the code.

  • naga 30 May, 2014, 11:40

    please tell me how to change the next pre buttion styles

  • Mohan 11 June, 2014, 12:04

    I m not getting output of this code please help me out…. I got an error like..
    The requested resource (/strutsPagination/) is not available.

  • parth 26 August, 2014, 14:08

    I have an error in <display:column tag in sorting the values are not sorted in well manner ,
    kindly request you to guide me

  • Ajit Raju 22 September, 2014, 19:40

    Hi Viral,

    I want to display 100 , 100 items at a time after each hunder the next button should get enabled or disabled based on the presence of next record.
    e.g : i have 130 records on first instance i want to fetch 100 records and display so the next button should get enable as next 30 record is there. on click of next record from 101 to 130 will
    get fetch and display with disabled next button.

    Need help on this as i am on a way with an implementation.


    • victor 25 March, 2015, 9:57

      did you get any solution for your question. im looking for the same approach Ajit

  • chinna 10 October, 2014, 16:39

    i am getting error here
    please help me how to resolve it

  • Kevi 16 December, 2014, 3:22

    How would i add an id to each cell element in your example?

  • chanaiah 6 February, 2015, 10:52

    Hi sir,
    i want source code because we are getting errors while executing the application.

  • suman 12 March, 2015, 19:43

    i asked you to send the code.but still i didnt get into my mail

Leave a Reply

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


To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]