This JSP Taglib provides some common utility features.
JDK 8 is required at a minimum.
This project is available as a Maven artifact. Maven users can include it with the following dependency in pom.xml:
<dependency>
<groupId>com.lt-peacock</groupId>
<artifactId>lp-taglib</artifactId>
<version>1.0.1</version>
</dependency>
Then add the taglib declaration at the top of any JSP file and all the tags in this taglib will be available.
<%@ taglib prefix="lp" uri="http://lt-peacock.com/tags"%>
When displaying a large amount of data, it is often convenient to split it into pages. This taglib provides a pagination
tag for generating a list of page numbers to facilitate user navigation.
<lp:pagination currentPage="7" firstPage="1" lastPage="20" basePageLink="/page"/>
The element will be replaced by an unordered list (<ul>
) element. Each page will be a list item (<li>
) with an anchor (<a>
) inside. Each anchor points to the configured base page link with slash (/
) and the page number appended. There is no CSS applied by default: it is up to the user to style the classes on the generated elements. The classes used for certain generated elements can be configured by setting some attributes.
All dynamic expressions (Java expressions, EL expressions, etc) are supported as attribute values.
Attributes are specified on the lp:pagination
tag with the syntax attributeName="attributeValue"
.
Attribute Name | Description | Default Value |
---|---|---|
currentPage | The current page number. | None; this attribute is required. |
firstPage | The first page number. This page number is not necessarily displayed. | 1 |
lastPage | The last page number. This page number is not necessarily displayed. | None; this attribute is required. |
maxPages | The maximum number of pages to display in the list. | 10 |
pageGenerator | The com.ltpeacock.taglib.pagination.PageGenerator object to use for generating pages. |
com.ltpeacock.taglib.pagination.DefaultPageGenerator |
pageLinkGenerator | The com.ltpeacock.taglib.pagination.PageLinkGenerator object to use for generating page links. |
com.ltpeacock.taglib.pagination.DefaultPageLinkGenerator |
generatePrevLink | Whether or not to generate a link (at the start of the list of pages) pointing to the previous page. The value should be a boolean (either true or false ). |
true |
generateNextLink | Whether or not to generate a link (at the end of the list of pages) pointing to the next page. The value should be a boolean (either true or false ). |
true |
generateFirstLink | Whether or not to generate a link (at the start of the list of pages) pointing to the first page. The value should be a boolean (either true or false ). |
true |
generateLastLink | Whether or not to generate a link (at the end of the list of pages) pointing to the last page. The value should be a boolean (either true or false ). |
true |
generateDisabledLinks | Whether or not to generate disabled first, last, previous, and next links. The first and previous links would be disabled when the current page is the first page; the last and next links would be disabled when the current page is the last page. The value should be a boolean (either true or false ). |
false |
currentClass | The CSS class to add to the list item for the current page (in addition to the page class). | "current" |
pageClass | The CSS class to add to the list item elements for all pages. | "page" |
pageLinkClass | The CSS class to add to the anchor in the list item for each page. | "page-link" |
rootClass | The CSS class to add to the unordered list element that the lp:pagination tag is replaced with. |
"pagination" |
prevClass | The CSS class to add to the list item for the previous page link. | "prev-page" |
nextClass | The CSS class to add to the list item for the next page link. | "next-page" |
firstClass | The CSS class to add to the list item for the first page link. | "first-page" |
lastClass | The CSS class to add to the list item for the last page link. | "last-page" |
disabledClass | The CSS class to add to all disabled list items. | "disabled" |
prevText | The content of the previous page link. | "« Prev" |
nextText | The content of the next page link. | "Next »" |
firstText | The content of the first page link. | "First" |
lastText | The content of the last page link. | "Last" |
basePageLink | The base page link for pages. The href for each page link will be the base page link appended with a slash and the page number. (If the specified value ends in a slash, it will first be removed.) |
"javascript:;" |
This example uses Bootstrap 4 to style the pagination element. For this integration, the pageClass
and currentClass
attributes need to be set to match the Bootstrap classes. (The default rootClass
of "pagination"
and pageLinkClass
of "page-link"
already match.)
<%@ taglib prefix="lp" uri="http://lt-peacock.com/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Pagination Example</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<!-- Content of current page here ... -->
<c:url value="/page" var="pageLink"/>
<!-- Pagination element -->
<lp:pagination currentPage="7" firstPage="1" lastPage="20" maxPages="10"
basePageLink="${pageLink}" rootClass="pagination" currentClass="active"
pageClass="page-item" generateDisabledLinks="true"/>
</div>
</body>
</html>
Result:
The generated HTML (prettified):
<ul class='pagination'>
<li class='page-item first-page'><a class='page-link'
href='/page/1'>First</a></li>
<li class='page-item prev-page'><a class='page-link'
href='/page/6'>« Prev</a></li>
<li class='page-item'><a class='page-link' href='/page/2'>2</a></li>
<li class='page-item'><a class='page-link' href='/page/3'>3</a></li>
<li class='page-item'><a class='page-link' href='/page/4'>4</a></li>
<li class='page-item'><a class='page-link' href='/page/5'>5</a></li>
<li class='page-item'><a class='page-link' href='/page/6'>6</a></li>
<li class='page-item active'><a class='page-link' href='/page/7'>7</a></li>
<li class='page-item'><a class='page-link' href='/page/8'>8</a></li>
<li class='page-item'><a class='page-link' href='/page/9'>9</a></li>
<li class='page-item'><a class='page-link' href='/page/10'>10</a></li>
<li class='page-item'><a class='page-link' href='/page/11'>11</a></li>
<li class='page-item next-page'><a class='page-link'
href='/page/8'>Next »</a></li>
<li class='page-item last-page'><a class='page-link'
href='/page/20'>Last</a></li>
</ul>