When displaying long lists, one of the most common Web UI techniques is to show 10 or 15 results at a time, allowing the user to scroll through the list. In most cases, the lists can be quite large, and returning the large result set might prove impractical. You wouldn't want to load the long list in session and then loop through the results.
One way of approaching this task is to return the results from the database pertinent to the current request. The page developer shouldn't worry about the anchors at the bottom of the page; he should worry only about how he should display the current list for that request. The anchors for looping through a large list should be done through a pagination tag. Pagination means fetching and displaying the results one page at a time, and a pagination tag removes from the developer the burden of coding for navigation and anchors.
This article explains what a pagination tag can do, how to use it, and best practices for displaying long lists.
You can download the pagination tag I present in this article from Resources.
Let's take a scenario in Struts-speak. Once the user completes a search, the actual search results might be 1,000, for example. But from our middle tier, we don't return 1,000 results; instead we limit our query to return 10 or 15 results.
In MySQL, we can do that like this:
SELECT * FROM TABLE WHERE {CONDITION} LIMIT 0, 10.
Note: The above example shows the use of the LIMIT clause, which works in MySQL. The logic of limiting the query is left to the DAO (data access object) developer or the database
administrator.
We also calculate the total number of results found for that particular query. Once we get the first 10 results and the total,
we can set that to our ActionForm or to the request and then forward that to the JSP page (JavaServer Pages), which displays the 10 results.
To allow the user to navigate through the total list, we must build the logic that allows that functionality. Instead of laying that burden on the page developer, I have developed a pagination tag that displays the total number of pages, the current page the user is on, and the links for the next page and previous pages if required. If the user is on the first page, no Previous page will display. Similarly, if the user is on the last page, no Next page displays.
In a JSP page, we can put the tag to work like this:
<pagination-tag:pager start="<%=start%>" range="<%=range%>" results="<%=results%>"/>
For this tag library to work correctly, we must import the tag library at the top of the JSP page, which can be done like this:
<%@ taglib uri="http://paginationtag.miin.com" prefix="pagination-tag"%> or <%@ taglib uri="/WEB-INF/pagination.tld" prefix="pagination-tag"%> (provided you drop the tld in the /WEB-INF/)
The figure below shows the pagination tag at work.
Pagination tag at work. Click on thumbnail to view full-size image.
In the loaded page, we can see anchor tags for page numbers 1, 2, 3, 4, and so on. When the user clicks on 2, for example,
the start and range request parameters are sent to the search action with values 10 and 20, respectively. Now the search action executes the
same query, but with the new limiting values of 10 and 20. So, the new result set is returned to the JSP page, which displays
it. The pagination tag generates the new anchors and displays them.
| Subject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Pagination tagBy Anonymous on August 21, 2009, 4:15 amMuy buen artículo
Reply | Read entire comment
problem in dropping taglib.tld file into web-infBy Anonymous on February 13, 2009, 3:19 amwhenever i include this statement in my jsp page,its showing an error like failed to load or instantiate TagExtraInfo class:com.jsptags.navigation.pager.PagerTagExtraInfo I...
Reply | Read entire comment
pagination in netbeans By Anonymous on November 7, 2008, 4:08 amis it mandatory to specify in web.xml when ever we use in jsp?
Reply | Read entire comment
View all comments