External Pagination & Sorting With DisplayTag

3 Comments

We often come across the need to display data of any web application in a tabular form to the end user. There are several ways to do this using different kind of technologies for e.g. Jquery’s JQGrid etc. One such technology in JSP is to use the DisplayTag library. External pagination is when the data is fetched on-demand by requesting the server in batches, monitored by page size.

The display tag allows the programmer to provide the end user very basic but important features of:
1. Sorting the contents of the displayed list based on the sort order(Ascending/Descending) and sort property (First Name/Last Name etc.).
2. Navigating to other records if the list is quite large (Pagination).
3. Exporting the displayed data in any of the file formats like PDF, Excel etc.
4. Customizing the design (look & feel) of the displayed table via a properties file or inline properties.

By default the display table is configured to use the internal Pagination and Sorting. I am going to describe that how we can use the External pagination and/or sorting with display table in this post. Assuming that we have a Spring-Hibernate configured application using Maven build tool, follow these steps:

1. Add the following dependency to your maven project:

<groupId>displaytag</groupId>
<artifactId>displaytag</artifactId>
<version>1.1.1</version>
2. Now in the JSP file, add the following code snippet:
<%@taglib prefix="display" uri="http://displaytag.sf.net" %>
${topic.name}

Let’s look at the required attributes of display:table tag for external paging and sorting:
a. partiallist: The value must be true in case we want external pagination.
b. size: The total size(count) of the records must be given to the table so as to manage pages.
c. sort: Must be set to “external” to use external sorting(sorting the records while fetching from database itself depending on column).
d. pagesize: Should be provided so as to tell the Query that how many records should be fetched for one page.

Also if we need a column to be sortable(externally), the attribute sortname should be provided. The value for this attribute is the property name of the Entity class which we are iterating through in the table. For e.g in this case, we are iterating the Topic entity objects and printing the topic name on the page. So, we are providing the property “name” of Topic entity class for sortname attribute of display:column tag.

3. The next step is to receive the paging and sorting request parameters in the spring controller(Can be Servlet,Struts etc.) so that we can pass them to the DAO layer. It can be done as:

String sortField = request.getParameter((new ParamEncoder("topic").encodeParameterName(TableTagParameters.PARAMETER_SORT)));

String sortOrder = request.getParameter((new ParamEncoder("topic").encodeParameterName(TableTagParameters.PARAMETER_ORDER)));

  if(!StringUtils.isEmpty(sortOrder)) {
   int order = Integer.parseInt(sortOrder);
   switch(order) {
    case 1: 
     sortOrder = "asc";
     break;
    case 2: 
     sortOrder = "desc";
     break;
    default: 
     sortOrder = "asc";
   }
  }

  int start = 0;
  String page = request.getParameter((new ParamEncoder("topic").encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
  if(page != null)
   start = (Integer.parseInt(page) - 1) * pageSize;

Where sortField will be the property name to be sorted. sortOrder would be the order of sorting i.e. Ascending or Descending and finally start will hold the offset value(first record to be fetched from database). You can either put the pageSize in an Enum, in a properties file or you can directly pass it in the request itself. But, remember that the pageSize value in the controller should match with that we have declared in the display table on the jsp page. Here, topic is the display table id attribute that we declared on jsp page.

4. Finally in the DAO class, assuming that we are using the hibernate criteria, we can do something as below:

criteria.setFirstResult(start);
criteria.setMaxResults(pageSize);
if(sortOrder.equals("asc")) {
   criteria.addOrder(Order.asc(sortField));
} else {
   criteria.addOrder(Order.desc(sortField));
}

That’s all. You are ready to go with external pagination and sorting with display table. Hope you found this post useful. Please do leave your comments and suggestions to make this blog more precise and useful.

Happy Reading!!

3 Replies to “External Pagination & Sorting With DisplayTag”

  1. Using display tables with external pagination and sorting is a great way to handle large datasets efficiently. It ensures that only the necessary data is fetched from the server, improving performance. The integration with Spring-Hibernate and Maven makes the setup seamless and robust. This approach is beneficial for web applications that require dynamic data management. Have you tried using DisplayTag with other frameworks apart from Spring-Hibernate?

  2. This is a very informative post about using external pagination and sorting with the DisplayTag library in JSP. I appreciate the clear steps provided for integrating it with a Spring-Hibernate application using Maven. It’s great to see how this approach can optimize data fetching and improve performance. I’m curious, though, how does this method compare to using other libraries like JQGrid in terms of ease of implementation and flexibility? Also, are there any specific scenarios where internal pagination might still be preferable over external pagination? I’d love to hear more about your experiences or any potential challenges you’ve faced with this setup. What are your thoughts on the scalability of this approach for larger datasets?

  3. This post provides a clear and practical guide on implementing external pagination and sorting with the DisplayTag library in a Spring-Hibernate setup. It’s great to see a step-by-step approach, making it easier for developers to follow along. I wonder if there are any potential performance considerations when using external pagination compared to internal pagination. Have you encountered any specific challenges while implementing this in a real-world application? It would be interesting to hear about any additional tips or best practices to optimize this process. Do you think this approach would scale well with large datasets? Overall, this is a useful resource, and I appreciate the effort put into explaining the setup. Would love to hear more about your experiences or any additional insights you might have!

Leave a Reply

Your email address will not be published. Required fields are marked *