Friday, June 1, 2007

Display Tag and External Pagination with Spring and Hibernate

Display tag is a very handy tag for displaying data in a tabular format.
I had to design and implement external pagination making use of the provided PaginatedList interface. What does it take to make it work:

Display tag requires an object of type PaginatedList. PaginatedList is an Interface as provided by Display Tag. The Web page (a jsp in this case required an object of type PaginatedList).
So I defined an Interface IExtendedPaginatedList which extended PaginatedList and added a few more methods that were required.

Here is the implementation for IExtendedPaginatedList PaginatedListImpl.

One of the JSP page is paginatedList.jsp which was included in those other jsp pages which required pagination. This page was included using jsp:include page="/paginatedList.jsp"


paginatedList.jsp snippet:

<% IExtendedPaginatedList paginatedList = null; if(request.getAttribute("PAGINATED_LIST") != null){ paginatedList =(IExtendedPaginatedList)request.getAttribute("PAGINATED_LIST"); request.setAttribute("paginatedList", paginatedList ); } %>


The display tag was used in a different jsp page displayUserList.jsp:
<display:table name="paginatedList" sort="list"  requestURI="/searchUserAction.do?isPagination=yes">

     <display:column property="firstName" title="First Name"  sortable="false" headerClass="sortable"/>

<display:column property="lastName" title="Last Name" sortable="false" headerClass="sortable"/>
<display:column property="login" title="User Name" sortable="false" headerClass="sortable"/>

<display:column property="group.groupName" title="Group" sortable="false" headerClass="sortable"/>
<display:column property="location" title="Location" sortable="false" headerClass="sortable"/>

</display:table>



Both of these jsp pages are included within a main jsp page (for e.g searchUsers.jsp ).

The Controller or the Action class is responsible for creating this object and getting back the data from the Data Access Layer(DAO). I had developed this PaginatedDAO so that the rest of the team can make use of this DAO. Much of the responsibility lies with this DAO. This DAO class could be extended by other DAOs to make use of the pagination. Here is the code for the interface IPaginatedDAO and the implementation PaginatedDAO. This DAO at this point of time is using Spring framework and Hibernate and you can do it without Spring too. The PaginatedDAO has methods which provides different parameters and are for convenience for different usage. One method uses Hibernate Criteria and others accept a SQL Query string. In order for the pagination to work you need to execute two methods from this DAO. 1. getPaginatedListBy.....(.....) and 2. getTotalCountOfRowsBy....(...). Both of them are required. One can combine both as well.

In the action or controllers you will need this code to create an instance of IExtendedPaginatedList :



public IExtendedPaginatedList getPaginatedListFromRequest(HttpServletRequest request){

IExtendedPaginatedList paginatedList = new PaginatedListImpl();
String sortCriterion = request.getParameter(IExtendedPaginatedList.IRequestParameters.SORT);
paginatedList.setSortDirection(IExtendedPaginatedList.IRequestParameters.DESC.equals(request.getParameter(IExtendedPaginatedList.IRequestParameters.DIRECTION))? SortOrderEnum.DESCENDING : SortOrderEnum.ASCENDING);
paginatedList.setSortCriterion(sortCriterion);
int pageSize = 25; // Rows per page
paginatedList.setPageSize(pageSize);
String thePage = request.getParameter(IExtendedPaginatedList.IRequestParameters.PAGE);
if(thePage != null){
int index = paginatedList == null? 0 : Integer.parseInt(thePage) - 1;
paginatedList.setIndex(index);
}else{
paginatedList.setIndex(0);
}

return paginatedList;
}




This is all is required for pagination. It is easy and simple with Display Tag.


click here to get all the code:
References:
Display Tag
Display Tag Examples