Pagination in sturts using Displaytags
Today We are going to learn about
How to do Pagination in sturts using Displaytags
.As all you know Struts is a MVC Architecture.So that It is easy for us to develop and maintain webapplications.The advantage of java community is the jar files it contains needed tasks combined in a file so that we can use it for all the processes.
Here i am using Displaytag-The 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.
The Three Jar Files needed for implementing Displaytag are
1.displaytag-1.2.jar 2.displaytag-export-poi-1.2.jar 3.displaytag-portlet-1.2.jar
These files i have added in lib folder of the example project Download link is at end of this tutorial,so don’t worry about that.
The other Jar files needed are listed below you need to add these jars in classpath of your project
commons-beanutils.jar,commons-collections.jar, commons-collections-3.1.jar,commons-digester.jar, commons-fileupload.jar,commons-fileupload-1.2.1.jar commons-io-1.3.2.jar,commons-lang-2.3.jar,commons-logging.jar commons-logging-1.1.jar,commons-validator.jar,dom4j-1.6.1.jar freemarker-2.3.13.jar,itext-1.3.jar,jakarta-oro.jar javassist-3.9.0.GA.jar,jta-1.1.jar,junit-3.8.1.jar junit-3.8.2.jar,log4j-1.2.15.jar servlet-api.jar,slf4j-api-1.5.8.jar slf4j-log4j12-1.5.8.jar,struts.jar struts2-convention-plugin-2.1.6.jar,struts2-core-2.1.6.jar xwork-2.1.2.jar
These jar files are part of packages of struts and servlet
Lets Start the Project,The data.java contains the needed data for Pagination
package com.s2sgateway;
import java.util.ArrayList;
public class data {
private int no;
private String name;
private int age;
private double salary;
public data() {
}
public data(int no, String name, int age, double salary) {
this.no = no;
this.name = name;
this.age = age;
this.salary = salary;
}
public ArrayList loadData() {
ArrayList userList = new ArrayList();
userList.add(new data(1, "Akilan", 20, 40000));
userList.add(new data(2, "Abu", 30, 37000));
userList.add(new data(3, "Charlos", 49, 35000));
userList.add(new data(4, "Edwin", 44, 22000));
userList.add(new data(5, "Gourav", 33, 22500));
userList.add(new data(6, "Kennady", 51, 21500));
userList.add(new data(7, "Mani", 27, 29500));
userList.add(new data(8, "Lal", 58, 19000));
userList.add(new data(9, "Tawak", 67, 18800));
userList.add(new data(10, "Ameen", 73, 23000));
userList.add(new data(11, "jani", 61, 32000));
userList.add(new data(12, "Iqbal", 28, 17500));
userList.add(new data(12, "Wasim", 54, 22000));
userList.add(new data(12, "Asho", 65, 31500));
userList.add(new data(15, "Arnold", 60, 24000));
userList.add(new data(16, "Jamal", 24, 100000));
userList.add(new data(17, "Safiq", 20, 100000));
userList.add(new data(18, "Li-jing", 61, 27000));
userList.add(new data(19, "Charles ", 43, 41000));
userList.add(new data(19, "David", 38, 26000));
return userList;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
The FormFile contains the setter and getter method to get the datas from data file in arraylist.
package com.s2sgateway;
import java.util.ArrayList;
import org.apache.struts.action.ActionForm;
public class FormFile extends ActionForm {
private ArrayList dataList;
public ArrayList getDataList() {
return dataList;
}
public void setDataList(ArrayList dataList) {
this.dataList = dataList;
}
}
The Action File Contains the code to call the method of the formfile and data file to load the data’s, once the success returns the value is displayed in the user page.
package com.s2sgateway;
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 ActionFile extends Action {
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FormFile formFile= (FormFile) form;
data userdata = new data();
formFile.setDataList(userdata.loadData());
return mapping.findForward(SUCCESS);
}
}
The index file <jsp:forward page=”actionFile.do”/> forwards the page to struts-config.xml after the success returns the code transferred to the user page to display the data.The user file contains the code to display the data using displaytag.
<%@page buffer="16kb"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts-pagination tutorial by http://s2sgateway.com</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="#ff550">
<h2>Struts-pagination tutorial by http://s2sgateway.com</h2>
<%
try
{response.flushBuffer();
}
catch(Exception e)
{}
finally
{}
%>
<display:table export="true" id="data"
name="sessionScope.FormFile.dataList"
requestURI="/actionFile.do" pagesize="10" >
<display:column property="no" title="No" sortable="true" />
<display:column property="name" title="Name" sortable="true" />
<display:column property="age" title="Age" sortable="true" />
<display:column property="salary" title="Salary"
sortable="true" />
</display:table>
</body>
</html>
Here in this code the sortable element is set to true to sort the values,Export is set to true to give the options to export in three default types
1.Excel 2.xml 3.Csv
The pagination is works fine by displaying 10 values per page of 20 values given in data file,When You Click the title links the values is sortable.
So download the complete code here
strutspagination
After downloading the project import the project in eclipse and add the required jars and run the project,Happy coding
Related Posts
Pagination in Java/JspRelated Posts





Pingback: How to export data as pdf in struts using displaytags | s2sgateway