Pagination using JSP
Pagination in JSP
Custom JSP taglib for paging. This taglib takes some data and dynamically splits up this information into several pieces (pages)
as well as generates automatically an index for available pages. You have seen some like this many times in any search engine for example: a list of data plus some index
with links to next pages.
So body tag paging is a common wrapper. Body tag item describes a repeatable element (piece of data). Body tag index is a
wrapper for the generated index. For example, suppose you have to split file search.jsp :
<pg:paging>
<%
for (int i=1; i<=100; i++) {
%>
<pg:item>
your data …
</pg:item>
<%
}
%>
<pg:index>
<pg:page><%=thisPage%></pg:page>
</pg:index>
</pg:paging>
Just for example this page contains a loop that prints 100 times you data. Tag paging will print
the first 20 of them (default page size is 20) and the index that looks so:
where links are search.jsp?pageNum=2, search.jsp?pageNum=3 etc. (by default tag paging uses the same URL for index and default
name for page number parameter is pageNum). You can redefine all the elements we used here by default. For example, you can provide your own URL for
index pages (it is a parameter url for tag paging) as well as a new name for page number parameter (it is a parameter idPage):
…
</pg:paging>
and links for your index will be: /home/newPage.jsp?action=next&p=2, /home/newPage.jsp?action=next&p=3 etc.
also you can provide in this attribute a name for your JavaScript function that will be used in the index. This function must accept one parameter: page number.
…
</pg:paging>
and links for your index will be: yourFunction(2), yourFunction(3) etc.
Tag paging defines two page scope variables. The name for first variable will be described by the parameter idPage (default name is pageNum),
type is java.lang.Integer. You can use this variable as a reference to the current page number (within the tag’s body or outside it). The second variable is
lastPage (will be available outside of the tag, type is java.lang.Integer) describes the last page number.
Automatically generated index is also configurable. For example, you can replace a default title (Pages:):
…
</pg:index>
or
<pg:title>More:</pg:title>
…
</pg:index>
Body tag page lets you define the content for links in your index. It the above mentioned example it was just a
number of page. The hyperlink itself (<a href=…) will be generated automatically. Tag skips this generation for the current page
(page you are viewing right now).
You can restrict a number of pages in you index and add (Next,Prev) pair of links to the next (previous) portion of pages. Body tags prevPages and nextPages
describe content for such links. For example:
<pg:nextPages>[Next]</pg:nextPages>
<pg:prevPages>[Prev]</pg:prevPages>
…
</pg:index>
Body tags firstPage and lastPage describe content for the first (last) link.
Tag page describes a nested page scope variable you can use as a reference for the page number in the
generated link. The name for this variable is thisPage and type is java.lang.Integer.
Tags are:
paging
Body tag splits data and generates index. Parameters are:
1) idPage Optional parameter. Describes a name for the parameter with the current page number.
Default value is pageNum.
2) url Optional parameter. Describes the url or JavaScript function that will be used in the index.
By default tag will use the url of the current JSP page.
3) pageSize Optional parameter. Describes a size for pages (how many items per page).
Default value is 20.
4) indexSize Optional parameter. Describes a size for index.
By default is undefined.
item
Body tag describes an item (a repeatable element of data). Parameters are:
1) invokeAll Optional parameter. Possible values are
true or false. If this value is false tag will skip completely
all the items that are out of the scope for the current page. Otherwise items will be executed
always but output for out of the scope items will be suppressed.
Default value is true.
index
Body tag describes an index (links for pages). Parameters are:
1) title Optional parameter. Describes a title (caption) for index.
Default value is Pages:.
title
Body tag describes a title (caption) for index. Parameters are: none
page
Body tag describes how the link for the next page looks like. Parameters are: none
firstPage
Body tag describes how the link for the first index looks like. Parameters are: none
nextPages
Body tag describes how the link for the next portion of index looks like. Parameters are: none
prevPages
Body tag describes how the link for the previous portion of index looks like. Parameters are: none
lastPage
Body tag describes how the link for the last index looks like. Parameters are: none
for downloading:
Library: pagertag.jar Description: taglib.tld
Written by : bhaskar
( Treats Given : 8 Treats Attended : )





on August 17th, 2009 at 12:16 pm
Hi
I have used this paging in my website and it is really nice
thanks
on November 10th, 2009 at 6:14 pm
Hi This is really great i create a new paging using this site
thanks a lot for this code