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<ForbesData> loadData() {
		ArrayList<ForbesData> userList = new ArrayList<ForbesData>();
		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 &amp; family", 69, 35));
		userList.add(new ForbesData(4, "Lawrence Ellison", 64, 22.5));
		userList.add(new ForbesData(5, "Ingvar Kamprad &amp; 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 &amp; 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<ForbesData> forbesList;

	public ArrayList<ForbesData> getForbesList() {
		return forbesList;

	public void setForbesList(ArrayList<ForbesData> 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"%>

<jsp:forward page=""/>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@taglib uri="" prefix="display" %>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The World's Billionaires 2009</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
    <h2>The World's Billionaires 2009 - Forbes List</h2>
        <display:table export="true"  id="data" 
        			requestURI="/" pagesize="10" >
            <display:column property="rank" title="Rank" sortable="true"   />
            <display:column property="name" title="Name" sortable="true"  />
            <display:column property="age" title="Age" sortable="true"  />
            <display:column property="netWorth" title="Net worth ($BIL)" 
            		sortable="true"  />


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

        <form-bean name="UserForm" 

        <forward name="welcome"  path="/"/>

        <action input="/" name="UserForm" path="/userAction" 
        	scope="session" type="net.viralpatel.struts.displaytag.UserAction">
            <forward name="success" path="/user.jsp" />
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    <message-resources parameter="com/vaannila/ApplicationResource"/>


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns=""

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.


Get our Articles via Email. Enter your email address.

You may also like...


  1. naresh says:

    viralpatel nice tutorial,,,

    plz send me this project code plz……..

    • naga says:

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

  2. Rajesh says:

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

  3. Nitin says:

    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 says:


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


  4. samkhan says:

    please send me the code

  5. Amir says:

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

  6. fighou zoubair says:

    why you didn’t include source file bro :(

  7. Scratlin says:

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

  8. Ali says:

    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

  9. Ali says:

    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.

  10. REST says:

    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?

  11. Alain says:

    Great Jobs,
    Please send the code.

  12. naga says:

    please tell me how to change the next pre buttion styles

  13. Mohan says:

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

  14. parth says:

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

  15. Ajit Raju says:

    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 says:

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

  16. chinna says:

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

  17. Kevi says:

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

  18. chanaiah says:

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

  19. suman says:

    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 *