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.

commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
displaytag-1.2.jar
freemarker-2.3.19.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
xwork-core-2.3.8.jar

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


 
Action Class
 
StudentAction.java

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;
}
}

 
StudentBean.java
 

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;
}
}

 
displaytag.jsp
 


<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="display" uri="http://displaytag.sf.net"%>

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

/* for hiding the page banner */
.pagebanner 
{
	display: none;
}
/* for customizing page links */
.pagelinks 
{
	color: maroon;
	margin: 20px 0px 20px 50px;
}
/* for shifting all the Export options*/
.exportlinks 
{
	margin: 20px 0px 20px 30px;
}
/* For changing the spaces between export link */
.export 
{
	margin-left: 30px;
}
/* For Table css */
table 
{
	border: 1px solid #666;
	width: 60%;
	margin: 20px 0 20px 0px;
}
/* For odd and even row decoration */
tr.odd 
{
	background-color: #fff
}
tr.tableRowEven,tr.even 
{
	background-color: #CCCCCC
}
/* Css for table elements */
th,td 
{
	padding: 2px 4px 2px 4px;
	text-align: left;
	vertical-align: top;
}
thead tr 
{
	background-color: #999999;
}
/* For changing the background colour while sorting */
th.sorted 
{
	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;
}
</style>

<html>
<body>
<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"/>
</display:table>
</body>
</html>

 
Questions:

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?
 
struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="displayStudentList" class="com.simplecode.action.StudentAction" 
                  method="displayStudentList">
   <result name="success">/displaytag.jsp</result>
</action>
</package>
</struts>

 
After executing the project, you will get the following output:
 
pagination
 
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
  •  
    Auto-sorting
     
    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.
     
    sort
     
    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.
     
    Struts-2-jTable-jQuery-plug-in-Create-record-animation-effect
     

    download
     

    About Mohaideen Jamil


    Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.