Why display tag pagination doesn't use partial list?
Asked Answered
M

3

7

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 number, it fetch all of the rows from db. Because of that it slow the performance of application.

I want that in display tag when i want to see the 15 rows then display tag also fetch 15 rows from db not entire db rows. Please help me if some one knows.

Marv answered 8/10, 2010 at 7:5 Comment(0)
O
13

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

<display:table name="command" sort="external" partialList="true" size="${command.fullListSize}" pagesize="${command.objectsPerPage}">
  <display:column property="name" title="name"/>
  ...
</display:table>

Note that it specified the sort is external.

2) org.displaytag.pagination.PaginatedList implementation.

public class PaginatedListImpl<T> implements PaginatedList{
  private int fullListSize;
  private int objectsPerPage;
  private int pageNumber;
  private String searchId;
  private String sortCriterion;
  private SortOrderEnum sortDirection;
  private List<T> 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<T> 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<T> 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;
}
Orlon answered 10/10, 2010 at 10:3 Comment(0)
T
3

Displaytag sends it’s pagination and sorting data using parameters that start with “d-”. So quick solution can be to check if request contains something starting with "d-",if yes then dont execute your query again.

Trail answered 31/3, 2011 at 14:7 Comment(0)
F
0

For some reason using the PaginatedList didn't seem to work. I tried with the latest version and it seems there is some bug and it gives the error attribute size must be specified even if I hard code the size value. I went through other links/blogs and many people have pointed this out.

The option which I finally used is to store the data in the session. When the user clicks next it checks the data in session. If present, it will not hit the services layer. So far it has worked fine.

Felid answered 18/9, 2012 at 20:24 Comment(2)
It sounds like you're the original poster - perhaps you registered now. If so, could you clarify. If not, please edit to say "doesn't seem to work" instead of "didn't seem to work".Eagan
It could be that your result list is null.Cottage

© 2022 - 2024 — McMap. All rights reserved.