第一步:
先定义一个分页对象,也就是一个java bean
package com.wyf.common.web; /** * 分页类 * @author wgy * */ public class Page { /** * 分页下标 即当前页 默认为第1页 */ private int pageIndex = 1; /** * 分页总数 即总共分多少页 默认为1页 */ private int pageCount = 1; /** * 分页大小 即每页显示记录条数 默认为5条 */ private int pageSize = 5; /** * 总的记录条数 即一共有多少条数据 */ private int dataCount; /** * 排序列字段名称 */ private String orderByColumnName; /** * 排序方式 默认为 "ASC" 升序 */ private String orderByWay = "ASC"; /** * 排序方式常量 "DESC" 降序 */ public static final String PAGE_ORDER_BY_DESC = "DESC"; /** * 排序方式常量 "ASC" 升序 */ public static final String PAGE_ORDER_BY_ASC = "ASC"; public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getDataCount() { return dataCount; } public void setDataCount(int dataCount) { this.dataCount = dataCount; } public String getOrderByColumnName() { return orderByColumnName; } public void setOrderByColumnName(String orderByColumnName) { this.orderByColumnName = orderByColumnName; } public String getOrderByWay() { return orderByWay; } public void setOrderByWay(String orderByWay) { this.orderByWay = orderByWay; } }
第二步骤:
分页组件
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>分页页面</title> <script type="text/javascript"> function goToIndex(index){ var myQueryForm = document.getElementById("queryForm");//读取数据的action var pageIndex = $("#pageIndex"); var pageCountValue = $("#pageCount").val(); switch(index){ case 0: if(pageIndex.val() == 1){ alert("当前已经是第一页。"); return; }else{ pageIndex.val("1"); } break; case -1: if(pageIndex.val() == 1){ alert("当前已经是第一页。"); return; }else{ pageIndex.val(parseInt(pageIndex.val())-1); } break; case 1: if(pageIndex.val() == pageCountValue){ alert("当前已经是最后一页。"); return; }else{ pageIndex.val(parseInt(pageIndex.val())+1); } break; case 2: if(pageIndex.val() == pageCountValue){ alert("当前已经是最后一页。"); return; }else{ pageIndex.val(pageCountValue); } } myQueryForm.submit(); } </script> </head> <body> <table align="center" width="100%"> <tr align="center"> <td width="30%">共 <s:property value="#request.pageBean.dataCount" /> 条数据</td> <td width="50"> <a href="#" onclick="goToIndex(0);return false;">首页</a> </td> <td width="80"> <a href="#" onclick="goToIndex(-1);return false;">上一页</a> </td> <td width="80"> <a href="#" onclick="goToIndex(1);return false;">下一页</a> </td> <td width="50"> <a href="#" onclick="goToIndex(2);return false;">尾页</a> </td> <td width="30%">第 <s:property value="#request.pageBean.pageIndex" /> 页/共 <s:property value="#request.pageBean.pageCount" /> 页</td> </tr> </table> </body> </html>
页面提交的时候传入2个隐藏域
<input type="hidden" id="pageIndex" name="pageBean.pageIndex" value="<s:property value="pageBean.pageIndex"/>"/> <input type="hidden" id="pageCount" name="pageBean.pageCount" value="<s:property value="pageBean.pageCount"/>"/>
后台分页算法:
public List<T> queryDataWithPageBeanAndHql(Page pageBean,String hql){ Session session = this.getSession(); Query queryPage = session.createQuery(hql); List<T> list = null; if(pageBean == null){ list = queryPage.list(); }else{ Query query = session.createQuery("select count(*) " + hql.substring(hql.indexOf("from"))); //总的记录条数 int count = ((Long)query.list().get(0)).intValue(); //保存总的记录条数到PageBean中 pageBean.setDataCount(count); pageBean.setPageCount((count-1)/pageBean.getPageSize() + 1); queryPage.setFirstResult((pageBean.getPageIndex()-1)*pageBean.getPageSize()); queryPage.setMaxResults(pageBean.getPageSize()); list=queryPage.list(); } session.close(); return list; }