通用的分页类

/*
 * Created on 2004-10-20
 */

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.common.util.PaginatedArrayList;
import com.ibatis.common.util.PaginatedList;

/**
 * @author simon
 * 通用的分页类
 * 
 * 新生成Page时 
 *     //condition是查询条件的map
 *     Map condition = new HashMap();
 *     condition.put("name",request.getParameter("name"));
 *
 *     //得到查询结果
 *     List raw = this.queryResult();
 *
 *     Page page = new Page(raw ,condition);
 *
 *     request.getSession().setAttribute("PAGE" , page);
 *   翻页操作:    
 *     Page page = request.getSession().getAttribute("PAGE");
 *    //跳转方式
 *     String param = request.getParameter("param");
 *     //指定页
 *     String toPage = request.getParameter("pages");
 *
 *     if(page.pageOperation(String param, String toPage))
 *       request.getSession().setAttribute("PAGE",page);
 * 
 */
public class Page {

 /**
  * 记录数
  */
 private int totalSize = 0;

 /**
  * 所有页
  */
 private int totalPage = 0;

 /**
  * 当前页
  */
 private int pageIndex = 0;

 /**
  * 每页记录数
  */
 private int pageSize = 8;

 /**
  * 查询结果
  */
 private List list = null;

 /**
  * 查询条件
  */
 private Map condition = new HashMap();

 public Page(List raw, Map condition) {
  this.condition = condition;

  if (raw != null) {
   PaginatedList list = new PaginatedArrayList(raw, this.pageSize);

   int pageIndex = 0;
   int maxIndex = raw.size() / list.getPageSize();

   if ((raw.size() % list.getPageSize()) != 0) {
    maxIndex++;
   }

   list.gotoPage(this.pageIndex);
   this.pageIndex = pageIndex;
   this.totalPage = maxIndex;
   this.totalSize = raw.size();

  }
 }
 /**
  * 翻页
  *
  * @param param
  * @param toPage
  * @return
  */

 public boolean pageOperation(String param, String toPage) {

  if (this.list != null) {
   if (param.compareTo("turnto") == 0) {
    if (toPage == null)
     return false;
    else {
     this.turnto(Integer.parseInt(toPage));
    }
   } else if (param.compareTo("first") == 0) {
    this.first();
   } else if (param.compareTo("last") == 0) {
    this.last();
   } else if (param.compareTo("next") == 0) {
    this.next();
   } else if (param.compareTo("prev") == 0) {
    this.prev();
   }
   return true;
  }
  return false;
 }
 /**
  * 向后一页
  * 
  */
 private void next() {
  PaginatedList list = (PaginatedList) this.list;

  if (list.isNextPageAvailable()) {
   list.nextPage();
   this.pageIndex = list.getPageIndex();
  }
 }

 /**
  * 向前一页
  * 
  */
 private void prev() {
  PaginatedList list = (PaginatedList) this.list;

  if (list.isPreviousPageAvailable()) {
   list.previousPage();
   this.pageIndex = list.getPageIndex();
  }
 }

 /**
  * 跳转到第一页
  * 
  */
 private void first() {
  this.pageIndex = 0;
  PaginatedList list = (PaginatedList) this.list;

  if (!list.isFirstPage()) {
   list.gotoPage(this.pageIndex);
  }
 }

 /**
  * 跳转到最后一页
  * 
  */
 private void last() {
  PaginatedList list = (PaginatedList) this.list;

  if (!list.isLastPage()) {
   list.gotoPage(this.totalPage);
   this.pageIndex = this.totalPage;
  }
 }

 /**
  * 跳转到指定页
  *
  * @param num
  */
 private void turnto(int num) {
  PaginatedList list = (PaginatedList) this.list;

  this.pageIndex = num - 1;

  if (this.pageIndex < 0) {
   this.pageIndex = 0;
  } else if (this.pageIndex > this.totalPage) {
   this.pageIndex = this.totalPage;
  }

  list.gotoPage(this.pageIndex);
 }

 

 /**
  * @return Returns the list.
  */
 public List getList() {
  return list;
 }

 /**
  * @return Returns the pageIndex.
  */
 public int getPageIndex() {
  return pageIndex;
 }

 /**
  * @return Returns the totalPage.
  */
 public int getTotalPage() {
  return totalPage;
 }

 /**
  * @return Returns the totalSize.
  */
 public int getTotalSize() {
  return totalSize;
 }

 /**
  * @return Returns the condition.
  */
 public Map getCondition() {
  return condition;
 }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值