通过分页查找方法: queryUsrsForPage(int pageSize, int page) 知道,要查找当页显示内容之需要2个参数,1是当页显示记录数, 2是当前页数,
@Override public int userNums() { List list = hibernateTemplate.find("select count(*) from Users"); int num = Integer.parseInt(list.get(0).toString()); return num; } @Override public PageBeanqueryUsersForPage(int pageSize, int page) { // 获取所以的记录数 int allRow = this.userNums(); // 获得总页数 int totalPage = PageBean.countTotalPage(pageSize, allRow); // 计算当前页 final int currentPage = PageBean.coutCurrentPage(page); // 当前要显示的起始数 final int offset = PageBean.countOffset(pageSize, currentPage); // 获取要显示的页面信息 offset要显示页面的第一个的所以 List list = this.getListForPage("from Users", offset, pageSize); // System.out.println(list); PageBean pagebean = new PageBean(); pagebean.setPagesize(pageSize); pagebean.setCurrentPage(currentPage); pagebean.setAllRow(allRow); pagebean.setTotalPage(totalPage); pagebean.setList(list); pagebean.init(); return pagebean; } public List getListForPage(final String hql, final int offset, final int length){ List list = hibernateTemplate.executeFind(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException,SQLException{ Query query = session.createQuery(hql); query.setFirstResult(offset); query.setMaxResults(length); List list = query.list(); return list; } }); return list; }
所以我们在跳转的时候只需要传入不同的page, 查询的第几页的page的参数。。
首先在form表单里面有隐藏域 :
记录当前的页数
点击 首页,上一页,下一页 ,尾页。。调用javascript代码
function jump() { var ye = document.getElementById("jumppp").value; var p = document.getElementById("page"); p.setAttribute("value" , ye); document.getElementById("form1").submit(); } function shouye() { var p = document.getElementById("page"); p.setAttribute("value" , 1); document.getElementById("form1").submit(); } function shangyiye() { var currentPage = document.getElementById("currentPage").value; var p = document.getElementById("page"); if(currentPage!=1) { p.setAttribute("value",--currentPage); } else { p.setAttribute("value",currentPage); } document.getElementById("form1").submit(); } function xiayiye() { var currentPage = document.getElementById("currentPage").value; var p = document.getElementById("page"); var totalPage = document.getElementById("totalPage").value; if(currentPage!=totalPage) { p.setAttribute("value",++currentPage ); } else { p.setAttribute("value",currentPage); } document.getElementById("form1").submit(); } function weiye() { var p = document.getElementById("page"); var totalPage = document.getElementById("totalPage").value; p.setAttribute("value",totalPage); document.getElementById("form1").submit(); } function submit(){ var p = document.getElementById("page"); p.setAttribute("value",1); document.getElementById("form1").submit(); }
版权声明:本文为博主原创文章,未经博主允许不得转载。