Tutorials
Updated On: Sunday, August 16, 2009
Created Date: Tuesday, July 07, 2009
Pagination Using Pager Tags
Overview
The Pager Tag Library is the easy and flexible way to implement paging of large data sets in JavaServer Pages (JSP). It can emulate all currently known paging styles with minimal effort. It also includes re-usable index styles that emulate the search result navigators of popular web sites such as GoogleSM, AltaVista® and Yahoo!. The Pager Tag Library does most of the work for you by dynamically organizing your data set into pages and generating a browsable index with virtually any look desired. JSPTags.com offers JSP developers a directory of resources related to JavaServer Pages, Servlets and Java. As the name JSPTags.com implies, special interest is given to JSPTag Libraries. Many developers are working with and designing new JSP Tag Libraries and JSPTags.com will provide a source for news, release information and documentation. The Pager Tag Library is distributed as a ready-to-run Web Application Archive (WAR). You can deploy the distribution WAR file as-is to your JavaServer Pages 1.1 compliant server. The WAR file may also be used as the basis for a new web application or integrated into an existing web application. Pager Tag Library can be downloaded from
Pre-Requisites:
- jdk1.6.0_11 ( works with previous versions of JDK)
- Note: for previous versions of JDK. All the .java files should be compiled once again. Make sure that all the supporing files are in classpath. To explore more about classpath refer Set Classpath
- pager-taglib.jar
- pager-src-jar (Optional)
- apache-tomcat-6.0.16 (Works with previous versions)
Download Location:
- Click here to download Pager Tag Demo application
- Click here to go to download page
- Page Tag Library Demo in online
Required .jar files
If you would like to make use of the Pager Tag in your own application,
do the following:
Drop the pager-taglib.jar file in your application WEB-INF/lib directory
Make sure that following libraries are in your WEB-INF/lib directory (or
made available via the classpath to your application server). You can
download a copy of everything from http://jsptags.com/tags/navigation/pager/download.jsp
or you can grab them from the example webapp in the binary distribution.
The following is the list of dependencies:
- pager-taglib.jar
- pager-src-jar (Optional)
Steps to run the sample web application
Add this JAR files to your project build path
- Download the binary distributions from the http://jsptags.com/tags/navigation/pager/pager-taglib-2.0.war
- Deploy the sample web application (pager-taglib-2.0.war to your application server or web server. Deploying is nothing but placing the war file inside webapps folder (C:\apache-tomcat-6.0.18\webapps\) and restarting the server. It varies according to the server.
- In browser type http://localhost:8080/pager-taglib-2.0/
- You can see the demo and documentation link in the left hand side. It confirms that you have successfully executed the program. Click demo link to see the demo.
If you are a beginner it will be confusing to understand the sample program. So we have provided a simple program.
Save as index.jsp
Path (\webapps\PagerTags\index.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Pager Example - www.javaworkspace.com</title> </head> <body> <h1>PagerTag Example - <a href="http://www.javaworkspace.com" target="_blank">www.javaworkspace.com</a></h1> <form method="post" action="Controller"><input type="submit" value="Get Report"></form> </body> </html>
Save as Controller.java
Path
(\webapps\PagerTags\WEB-INF\classes\com\javaworkspace\pagertag\controller\Controller.java)
/*
* Controller.java
*/
package com.javaworkspace.pagertag.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.javaworkspace.pagertag.dto.StudentDetailsDTO;
/**
* @author www.javaworkspace.com
*
*/
public class Controller extends HttpServlet {
static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List list = new ArrayList();
HttpSession httpSession = request.getSession();
/*
* Hard-coded sample data. Normally this would come from a real data
* source such as a database
*/
list.add(new StudentDetailsDTO("Mark", "John", 25, "India"));
list.add(new StudentDetailsDTO("Harry", "Scott", 35, "England"));
list.add(new StudentDetailsDTO("Nathan", "Ridley", 12, "America"));
list.add(new StudentDetailsDTO("Tom", "Hanks", 55, "France"));
list.add(new StudentDetailsDTO("Roger", "Chris", 65, "Germany"));
list.add(new StudentDetailsDTO("Antony", "Jason", 45, "Denmark"));
httpSession.setAttribute("studentDetails", list);
RequestDispatcher dispatcher = request
.getRequestDispatcher("reportWithPagination.jsp");
dispatcher.forward(request, response);
}
}
Save asStudentDetailsDTO.java
Path
(\webapps\PagerTags\WEB-INF\classes\com\javaworkspace\pagertag\dto\StudentDetailsDTO.java)
/**
* StudentDetailsDTO.java
*/
package com.javaworkspace.pagertag.dto;
import java.io.Serializable;
/**
* @author www.javaworkspace.com
*
*/
public class StudentDetailsDTO implements Serializable {
private String studentName;
private String fatherName;
private int age;
private String country;
public StudentDetailsDTO(String studentName, String fatherName, int age,
String country) {
this.studentName = studentName;
this.fatherName = fatherName;
this.age = age;
this.country = country;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Save as reportWithPagination.jsp
Path
(\webapps\PagerTags\reportWithPagination.jsp)
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.List"%>
<%@page import="com.javaworkspace.pagertag.dto.StudentDetailsDTO"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pager Tag Tutorial - www.javaworkspace.com</title>
</head>
<body>
<%
String style = "simple";
String position = "top";
int maxPageItems = 2;
int maxIndexPages = 5;
List list = (List) session.getAttribute("studentDetails");
%>
<center><pg:pager items="<%= list.size()%>" index="center"
maxPageItems="<%= maxPageItems %>" maxIndexPages="<%= maxIndexPages %>"
isOffset="<%= true %>" export="offset,currentPageNumber=pageNumber"
scope="request">
<%-- keep track of preference --%>
<pg:param name="style" />
<pg:param name="position" />
<pg:param name="index" />
<pg:param name="maxPageItems" />
<pg:param name="maxIndexPages" />
<table width="90%" cellspacing="4" cellpadding="4" border="1">
<tr bgcolor="#FFCE00">
<td><strong>Student Name</strong></td>
<td><strong>Father Name</strong></td>
<td><strong>Age</strong></td>
<td><strong>Country</strong></td>
</tr>
<%
for (int i = offset.intValue(), l = Math.min(i
+ maxPageItems, list.size()); i < l; i++) {
%><pg:item>
<tr>
<%
StudentDetailsDTO studentDetailsDTO = (StudentDetailsDTO) list
.get(i);
out.println("<td>"
+ studentDetailsDTO.getStudentName()
+ "</td>");
out.println("<td>"
+ studentDetailsDTO.getFatherName()
+ "</td>");
out.println("<td>" + studentDetailsDTO.getAge()
+ "</td>");
out.println("<td>"
+ studentDetailsDTO.getCountry()
+ "</td>");
%>
</tr>
</pg:item>
<%
}
%>
</table>
<hr>
<pg:index>
<!--<jsp:include page="/WEB-INF/jsp/alltheweb.jsp" flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/altavista.jsp" flush="true" />-->
<jsp:include page="/WEB-INF/jsp/google.jsp" flush="true" />
<!--<jsp:include page="/WEB-INF/jsp/jsptags.jsp" flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/lycos.jsp" flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/texticon.jsp" flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/yahoo.jsp" flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/simple.jsp" flush="true" />-->
</pg:index>
</pg:pager></center>
</body>
</html>
Save as web.xml
Path (\WEB-INF\web.xml)
Click here to download the source code of javaworkspace sample programPager Tag index.jsp Pager Tag Example www.javaworkspace.com Controller Controller com.javaworkspace.pagertag.controller.Controller Controller /Controller
There are lots of pagination styles (google, altavista, alltheweb, lycos, yahoo...) available in WEB-INF\jsp\ folder. Choose any one of the paging style. In reportWithPagination.jsp uncomment any one of the style which you want. You can even delete the remaining styles. For some pagination styles (google) it requires internet connection (not mandatory). (Optional: You can download those images and paste in images folder and change the links which is pointing to google site to your images folder).
<!--<jsp:include
page="/WEB-INF/jsp/alltheweb.jsp" flush="true"
/>-->
<!--<jsp:include page="/WEB-INF/jsp/altavista.jsp"
flush="true" />-->
<jsp:include page="/WEB-INF/jsp/google.jsp"
flush="true" />
<!--<jsp:include page="/WEB-INF/jsp/jsptags.jsp"
flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/lycos.jsp"
flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/texticon.jsp"
flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/yahoo.jsp"
flush="true" />-->
<!--<jsp:include page="/WEB-INF/jsp/simple.jsp"
flush="true" />-->
Figure:1
- In some browsers the output may slightly vary.
- Change the maxPageItems and maxIndexPages with any number. By changing the maxPageItems you can determine the number of records per page.
- If you use older versions of tomcat then you need to add the below code in web.xml.
<taglib>
<taglib-uri> http://jsptags.com/tags/navigation/pager
</taglib-uri>
<taglib-location> /WEB-INF/jsp/pager-taglib.tld
</taglib-location>
</taglib>
(Inside <web-app>)
Further Reading:
Relate Links:
Create Executable File In Java
Generate Private Policy online
Display Tag Tutorial
Add Social Bookmark
Send E-mail through java program
Change Tomcat Port Number
