Pages Navigation Menu

Coding is much easier than you think

Pagination in Struts 2 Using display tag

pagination in struts 2
In Struts 2 Pagination can be implemented with the help of Displaytag. This tag can handle feature such as pagination, sorting and exporting of a table. In the following example we will see how to display data using display tag and to do pagination, sorting and exporting.
** UPDATE: Struts 2 Complete tutorial now available here.
Download the Latest version of displaytag.jar here
Now Create a Dynamic web project and make sure that you have the following libraries are in your WEB-INF/lib directory.


displaytag-export-poi.jar is used to export the files in xls,csv or xml format.

Action Class

package com.simplecode.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.simplecode.form.StudentBean;

public class StudentAction extends ActionSupport 
private static final long serialVersionUID = 1L;
private List<StudentBean> students = new ArrayList<StudentBean>();

public String displayStudentList() 
	students.add(new StudentBean("o7bb002", "Gokul", "ECE", 4));
	students.add(new StudentBean("o7bc074", "Muthu Vijay", "CSE", 6));
	students.add(new StudentBean("o7bb040", "Jaya Prakash", "ECE", 10));
	students.add(new StudentBean("o7bc055", "Mohiadeen", "CSE", 7));
	students.add(new StudentBean("o7bd047", "HariPriya", "IT", 1));
	students.add(new StudentBean("o7bd024", "Pavithra", "IT", 3));
	students.add(new StudentBean("o7bb009", "Aswin", "ECE", 8));
	students.add(new StudentBean("o7ba029", "Sharmila", "IT", 11));
	students.add(new StudentBean("o7ba027", "Nilafar", "IT", 2));
	students.add(new StudentBean("o7bd081", "Dinesh Babu", "MECH", 13));
	students.add(new StudentBean("o7ba062", "Lourde", "MECH", 9));
	students.add(new StudentBean("o7bc079", "Nisha", "CSC", 5));
	students.add(new StudentBean("o7bb039", "Guru Prasad", "MECH", 12));
	students.add(new StudentBean("o7bc033", "Gowtham Raj", "CSE", 15));
	students.add(new StudentBean("o7bb039", "Ibrahim Sha", "ECE", 14));
	students.add(new StudentBean("o7bd081", "Dinesh Babu", "MECH", 16));

	return SUCCESS;

public List<StudentBean> getStudents() 
	return students;

public void setStudents(List<StudentBean> students) 
	this.students = students;

package com.simplecode.form;

public class StudentBean 
private String rollNo;
private String studentName;
private String department;
private int rank;

public StudentBean(String rollNo, String studentName, String department, int rank) 
    this.rollNo = rollNo;
    this.studentName = studentName;
    this.department = department;
    this.rank = rank;
public String getRollNo() 
	return rollNo;
public void setRollNo(String rollNo) 
	this.rollNo = rollNo;
public String getStudentName() 
	return studentName;
public void setStudentName(String studentName) 
	this.studentName = studentName;
public String getDepartment() 
	return department;
public void setDepartment(String department) 
	this.department = department;
public int getRank() 
	return rank;
public void setRank(int rank) 
	this.rank = rank;


<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="display" uri=""%>

<title>Students Details</title>
<style type="text/css">

/* for hiding the page banner */
	display: none;
/* for customizing page links */
	color: maroon;
	margin: 20px 0px 20px 50px;
/* for shifting all the Export options*/
	margin: 20px 0px 20px 30px;
/* For changing the spaces between export link */
	margin-left: 30px;
/* For Table css */
	border: 1px solid #666;
	width: 60%;
	margin: 20px 0 20px 0px;
/* For odd and even row decoration */
	background-color: #fff
	background-color: #CCCCCC
/* Css for table elements */
	padding: 2px 4px 2px 4px;
	text-align: left;
	vertical-align: top;
thead tr 
	background-color: #999999;
/* For changing the background colour while sorting */
	background-color: #CCCCCC;
th.sorted a,th.sortable a 
	background-position: right;
	display: block;
	width: 100%;
th a:hover 
	text-decoration: underline;
	color: black;
th a,th a:visited 
	color: black;

<display:table id="studentTable" name="students" pagesize="5" cellpadding="5px;"
cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">
<display:column property="rollNo" title="Roll No"/>
<display:column property="studentName" title="Student Name"/>
<display:column property="department" title="Department"/>
<display:column property="rank" title="Rank"/>


How to make Struts 2 tag work inside Display tag?
How to get checkbox values from displaytag using struts2
Displaytag export option is not working?

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<package name="default" extends="struts-default">
<action name="displayStudentList" class="com.simplecode.action.StudentAction" 
   <result name="success">/displaytag.jsp</result>

After executing the project, you will get the following output:
Recommended reading :

  • AJAX implementation in Struts 2 using JQuery and JSON
  • Gridview in Struts2 using jQuery DataTable
  • CRUD Operations in Struts 2 using jTable jQuery plugin via Ajax
    If you want to allow the user to sort the data displayed then set the attribute sortable=”true” on the display:column tag that you want to sort by.

    <display:column property="rollNo" title="Roll No" sortable="true"/>
    <display:column property="studentName" title="Student Name" sortable="true"/>
    <display:column property="department" title="Department" sortable="true"/>
    <display:column property="rank" title="Rank" sortable="true"/>

    Now on clicking the rank header the student resultset are sorted according to rank as shown below.

    sort in pagination
    Here the problem is whenever we sort the table by clicking on any sortable column header it only sort the data visible in that page. We can override this behavior by setting sort attribute in column to list as shown below.

    <display:table export="true" id="studentTable" name="students" pagesize="5" cellpadding="5px;"
    cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="" sort = "list">


    So now when you click on sort, the data will be sorted based on whole list.
    Data exporting
    When you set the Table Tag’s export attribute to “true”, footer will appear below the sorted result, which will allow you to export the data being shown in various formats(CSV, XLS, and XML).
    ** UPDATE: Struts 2 Complete tutorial now available here.

    <display:table export="true" id="studentTable" name="students" pagesize="5" cellpadding="5px;"
    cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">

    export on pagination
    Suppose if you want to place the banner at bottom , then you can use the following code, within the display-table tag

    <display:setProperty name="paging.banner.placement" value="bottom" />

    ** Update — The below screenshot is the demo of pagination in struts 2 via ajax using using jQuery jTable plugin, this plugin has several inbuilt theme and lot may cool features like crud implementation via ajax, internalization etc, to know more about this plugin, please refer the article here.


    About Mohaideen Jamil

    I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

    %d bloggers like this: