Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
AmeerAhmed




JSPWiki v2.2.33

[RSS]


Hide Menu

DisplayTagAndHibernatePagination


This is version 3. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


Display Tag and Hibernate Pagination - How to achieve pagination in display tag library using hibernate in 3 easy steps.

1) Add partialList="true" size="resultSize" to the display tag in your jsp file like so:


<display:table name="collection" cellspacing="0" cellpadding="0" requestURI="" 
    defaultsort="1" id="c" pagesize="25" partialList="true" size="resultSize" class="list" export="false">

2) Modify your action file by adding a method which gets the page number from the request


public final class DisplayTagAction extends BaseAction {

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    if (log.isDebugEnabled()) {
      log.debug("Entering 'execute' method");
    }

  TestDataManager amgr = (TestDataManagergetBean("testDataManager");

        //get the page number from request
        int page = getPage(request);
        int size = 25;
        
        //pass in page and size
        request.setAttribute("collection", amgr.getTestData(page, size);

        //get the total size of collection , required for display tag to get the pagination to work
  request.setAttribute("resultSize"new Integer(amgr.getTestDataSize()));

  return mapping.findForward("displayTagPage");

  }
  

    public int getPage(HttpServletRequest request){
      int page=0;
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String name = (String)paramNames.nextElement();
            if (name != null && name.startsWith("d-"&& name.endsWith("-p")) {
                String pageValue = request.getParameter(name);
                if (pageValue != null) {
                    page = Integer.parseInt(pageValue)-1;
                }
            }
        }
        return page;
    }

3) Modify the data access call to return a paginated list.


        public List getTestData(int page, int pageSize){
  
        Query query = getSession().createQuery("from Test");

        return query.setFirstResult(page * pageSize).setMaxResults(pageSize+1).list();
       
        }

  public int getTestDataSize() {

        return ((Integer)getSession().createQuery("select count(*) from Test").uniqueResult()).intValue();

        }

voila!



Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:57 MST by AmeerAhmed.