通用的分页类 收藏
/*
* 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;
}
}
阅读(955) | 评论(0) | 转发(0) |