package com.wpao.util;
import java.util.List;
public class Pagination {
private int pageNo=1;
private int pageSize=15;
private int totalPage;
private int totalCount;
private int onepage;
private int lstpage;
private List list;public Pagination() {
//
}
//计算总分页数:
public int GetPageCount(int totalRec,int pgeRec) {
int pageCount = 0;
if (totalRec % pgeRec == 0) {
pageCount = totalRec/pgeRec;
}else {
pageCount = totalRec/pgeRec + 1;
}
if(pageCount <= 0) {
pageCount = 1;
}
return pageCount;
}
//分页页码显示:
public void SetPageShow(Pagination pagination) {
int pagecode = pagination.getPageNo();
int pagecount = pagination.getTotalPage();
int ipage = 0;
if(pagecount > 9 && pagecode > 5){
if(pagecount == pagecode){//总页码45,当前页45
ipage = pagecode-8;
} else if(pagecount-1 == pagecode){//总页码45,当前页44
ipage = pagecode-7;
} else if(pagecount-2 == pagecode){//总页码45,当前页43
ipage = pagecode-6;
} else if(pagecount-3 == pagecode){//总页码45,当前页42
ipage = pagecode-5;
} else {//总页码45,当前页41
ipage = pagecode-4;
}
} else {
ipage = 1;//总页码小于等于9,或者当前页小于等于5
}
int cpage = 0;
if(pagecount > 9) {
if(pagecode > 5) {
if(pagecode != pagecount) {
cpage = ((pagecode + 4) > pagecount) ? pagecount : pagecode + 4;
}else {
cpage = pagecount;
}
}else {
cpage = 9;
}
}else {
cpage = pagecount;
}
pagination.setOnepage(ipage);
pagination.setLstpage(cpage);
}public int getPageNo() {
return pageNo;
}public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int ParseInt(String para_str,int defaultvalue) {
if(para_str!=null && !para_str.equals("")) {
try {
return Integer.parseInt(para_str);
}catch (Exception e) {
return defaultvalue;
}
}
return defaultvalue;
}public int getPageSize() {
return pageSize;
}public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}public int getOnepage() {
return onepage;
}public void setOnepage(int onepage) {
this.onepage = onepage;
}public int getLstpage() {
return lstpage;
}public void setLstpage(int lstpage) {
this.lstpage = lstpage;
}public int getTotalPage() {
return totalPage;
}public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}public int getTotalCount() {
return totalCount;
}public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}public List getList() {
return list;
}public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "Pagination [pageNo=" + pageNo + ", pageSize=" + pageSize
+ ", totalPage=" + totalPage + ", totalCount=" + totalCount
+ ", onepage=" + onepage + ", lstpage=" + lstpage + ", list="
+ list + "]";
}
}
客户端调用
public static void main(String[] args) {
String pageStr= "2";//当前页码;
Pagination fyb = new Pagination();
//1、封装分页对象
fyb.setTotalCount(100);//总条数
fyb.setPageSize(10);//每页显示数
fyb.setTotalPage(fyb.GetPageCount(fyb.getTotalCount(), fyb.getPageSize()));//总页数
int pageNo = 1;
if(pageStr!=null && !pageStr.equals("")) {
try {
pageNo = Integer.parseInt(pageStr);
}catch (Exception e) {
}
}
if (pageNo>fyb.getTotalPage()){
pageNo = fyb.getTotalPage();
}else if (pageNo<1){
pageNo=1;
}
fyb.setPageNo(pageNo);//当前页的编号.
fyb.SetPageShow(fyb); //执行内部计算,获得开始页码,终止页码
System.out.println(fyb);
}