Pagination & sorting in jsp’s with the displaytag taglib & spring
I hate writing pagination code. Infact I hate it so much, I try not to paginate my lists at all. ( I know, i know, very bad on my part)
. Writing pagination code totally destroys the code reuse in an application, developers generally just copy paste the very same pagination code all over the place over and over again. Not to mention decorating such pagination displays is a pain in the ass as well. So while searching for a more concrete solution to my problem I stumbled upon displaytag.
Display tag is by far the most comprehensive pagination solution that I have ever come across. Not only is the library extremely stable, it’s rich of features and customizations. I haven’t yet run across a scenario where display tag didn’t fulfill my needs. And you don’t need to do anything extra to get it working in your webapp. I’m going to develop a small example which will show a list of 20 user’s.
Because of my love for spring, i’ll use a Spring controller to pass data onto my jsp. You don’t really NEED to use spring with displaytag per se since displaytag can pick up the list to display from anywhere, session, context, page and you can set this list anywhere as well.
My controller first :
package com.codercorp.displaytag;
//imports
@Controller
@RequestMapping("/displaytag.html")
public class DisplaytagController {
private static List<User> userList;
static {
userList = new ArrayList<User>(20);
userList.add(new User(1, "Gaurav Arora", "gaurav@myemail.com", "+91-98119-09880"));
userList.add(new User(2, "Bernard Shaw", "bernard@myemail.com", "+91-92119-09880"));
userList.add(new User(3, "Aurora Aurealis", "aurora@myemail.com", "+91-93119-09880"));
userList.add(new User(4, "James Lock", "james@myemail.com", "+91-98114-09880"));
userList.add(new User(5, "Karl love", "karl@myemail.com", "+91-98169-09880"));
userList.add(new User(6, "George jungle", "george@myemail.com", "+91-98819-09880"));
userList.add(new User(7, "Switch blade", "switch@myemail.com", "+91-98118-09880"));
userList.add(new User(8, "Gold flake", "gold@myemail.com", "+91-98119-09380"));
userList.add(new User(9, "Susan Anthony", "susan@myemail.com", "+91-98113-09880"));
userList.add(new User(10, "Arm Guard", "armg@myemail.com", "+91-98119-02880"));
userList.add(new User(11, "Raju Gentleman", "raju@myemail.com", "+91-98319-09880"));
userList.add(new User(12, "Rajat Ghandhi", "rajat@myemail.com", "+91-98519-09880"));
userList.add(new User(13, "Niketa Shyam", "niketa@myemail.com", "+91-98719-09880"));
userList.add(new User(14, "G. Mahesh", "mahesh@myemail.com", "+91-98118-09880"));
userList.add(new User(15, "Yogvinder Singh", "yogi@myemail.com", "+91-99119-09880"));
userList.add(new User(16, "Eklavya", "eklavya@myemail.com", "+91-98119-00880"));
userList.add(new User(17, "Asus shyam", "asus@myemail.com", "+91-98119-09680"));
userList.add(new User(18, "Jack donneley", "jack@myemail.com", "+91-98119-01880"));
userList.add(new User(19, "Donald McDonald", "donald@myemail.com", "+91-98119-03880"));
userList.add(new User(20, "Azure Aloha", "azure@myemail.com", "+91-98119-09890"));
}
@RequestMapping(method = RequestMethod.GET)
public String showPage(HttpServletRequest request, HttpSession session) {
Map paramMap = WebUtils.getParametersStartingWith(request, "d-");
if (paramMap.size() == 0) {
WebUtils.setSessionAttribute(request, "userList", userList);
}
return "displaytag";
}
}
The static part populates the userList with dummy User objects. Line #36 is used to decide if the list should be set in the session or not. displaytag sends it’s pagination and sorting data using parameters that start with “d-”. If any such params exist in our request we ignore setting the list in the session because it is already set. However, on a new request, when no params are set, the list is put into the session again which is ideal for us.
Now the display:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="display" uri="http://displaytag.sf.net"%> <body> <head> <link rel="stylesheet" href="css/screen.css" type="text/css" /> </head> <display:table uid="user" name="sessionScope.userList" defaultsort="1" defaultorder="ascending" pagesize="10"> <display:column property="id" sortable="true" title="Employee ID" maxLength="25" /> <display:column property="name" sortable="true" title="Real Name" maxLength="25" /> <display:column property="emailAddress" sortable="true" title="Email Address" maxLength="25" /> <display:column property="phone" sortable="true" title="Phone" maxLength="25" /> <display:setProperty name="basic.empty.showtable" value="true" /> <display:setProperty name="paging.banner.group_size" value="10" /> <display:setProperty name="paging.banner.item_name" value="user" /> <display:setProperty name="paging.banner.item_names" value="users" /> <display:setProperty name="paging.banner.onepage" value="<span class="pagelinks"> </span>" /> </display:table> </body>
Running your webapp and navigating to displaytag.html should now display your table like this :

Try sorting your list based on the name of the person. You will notice that only the entries that are on the page in view and sorted, not the entire list. This is not ideal since the entire list should be sorted. A lot of people run into this problem of only one page sorting with displaytag. To make this work you need to add a file with the name displaytag.properties to your classpath. displaytag will automagically pick up settings specified in this file. My displaytag.properties looks something like this :
sort.amount = list basic.empty.showtable = true paging.banner.placement = top
The sort.amount tells displaytag to sort the entire list and not just the current page when sorting lists. The basic.empty.showtable settings tells display tag to show a table even if it contains no data. paging.banner.placement tells displaytag to always place the pagination banner on top of the table display.
Remember, it doesn’t matter where your list of users comes from, displaytag picks it up from the session and shows it. You don’t need spring or anything else for displaytag to work. I used spring because I just find it really easy to make examples using it.
No related posts.

This article was helpful to me doing pagination and sorting in display tag.Thanks
Hi All,
Please some one can mail me this code. I need to submit my work and need pagination ASAP. Please help me in sending this full code sample.
My job depends on this work
Please send me mail to this :prasanth_2955@hotmail.com
Thanks alot and thanks to display tags
very informative article written, so easy to implement.
Just to inform that you forgot to include property “requestURI” with empty string.
To be like this
Rgds
<display:table uid=”user” name=”sessionScope.userList” defaultsort=”1″
# defaultorder=”ascending” pagesize=”10″ requestURI=”" >
<!–
–>