View RSS Feed

My Java Tips

Using pagging techniques

Rate this Entry
by , 10-30-2011 at 05:59 PM (2087 Views)
When displaying long lists on the web, we use paging techniques. In this post, I will show how to do that.

Normally we display 10 or 15 results at a time and the user can scroll through the list. If list is a long one, then it does not make an sense to loat it completely. Loading the complete list in session and then loop through the results is not a nice approach. An approach can be to use pagination tags which means fetching and displaying the results one page at a time. Pagination tag removes from the developer the burden of coding for navigation and anchors.

Let me present an example: We do some search and the result count is over 1000. 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:


To make like easy for the JSP developer, a pagination tag can be used 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. This in indeed trival stuff.

In a JSP page, we can put the tag to work like this:

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="" prefix="pagination-tag"%> or
<%@ taglib uri="/WEB-INF/pagination.tld" prefix="pagination-tag"%> (provided you drop the tld in the /WEB-INF/)

The idea is that one use clicks a page no, 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.

Now I will present pagination tag parameters. These parameters are runtime expressions:

* start: This required parameter defines the starting position in the list (e.g., 10, 20, and so on).
* range: Defines how many results to display at a time, for example, 10; also a required parameter.
* results: Defines the total number of results that would be returned if we hadn't limited our query. The pagination tag uses the results parameter (which is required) to display the number of pages.

I would like you to go through the first 3 parts on this topic to develop good understanding of this topic.

- styleClass tag returns the anchors in an HTML table so you can plug that table within another table or anywhere on the page. styleClass, which is an optional parameter, defines the style for that table. If not specified, a default style is used.

- action tag defines an action parameter to build the anchor tags. The action parameter can be used if the search action doing the extraction redirects (instead of forwards) to a JSP page that displays the results. The pagination tag, by default, tries to figure out the URL for the anchors from the current URI. If the JSP page that displays the results using the pagination tag is reached by redirect from the search action, the pagination tag adds the JSP page's URL to the navigation anchors, instead of the search action's URL. So, in the case of redirects, if you define an action parameter, it will be used to generate the anchor's URL. action is an optional parameter.

- title defines whether to show a title for the anchors on a mouse over. The options are yes or no. Anything other than yes is treated as no. If we choose yes, then the title's output by the pagination tag appears like this: First page, Previous page, Next page, and Go to page 5, and so on. title is an optional parameter.

We set the start and range parameters for the pagination tag. We could have made the tag read from the request but it is not done because the search action could be redirecting to the JSP page instead of forwarding. The pagination tag preserves all the request parameters.

If you are using the Struts framework, then design the search action in such a way that you would read the start and range parameters from the request and pass them to the DAO, which executes a limiting query equivalent to the one above and returns the relevant results for the start and range. Example snippet is given below:

// NumberUtils is part of Jakarata-commons-lang. Please download Jakarta commons-lang.
Java Code:
public class PaginationAction extends Action {
    public static final String SUCCESS = "success";
      public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {
        PageDAO pageDAO = DAOFactory.getDAOFactory(Factory.MySQL).getPageDAO();
        int start = NumberUtils.stringToInt(((PaginationForm) form).getStart(), 0);
        int range = NumberUtils.stringToInt(((PaginationForm) form).getRange(), 10);

        ((PaginationForm) form).setList(pageDAO.getList(start, range)); // Setting the list
        ((PaginationForm) form).setResults(    //Setting the total number of results
        return mapping.findForward(SUCCESS);
Now lets talk about the JSP page. We will display the list of results, and then the start, range, and results parameters are supplied to the pagination tag, which displays the total number of pages, the current page the user is on, and then links for navigating to the next page, previous page, and so on:

// this refers to total number of results

Now the DAO can be replaced by a session bean acting as a facade, which in turn delegates the work to an entity bean.

If you are using Oracle, you can achieve LIMIT clause functionality in MySQL like this:


Hope this was useful.

Submit "Using pagging techniques" to Facebook Submit "Using pagging techniques" to Digg Submit "Using pagging techniques" to Submit "Using pagging techniques" to StumbleUpon Submit "Using pagging techniques" to Google

Tags: None Add / Edit Tags
Java EE , JSF , JSP