一个通用的分页PageBean

package com.ppy.web.util;

import java.util.List;

public class PageBean {
 /**
  *
  *
  * @author ppy  2008-10-18 14:3:56
  * totalRecords 总记录数
  * list         保存分页的数据
  * pageNo       当前页
  * pageSize     页大小
  * query        保存用户查询的字符串,查询分页用
  * pageAction   操作分页的Servlet或Action(struts)
  * method       (struts中DispatchAction中的method)
  *
  *
  */

 private int totalRecords;

 private List list;

 private int pageNo;

 private int pageSize;

 private String query;

 private String pageAction;

 private String method;

 public void setPageAction(String pageAction) {
  this.pageAction = pageAction;
 }

 public void setMethod(String method) {
  this.method = method;
 }

 public List getList() {
  return list;
 }

 public void setList(List list) {
  this.list = list;
 }

 public int getPageNo() {
  return pageNo;
 }

 public void setPageNo(int pageNo) {
  this.pageNo = pageNo;
 }

 public int getPageSize() {
  return pageSize;
 }

 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }

 public int getTotalRecords() {
  return totalRecords;
 }

 public void setTotalRecords(int totalRecords) {
  this.totalRecords = totalRecords;
 }

 

 public void setQuery(String query) {
  this.query = query;
 }

 

 /**
  * 取得总页数的方法 return
  * totalRecords%pageSize==0?(totalRecords/pageSize):(totalRecords/pageSize+1)
  *
  * @return
  */
 public int getTotalPages() {
  return (totalRecords + pageSize - 1) / pageSize;
 }

 /**
  * 得到首页
  *
  * @return
  */
 public int getTopPage() {
  return 1;
 }

 /**
  * 得到上一页
  *
  * @return
  */
 public int getPreviousPageNo() {
  if (pageNo <= 1)
   return 1;
  else
   return (pageNo - 1);
 }

 /**
  * 得到下一页
  *
  * @return
  */
 public int getNextPageNo() {
  if (pageNo >= getTotalPages()) {
   return getTotalPages() == 0 ? 1 : getTotalPages();
  } else {
   return pageNo + 1;
  }
 }

 /**
  * 得到尾页
  *
  * @return
  */
 public int getBottomPageNo() {
  return getTotalRecords() == 0 ? 1 : getTotalPages();
 }

 

    //页面分页导航的链接 方式一

 public String getPageToolBar1() {
  String str = "";
  str += "<a href='" + pageAction + "?method=" + method + "&userQuery="
    + query + "&pageNo=" + getTopPage() + "&pageSize=" + pageSize
    + "'>首页</a>&nbsp;";
  str += "<a href='" + pageAction + "?method=" + method + "&userQuery="
    + query + "&pageNo=" + getPreviousPageNo() + "&pageSize="
    + pageSize + "'>上一页</a>&nbsp;";
  str += "<a href='" + pageAction + "?method=" + method + "&userQuery="
    + query + "&pageNo=" + getNextPageNo() + "&pageSize="
    + pageSize + "'>下一页</a>&nbsp;";
  str += "<a href='" + pageAction + "?method=" + method + "&userQuery="
    + query + "&pageNo=" + getBottomPageNo() + "&pageSize="
    + pageSize + "'>尾页</a>&nbsp;";

  return str;
 }

 

    //页面分页导航的链接 方式二

 public String getPageToolBar2() {
  String str = "";
  int pageSplit = (pageNo / 5) * 5;

  for (int i = pageSplit - 1; i < (pageSplit + 6); i++) {
   if (i <= 0) {

   } else if (pageNo == i) {
    str += i + "&nbsp;";
   } else if (i > getTotalPages()) {

   } else {
    str += "<a href='" + pageAction + "?method=" + method
      + "&userQuery=" + query + "&pageNo=" + i + "&pageSize="
      + pageSize + "'>" + i + "</a>" + "&nbsp;";
   }
  }
  return str;
 }


 

}

 

 

以一个表为例来详述分页组件的用法,本例采用的Struts+Spring+Hibernate,分页用的是Hibernate中的方法

 

表名Product  字段:

id     int primary key identity(1,1),
 typeid     varchar(20),
 name       varchar(50),
 Price       float,
 meno       varchar(100)

 

 

1 pojo类和Product.hbm.xml省略

2 ProductDAO类

 

public class ProductDAO extends HibernateDaoSupport implements IProductDAO {

 public List getProducts(final int pageNo, final int pageSize) {

  List list = new ArrayList();

  list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {

    
    return session.createQuery("from Product").setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
    
   }
  });
  return list;
 }
 
 public int getTotalRecords(String query) {
  Integer totalRecords = null;
  if (query != null && query.trim().length() != 0) {
   totalRecords = (Integer)this.getHibernateTemplate().find("select count(*) from Product a where a.id like ? or a.name like ?",
            new Object[]{query + "%", query + "%"}).get(0);
  }else {
   totalRecords = (Integer)this.getSession().createQuery("select count(*) from Product a").uniqueResult();
   
  }
  return totalRecords.intValue();
 }

}

 

3 Action中的代码,本例没有采用DispatchAction,没有使用查询后再分页(PageBean中的query)

 


public class PageAction extends Action {
 
 private IProductDAO proDAO;

 private PageBean pageBean;

 public void setPageBean(PageBean pageBean) {
  this.pageBean = pageBean;
 }

 public void setProDAO(IProductDAO proDAO) {
  this.proDAO = proDAO;
 }

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  PageForm pageForm = (PageForm) form;
  int pageNo = pageForm.getPageNo();
  int pageSize = pageForm.getPageSize();
  
  List list = proDAO.getProducts(pageNo, pageSize);
  
  pageBean.setPageNo(pageNo);
  pageBean.setPageSize(pageSize);
  pageBean.setTotalRecords(proDAO.getTotalRecords(""));
  pageBean.setPageAction("page.do");
  pageBean.setList(list);
  
  request.setAttribute("pageBean", pageBean);
  
  return mapping.findForward("paging");
 }
 
}

 

3 页面的代码就很简单了,我用JSTL迭代标签,分页导航都封装在PageBean中了,取出来就好了(两种方式来分页)

<c:forEach items="${pageBean.list}" var="prod">
${prod.name } ${prod.meno }<br>
</c:forEach>
${pageBean.pageToolBar1 }
<hr>
${pageBean.pageToolBar2 }

 

 

4 web.xml struts-config.xml applicationContext.xml文件略

  ProductDAO PageAction  PageBean都在spring中注入

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值