Struts 2 pagination using display tag

In this Struts 2 tutorial, we will talk about pagination using display tag library. Showing a huge amount of records on a single page is not a good idea. You can divide it into several pages. In Struts 2, you can do that using display tag library. This library is reached in features like…

  1. Paging
  2. Sorting
  3. Grouping
  4. Exporting, etc.

Similar tutorial- Pagination in Struts 2 using jQuery datatable

Dependencies Required

To resolve the JARs dependency, you can add the following in your pom.xml file.


Or you can directly download it by clicking here.

Tag Description

Here is some tag and their properties that we are going to use in this example.

First of all, to work with display tag, the following tag extension should be placed in each JSP page that uses the display taglib.

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

<display:table> tag: Displays a list in an HTML table, formatting each item in the list according to the Column tags nested inside of this tag.

Attribute NameDescription
nameName of the list that contains records.
pagesizeNumber of records to be displayed on a single page.
requestedURIURL that you are requested for pagination.
lengthNumber of records to be shown out of the total.
exportEnable/disable export. Valid values are true or false.
sortUse ‘page’ if you want to sort only visible records, or ‘list’ if you want to sort the full list, or ‘external’ if the data is sorted outside displaytag.

<display:column> tag: Displays a property of a row object inside a table. MUST be nested inside of a Table tag.

Attribute NameDescription
propertyName of the property in the bean specified in the parent table tag (via the “name” attribute) mapped to this column.
titleTitle of the column (text for the th cell).
sortableSet to ‘true’ to make the column sortable. Defaults to ‘false’.
valueStatic value to be used for the column.

<display:caption> tag: Simple tag which mimics the html caption tag. Use it inside a table tag to display a caption.

<display:caption>City list of State Andhra Pradesh</display:caption>

<display:footer> tag:
Tag which should be nested into a table tag to provide a custom table footer. The body of the tag is into the tfoot section of the table.

        <td>Thank You</td>

<display:setProperty> tag:
Sets the indicated property on the enclosing Table tag. MUST be nested within a Table tag.

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

Struts 2 Filter

Define the Struts 2 filter in web.xml file.


DAO Class

Create the Data Access Object class to fetch the city list details from the table.
package org.websparrow.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DisplayDao {

	public static ResultSet report() throws SQLException {
		ResultSet rs = null;
		try {
			Connection con = DriverManager.getConnection("jdbc:mysql://", "root", "");
			PreparedStatement ps = con.prepareStatement("SELECT * FROM cities where city_state='Andhra Pradesh'");
			rs = ps.executeQuery();
		} catch (Exception e) {
		return rs;

Action Class

Inside the Action class add all records into an ArrayList.
package org.websparrow.action;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class DisplayAction extends ActionSupport {
	private static final long serialVersionUID = -7591893545033222898L;

	private DisplayAction dto = null;
	private int cityId;
	private String cityName;
	private String cityState;
	private List<DisplayAction> cityList = null;

	public String execute() throws Exception {
		ResultSet rs =;
		cityList = new ArrayList<DisplayAction>();

		if (rs != null) {
			while ( {
				dto = new DisplayAction();

		return SUCCESS;

	public List<DisplayAction> getCityList() {
		return cityList;

	public void setCityList(List<DisplayAction> cityList) {
		this.cityList = cityList;

	public int getCityId() {
		return cityId;

	public void setCityId(int cityId) {
		this.cityId = cityId;

	public String getCityName() {
		return cityName;

	public void setCityName(String cityName) {
		this.cityName = cityName;

	public String getCityState() {
		return cityState;

	public void setCityState(String cityState) {
		this.cityState = cityState;


Finally, map the action class inside the struts.xml.

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	<constant name="struts.devMode" value="true" />
	<package name="default" extends="struts-default" namespace="/">
		<action name="pagination" class="org.websparrow.action.DisplayAction">
			<result name="success">/index.jsp</result>

UI Component

Create the JSP page for the user interface.

<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="display" uri=""%>
<style type="text/css">
table, th, td {
	border: 1px solid black;

table {
	width: 38%;
	<h2>Struts 2 pagination using display tag</h2>
	<display:table name="cityList" pagesize="12" requestURI="pagination">

		<display:caption>City list of State Andhra Pradesh</display:caption>

		<display:column property="cityId" title="City ID" />
		<display:column property="cityName" title="City Name" />
		<display:column property="cityState" title="State" />

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


Now everything is all set. Deploy the project inside the Tomcat » webapps folder, start the server and hit the following URL  localhost:8087/Struts2DisplayTag/pagination. You will get the output as shown in the image.

Struts 2 pagination using display tag


If you want to sort the table records, add the sortable="true" attribute to <display:column> tag.

This will sort the record for current page only but if you want to apply sorting on whole records, add sort="list" attribute on <display:table> tag.


To enable the exporting, add the export="true" attribute on <display:table> tag.

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.