Chinaunix首页 | 论坛 | 博客
  • 博客访问: 983027
  • 博文数量: 152
  • 博客积分: 4937
  • 博客等级: 上校
  • 技术积分: 1662
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-05 16:55
文章分类

全部博文(152)

文章存档

2013年(12)

2012年(6)

2011年(58)

2010年(43)

2009年(1)

2008年(15)

2007年(17)

我的朋友

分类: Java

2010-07-09 11:51:04

通用的分页类 收藏
/*
 * 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;
 }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fadestarfish/archive/2004/11/21/189918.aspx
阅读(917) | 评论(0) | 转发(0) |
0

上一篇:Velocity 的应用示例

下一篇:Velocity初体验

给主人留下些什么吧!~~