分页类分享:
package com.hcj.util; import java.util.ArrayList; import java.util.List; import org.apache.struts2.json.annotations.JSON; //分页类 public class Page<E> { private int totals; //查询到的总数量 private List<E> bigList ; //总记录集 private List<E> smallList; //当页显示的记录集 private int currentPage = 1; //当前页 private int pages; //总页数 private int pageSize = 8; //每页记录数 private int privious; //上一页 private int next; //下一页 private boolean firstPage = false;//第一页 private boolean lastPage = false; //最后一页 public int getTotals() { return totals; } public void setTotals(int totals) { this.totals = totals; } @JSON(serialize=false) public List<E> getBigList() { return bigList; } public void setBigList(List<E> bigList) { this.bigList = bigList; } public List<E> getSmallList() { this.smallList = new ArrayList<E>(); for (int i = (currentPage - 1) * pageSize; (i < currentPage* pageSize)&& (i < totals); i++) { smallList.add(bigList.get(i)); } return smallList; } public void setSmallList(List<E> smallList) { this.smallList = smallList; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPages() { if (totals % pageSize == 0) { pages = totals / pageSize; } else { pages = totals / pageSize + 1; } return pages; } public void setPages(int pages) { this.pages = pages; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPrivious() { if(currentPage != 1){ privious = currentPage - 1; }else{ privious = 1; } return privious; } public void setPrivious(int privious) { this.privious = privious; } public int getNext() { if(currentPage != getPages()){ next = currentPage + 1; }else{ next = currentPage; } return next; } public void setNext(int next) { this.next = next; } public boolean isFirstPage() { if(currentPage == 1){ firstPage = true; } return firstPage; } public void setFirstPage(boolean firstPage) { this.firstPage = firstPage; } public boolean isLastPage() { if(currentPage == getPages()){ lastPage = true; } return lastPage; } public void setLastPage(boolean lastPage) { this.lastPage = lastPage; } public Page() { super(); // TODO Auto-generated constructor stub } //输入总结果,当前页和每页大小(默认为8页) public Page(List<E> bigList, int currentPage, int pageSize) { super(); this.bigList = bigList; this.currentPage = currentPage; this.pageSize = pageSize; //设置总的total,这步很重要 this.setTotals(bigList.size()); } public Page(List<E> bigList, int currentPage) { super(); this.bigList = bigList; this.currentPage = currentPage; this.setTotals(bigList.size()); } }
时间: 2024-10-03 22:33:27