使用此分页插件很简单就可以实现多种分页风格!其官方网址为:pager taglib 网址在此下载要用的jar包及实例。现在开始分页:
2.创建两个工具类:PageMode.java(此类用来生成便利需要的集合,及其总的数据行数) MakePageModel.java
package com.util.pageUtil;
import java.util.List;
/**
* @author ZhangJunpeng
* @declare:此类用来生成便利需要的集合,及其总的数据行数
**/
public class PageMode {
private int total;
private List datas;
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
------------------------------------------------
package com.util.pageUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.school.dao.BaseHibernateDAO;
//import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;
/**
*@method:MakePageModel
*@author ZhangJunpeng
**/
public class MakePageModel{
/**
*@param offset 当前页数
*@param pagesize 每页显示的行数
*@param objName 传入要传入对象的名字
*@return pageModel pm
**/
private BaseHibernateDAO baseHibernateDAO=new BaseHibernateDAO();
public BaseHibernateDAO getBaseHibernateDAO() {
return baseHibernateDAO;
}
public void setBaseHibernateDAO(BaseHibernateDAO baseHibernateDAO) {
this.baseHibernateDAO = baseHibernateDAO;
}
@SuppressWarnings("unchecked")
public PageMode makePageModel(int offset, int pagesize,String objName) {
String queryCountHql = "select count(*) from "+objName;
List datas=new ArrayList();
int totoalPages=0;
Session session=baseHibernateDAO.getSession();
Transaction transaction=null;
try{
transaction=session.beginTransaction();
Query query = session.createQuery(queryCountHql);
totoalPages = ((Integer)query.uniqueResult()).intValue();
System.out.println(">>>1");
datas = session .createQuery("from "+objName)
.setFirstResult(offset)
.setMaxResults(pagesize)
.list();
System.out.println(">>>2");
transaction.commit();
System.out.println(">>>3"+totoalPages);
}catch (Exception e) {
e.printStackTrace();
transaction.rollback();// TODO: handle exception
}finally{
// session.close();
}
PageMode pm = new PageMode();
pm.setTotal(totoalPages);
pm.setDatas(datas);
return pm;
}
}
3.创建相应的DAO类,实现pageMode的查询(就是返回指定首尾的结果集,及其总的数据数)例如:
/**
* 用于分页显示的查询
* */
public PageMode findAll(int offset, int pagesize){
String queryCountHql = "select count(*) from Admin";
Query query = getSession().createQuery(queryCountHql);
int total = ((Number)query.uniqueResult()).intValue();
List datas = getSession().createQuery("from Admin")
.setFirstResult(offset)
.setMaxResults(pagesize)
.list();
PageMode pm = new PageMode();
pm.setTotal(total);
pm.setDatas(datas);
return pm;
}如果是struts,或是struts2的话,在action内需要处理如下数据:(STRUTS2示例)
int offset = 0;
AdminDAO adminDAO = new AdminDAO();//需要查询类的DAO
HttpServletRequest request = ServletActionContext.getRequest();
try {
if (request.getParameter("pager.offset") == null) {//pager.offset,taglib标签自带的属性
offset = 0;
} else {
offset = Integer.parseInt(request
.getParameter("pager.offset"));
}
int maxPageItems;//每页显示行数
if(request.getAttribute("maxPageItems")==null){
maxPageItems=10;
}else{
maxPageItems = Integer.parseInt(request.getParameter("maxPageItems"));
}
logger.debug(">>>>"+maxPageItems);
PageMode pm = adminDAO.findAll(offset, maxPageItems);//查询PageMode
request.setAttribute("admins", pm); //设置范围if (logger.isDebugEnabled()) {
logger.debug("viewAdmin() - end");
}
return "viewAdmin";//转入视图层
} catch (Exception e) {
logger.error("viewAdmin()", e);logger.error(e.getMessage());
return ERROR;
}
4.根据返回的pageMode可以保存在指定的范围内,然后逃到页面进行显示数据:
在需要显示分页的地方放入:
参数:url:所要跳转的action maxPageItems:每页显示行数 items: 前面在action中保存的PageMode中的 总数据数
注意:在页面便利数据时候,也是利用PageMode中的list进行遍历的