注册 登录
编程论坛 J2EE论坛

ssh1分页(hibernate分页)

a123213813 发布于 2010-12-22 11:47, 912 次点击
最近做个web项目,遇到点难题,网上贴子贴的代码都不全,希望大侠能指点指点。 最好给个全面的分页实例。 hibernate用的Query的接口查询。

W.s-yangtao@
3 回复
#2
cdxkyz9282010-12-22 17:14
看不懂啊 哎 什么时候才能看懂啊~!
#3
mary_xiaoman2010-12-23 10:04
给你一个分页类,直接在action里调用就可以了,代码给你贴出来!
----------------------------------------------------------------------------------
分页类:
public class MyPageList {

    private PagedListHolder pagedlist;

    public static final int PAGESIZE = 15; // 每页显示几条记录

    public MyPageList(List list) {
        this.pagedlist = new PagedListHolder(list);
    }

    public List getInstance() {
        pagedlist.setPageSize(PAGESIZE);
        return pagedlist.getPageList();
    }

    public List getInstance(int PAGESIZE) {
        pagedlist.setPageSize(PAGESIZE);
        return pagedlist.getPageList();
    }

    public List next() {//下一页
        pagedlist.nextPage();
        return pagedlist.getPageList();
    }

    public List prep() {//上一页
        pagedlist.previousPage();
        return pagedlist.getPageList();
    }
   
    public List first() {//首页
        for (int i = 0; i < pageCount(); i++) {
            if (pagedlist.isFirstPage()) {
                break;
            }
            pagedlist.previousPage();
        }
        return pagedlist.getPageList();
    }

    public List last() {//最后一页
        for (int i = 0; i < pageCount(); i++) {
            if (pagedlist.isLastPage()) {
                break;
            }
            pagedlist.nextPage();
        }
        return pagedlist.getPageList();
    }

    public int pageCount() {//总共多少页
        return pagedlist.getPageCount();
    }

    public int pageNumber() {//第几页
        return pagedlist.getPage() + 1;
    }

}
----------------------------------------------------------------------------------
action中的代码:

    MyPageList myPageList = null;

    public ActionForward doPage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
    {
        String op=request.getParameter("op");
        
        List list=new ArrayList();
        if(op != null)
        {
            if(op.equals("prep"))
            {
                list=myPageList.prep();
            }
            else if(op.equals("next"))
            {
                list=myPageList.next();
            }
            else if(op.equals("first"))
            {
                list=myPageList.first();
            }
            else if(op.equals("last"))
            {
                list=myPageList.last();
            }
        }

        request.getSession().setAttribute("warnList", list);
        request.getSession().setAttribute("pageNumber", myPageList.pageNumber());
        
        return mapping.findForward("warnInfo");
    }

----------------------------------------------------------------------------------
                                            页面中的代码:

                                                        <td>
                                                            <a href="warn.do?method=doPage&op=first"> 第一页 </a>
                                                        </td>
                                                        <td>
                                                            <a href="warn.do?method=doPage&op=prep"> 上一页 </a>
                                                        </td>
                                                        <td>
                                                            <a href="warn.do?method=doPage&op=next">下一页 </a>
                                                        </td>
                                                        <td>
                                                            <a
                                                                href="warn.do?method=doPage&op=last"> 最后页 </a>
                                                        </td>
#4
努力加油2010-12-27 08:33
  精彩
1