Skip to main content

External Pagination & Sorting With Display Tag

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 e.g. Jquery's JQGrid etc. One such technology in JSP is to use the "displaytag" library.

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 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:
      
  displaytag
  displaytag
  1.1.1

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. Thanks!!

Comments

  1. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.
    Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    Core Java interview questions and answers
    Java training in Tambaram
    Java training in Velachery
    Java training in Omr
    Oracle training in Chennai

    I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.
    Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    Core Java interview questions and answers
    Java training in Tambaram
    Java training in Velachery
    Java training in Omr
    Oracle training in Chennai

    ReplyDelete
  2. Wonderful to visit your websites, This sites are fully filled with full of Information's. I perceived a lots of knowledge from this Articles.

    For more to learn about Python Visit below
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

    ReplyDelete
  3. Very nice informative blog. Actually it's great article. Thanks for sharing unique content. oracle training in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Use PostgreSQL Full Text Search With HQL

Full Text Search (FTS) is a great mechanism that can be used to search the database against a search term. FTS can produce miraculous results when used efficiently. The PostgreSQL database has inbuilt library for FTS and provides various functions to make use of it. It also has a special data type called "tsvector" to store the keywords(tokens) that take part in the searching process. We are not going to discuss about how Postgres handles FTS, but we will discuss about how to use this with Hibernate HQL(Hibernate Query Language). For more info about full text searching with postgres, you can visit This link. To use FTS with Hibernate, we will have to first create a POJO (say PostgreSQLFullTextSearchFunction ) which implements SQLFunction interface. We are going to use the custom Dialect java class (say CustomPostgresDialect ) in the SessionFactory configuration. In this custom dialect class, you will have to register a new SQL function by passing the object of PostgreSQLF...

Sort data with Streams in Java

Sorting refers to the mechanism of arranging/re-arranging something in a specific order. When it comes to java, we talk about placing the elements of an array or a list (Collection) in either Ascending or Descending order. There are multiple ways of achieving the desired order of elements in a collection, but here we will see how to make efficient use of Stream API which was introduced since JDK 8. As we already know, streams work with Lambda expressions and Functional Interfaces and provide some out of the box features which make developers' life easier. To sort a collection, streams have the inbuilt method sorted()    which has the following 2 method signatures: 1. Stream < T > sorted (); If we have to use this method, the elements of collection must be comparable i.e. the element object must implement Comparable interface. It will sort the collection according to the natural order of underlying object. 2. Stream < T > sorted ( Comparator <? super T > compa...