In display tag I used pagination feature, when I want to see the list of 15 rows but display tag fetches all the rows from database. Every time when i click on pagination nu
You have to use external pagination feature. First, specify in html tag that you're using external pagination. And create an object implements org.displaytag.pagination.PaginatedList. Finally, you have to implement the DAO which makes actually query for 15 rows only and returns PaginatedList.
1) Your jsp will look like this
...
Note that it specified the sort is external.
2) org.displaytag.pagination.PaginatedList implementation.
public class PaginatedListImpl implements PaginatedList{
private int fullListSize;
private int objectsPerPage;
private int pageNumber;
private String searchId;
private String sortCriterion;
private SortOrderEnum sortDirection;
private List list;
//getters and setters
...
}
3) DAO Implementation sample using hibernate. You can do it with JDBC or anything but make sure you're making the right query to get 15 rows in proper order and return PaginatedListImpl object.
@SuppressWarnings("unchecked")
public PaginatedListImpl getPaginatedList(PaginatedListImpl paginatedList) {
int pageNum = paginatedList.getPageNumber();
final int objectsPerPage = paginatedList.getObjectsPerPage();
final int firstResult = objectsPerPage * pageNum;
String sortOrderCriterion = pagiantedList.getSortOrderCriterion();
String sortOrder = paginatedList.getSortOrder
String className = type.getName().substring(type.getName().lastIndexOf(".") + 1);
final StringBuilder fromClause = new StringBuilder("from " + className + " " + alias);
String orderByClause = new StringBuilder(" order by ").append(sortCriterion).append(" ").append(sortDirection);
final String hql = new StringBuilder().append(fromClause).append(orderClause).toString();
List resultList = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(hql)
.setFirstResult(firstResult)
.setMaxResults(objectsPerPage)
.list();
}
});
Long count = (Long)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery("select count(*) " + fromClause).uniqueResult();
}
});
paginatedList.setFullListSize(count.intValue());
paginatedList.setList(resultList);
paginatedList.setPageNumber(pageNum+1);
paginatedList.setObjectsPerPage(objectsPerPage);
return paginatedList;
}