使用此分页插件很简单就可以实现多种分页风格!其官方网址为:pager taglib 网址在此下载要用的jar包及实例。现在开始分页:
1.首先导入所需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可以保存在指定的范围内,然后逃到页面进行显示数据:
在需要显示分页的地方放入:
view plaincopy to clipboardprint?
<!--别忘记引入插件-->
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
<div>
<pg:pager url="forward!viewAdmin.action" maxPageItems="10" items="${admins.total}" export="currentPageNumber=pageNumber">
<pg:first>
<a href="${pageUrl}" mce_href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }" mce_href="${pageUrl }">上一页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }" mce_href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }" mce_href="${pageUrl }">下一页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }" mce_href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>
</div>
<!--别忘记引入插件-->
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
<div>
<pg:pager url="forward!viewAdmin.action" maxPageItems="10" items="${admins.total}" export="currentPageNumber=pageNumber">
<pg:first>
<a href="${pageUrl}" mce_href="${pageUrl}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl }" mce_href="${pageUrl }">上一页</a>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<font color="red">${pageNumber }</font>
</c:when>
<c:otherwise>
<a href="${pageUrl }" mce_href="${pageUrl }">${pageNumber }</a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<a href="${pageUrl }" mce_href="${pageUrl }">下一页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }" mce_href="${pageUrl }">尾页</a>
</pg:last>
</pg:pager>
</div>
参数:url:所要跳转的action maxPageItems:每页显示行数 items: 前面在action中保存的PageMode中的 总数据数
注意:在页面便利数据时候,也是利用PageMode中的list进行遍历的
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/icejadelove/archive/2009/10/13/4663223.aspx
来源:https://www.cnblogs.com/JemBai/archive/2009/11/16/1603788.html