In this section, you will learn how to add pagination in your Struts2 application using Display tag.
Struts2 Pagination Using Display Tags
In this section, you will learn how to add pagination in your Struts2 application using Display tag.
Using Display tag, its become very easy to add pagination in your application. It is specially beneficial where you need to display a long list of data in a limited space without changing the page.
In this tutorial, the top ten list of richest sports person in world in the year 2009(according to Forbes magazine) is displayed using Struts2 with Display tag library 1.2.
The jar file used in the application is given below :
You can download the Display tag library 1.2 from here .
The hierarchy of the project is given below :
CODE
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="HomeForm" type="com.devmanuals.struts2.HomeForm"/> </form-beans> <action-mappings> <action input="/" name="HomeForm" path="/homeAction" scope="session" type="com.devmanuals.struts2.HomeAction"> <forward name="success" path="/home.jsp" /> </action> </action-mappings> </struts-config>
RichSportsmanList.java
package com.devmanuals.struts2; import java.util.ArrayList; public class RichSportsmanList { private int place; private String name; private int age; private double annualIncome; public RichSportsmanList(){ } public RichSportsmanList(int place, String name, int age, double annualIncome) { this.place = place; this.name = name; this.age = age; this.annualIncome = annualIncome; } public ArrayList<RichSportsmanList> loadData() { ArrayList<RichSportsmanList> sportsmanList = new ArrayList<RichSportsmanList>(); sportsmanList.add(new RichSportsmanList(1, "Tiger Woods", 35, 115)); sportsmanList.add(new RichSportsmanList(2, "David Beckham", 36, 50)); sportsmanList.add(new RichSportsmanList(3, "Michael Jordan", 48, 45)); sportsmanList.add(new RichSportsmanList(4, "Phil Mickelson", 41, 45)); sportsmanList.add(new RichSportsmanList(5, "Kimi Raikkonen", 32, 44)); sportsmanList.add(new RichSportsmanList(6, "Kobe Bryant", 33, 39)); sportsmanList.add(new RichSportsmanList(7, "LeBron James", 26, 38)); sportsmanList.add(new RichSportsmanList(8, "Ronaldinho", 31, 37)); sportsmanList.add(new RichSportsmanList(9, "Roger Federer", 35, 30)); sportsmanList.add(new RichSportsmanList(10,"Alex Rodriguez", 36, 34)); return sportsmanList; } public int getPlace() { return place; } public void setPlace(int place) { this.place = place; } 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 getAnnualIncome() { return annualIncome; } public void setAnnualIncome(double annualIncome) { this.annualIncome = annualIncome; } }
HomeForm.java
package com.devmanuals.struts2; import java.util.ArrayList; public class HomeForm extends org.apache.struts.action.ActionForm { private ArrayList<RichSportsmanList> richSportsmanList; public ArrayList<RichSportsmanList> getRichSportsmanList() { return richSportsmanList; } public void setRichSportsmanList(ArrayList<RichSportsmanList> richSportsmanList) { this.richSportsmanList = richSportsmanList; } }
HomeAction.java
package com.devmanuals.struts2; 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 HomeAction extends Action { private final static String SUCCESS = "success"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HomeForm homeForm = (HomeForm) form; RichSportsmanList sportsmanList = new RichSportsmanList(); homeForm.setRichSportsmanList(sportsmanList.loadData()); return mapping.findForward(SUCCESS); } }
index.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <jsp:forward page="homeAction.do"/>
home.jsp
<!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>Forbes Top 10 Richest Sports Stars 2009</title> <link href="css/displaytag.css" rel="stylesheet" type="text/css" /> </head> <body> <h2>Forbes Top 10 Richest Sports Stars 2009</h2> <display:table export="true" id="data" name="sessionScope.HomeForm.richSportsmanList" requestURI="/homeAction.do" pagesize="3"> <display:column property="place" title="Place" sortable="true"/> <display:column property="name" title="Name" sortable="true"/> <display:column property="age" title="Age" sortable="true"/> <display:column property="annualIncome" title="Income(in m/yr)" sortable="true" /> </display:table> <p>Note : List is according to Forbes List 2009</p> </body> </html>
OUTPUT
After executing the project, you will get the following output :
[ 0 ] Comments