3:50 pm - Thursday February 19, 3880

Pagination tutorial and example program in Struts 1x

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044


Pagination is the process of organizing or splitting large amount of data or rows into manageable size and numbering them to view the set of content, when you’re having large number of rows to be displayed in a web page we should organize them to display them as page 1,2,3 etc.

In this struts tutorial we will learn how to create pagination for large data and sort them in ascending or descending order using displaytag.tld file in struts, display tag can handle data to display in ascending , descending order and decoration of a table in a customizable manner.

Displaytag.tld is an open source suit you can down it from http://www.displaytag.org/10/tlds.html

Project  Structure                                             



 Tld Files

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



package Com.Candidjava;

import java.util.ArrayList;

public class EmployeeData {

	private String id;
	private String userName;
	private String emailId;

	public EmployeeData(String Id, String userName, String emailId) {
		this.id = Id;
		this.userName = userName;
		this.emailId = emailId;

	public EmployeeData() {
		// TODO Auto-generated constructor stub

	public ArrayList loadData() {
		ArrayList userList = new ArrayList();
		userList.add(new EmployeeData("001", "Mathanlal sait","mathanlal@candidjava.com"));
		userList.add(new EmployeeData("002", "Anburaj","anburaj@candidjava.com"));
		userList.add(new EmployeeData("003", "Vignesh","vignesh@candidjava.com"));
		userList.add(new EmployeeData("004", "Antony", "antony@candidjava.com"));
		userList.add(new EmployeeData("005", "Gopukumar", "gobu@candidjava.com"));
		userList.add(new EmployeeData("006", "Kalaimani","kalaimani@candidjava.com"));
		userList.add(new EmployeeData("007", "karthika","karthika@candidjava.com"));
		userList.add(new EmployeeData("008", "deepa", "deepa@candidjava.com"));
		userList.add(new EmployeeData("009", "Prabhu", "praphu@candidjava.com"));
		userList.add(new EmployeeData("010", "karupia","karupia@candidjava.com"));
		userList.add(new EmployeeData("011", "Bala", "bala@candidjava.com"));
		userList.add(new EmployeeData("012", "Mahesh", "mahesh@candidjava.com"));
		userList.add(new EmployeeData("013", "Rajendran","rajendran@candidjava.com"));
		return userList;

	 * @return the id
	public String getId() {
		return id;

	 * @return the userName
	public String getUserName() {
		return userName;

	 * @return the emailId
	public String getEmailId() {
		return emailId;



# -- standard errors --
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.


package Com.Candidjava;

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

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

public class UserAction extends org.apache.struts.action.Action {

	/* forward name="success" path="" */
	private final static String SUCCESS = "success";

	 * This is the action called from the Struts framework.
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @throws java.lang.Exception
	 * @return
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
		EmployeeData empData = new EmployeeData();
		return mapping.findForward(SUCCESS);


package Com.Candidjava;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

	private ArrayList employeeList;

	public UserForm() {
		// TODO Auto-generated constructor stub

	 * @return the employeeList
	public ArrayList getEmployeeList() {
		return employeeList;

	 * @param employeeList
	 *            the employeeList to set
	public void setEmployeeList(ArrayList employeeList) {
		this.employeeList = employeeList;



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


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

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

		<form-bean name="UserForm" type="Com.Candidjava.UserForm" />



		<forward name="welcome" path="/Welcome.do" />

		<action input="/" name="UserForm" path="/userAction" scope="session"
			<forward name="success" path="/user.jsp" />
		<action path="/Welcome" forward="/welcomeStruts.jsp" />

	<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" />

	<message-resources parameter="Com/Candidjava/ApplicationResource" />

		========================= Tiles plugin ===============================
		This plugin initialize Tiles definition factory. This later can takes
		some parameters explained here after. The plugin first read parameters
		from web.xml, thenoverload them with parameters defined here. All
		parameters are optional. The plugin should be declared in each
		struts-config file. - definitions-config: (optional) Specify
		configuration file names. There can be several comma separated file
		names (default: ?? ) - moduleAware: (optional - struts1.1) Specify if
		the Tiles definition factory is module aware. If true (default), there
		will be one factory for each Struts module. If false, there will be
		one common factory for all module. In this later case, it is still
		needed to declare one plugin per module. The factory will be
		initialized with parameters found in the first initialized plugin
		(generally the one associated with the default module). true : One
		factory per module. (default) false : one single shared factory for
		all modules - definitions-parser-validate: (optional) Specify if xml
		parser should validate the Tiles configuration file. true : validate.
		DTD should be specified in file header (default) false : no validation

		Paths found in Tiles definitions are relative to the main context.
	<plug-in className="org.apache.struts.tiles.TilesPlugin">
		<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
		<set-property property="moduleAware" value="true" />

		============= Validator plugin	=============
	<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
		<set-property property="pathnames"
			value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<jsp:forward page="userAction.do" />


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<display:table id="data" name="sessionScope.UserForm.employeeList"
	requestURI="/userAction.do" pagesize="10">
	<display:column property="id" title="Id" sortable="true" />
	<display:column property="userName" title="User Name" sortable="true" />
	<display:column property="emailId" title="Email Id" sortable="true" />


@charset "utf-8";

/* CSS Document */
table {
	border: 1px solid #666;
	width: 80%;
	margin: 20px 0 20px 0 !important;

th,td {
	padding: 2px 4px 2px 4px !important;
	text-align: left;
	vertical-align: top;

thead tr {
	background-color: #0B615E;

th.sorted {
	background-color: #0B615E;

th a,th a:visited {
	color: blue;

th a:hover {
	text-decoration: underline;
	color: black;

th.sorted a,th.sortable a {
	background-position: right;
	display: block;
	width: 100%;

th.sortable a {
	background-image: url(../img/arrow_off.png);
	background-repeat: no-repeat;

th.order1 a {
	background-image: url(../img/arrow_down.png);
	background-repeat: no-repeat;

th.order2 a {
	background-image: url(../img/arrow_up.png);
	background-repeat: no-repeat;

tr.odd {
	background-color: #fff

tr.tableRowEven,tr.even {
	background-color: #CCCCCC

.group-1 {
	font-weight: bold;
	padding-bottom: 10px;
	border-top: 1px solid black;

.group-2 {
	font-style: italic;
	border-top: 1px solid black;

.grouped-table tr.even {
	background-color: #fff;

.grouped-table tr.odd {
	background-color: #fff;


DOWNLOAD+SOURCE CODE                                                                                                          BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply