免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1311 | 回复: 0
打印 上一主题 下一主题

Hibernate和displayTag 完美分页3步曲 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-03 18:21 |只看该作者 |倒序浏览

               
               
               
               
               
displaytag 中的分页对于数据量不大来说是很好的,只需要简单的配置就可以做出来强大的功能,但是要注意的是分页链接的编码问题。我们知道的,这样的分页模式在数据量大时,对于性能来说是个问题,毕竟将大量的数据一次性取出来,而用到的只有少数,不是很好的选择,而Hibernate强大的分页功能,对于我们来说再简单不过了,但是却不知道如何2者结合起来使用?
下面就是一个很好的例子:只需要3部。
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 = (TestDataManager) getBean("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();
   }
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/63956/showart_725094.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP