java分页

package entity;

public class Page { //记录当前页的状态信息
private int num; //当前页号,采用自然数计数 1,2,3,...
private int size; //一页显示多少个数据
private int rowCount; //记录的总数目(一共有多少个数据)
private int pageCount; //页面总数(n页)
private int startRow; //当前页开始行号,第一行是0行
private int first=1; //第一页 页号
private int last; //最后页 页号
private int next; //下一页 页号
private int prev; //前一页 页号
private int start; // 页号式导航, 起始页号
private int end; // 页号式导航, 结束页号
private int numCount = 4; // 页号式导航, 最多显示页号数量为numCount+1;这里显示5页。

public Page(int size, String str_num, int rowCount) {

int num = 1;
if (str_num != null) { //str_num,起始记录号
num = Integer.parseInt(str_num);
}
this.num = num;
this.size = size;
this.rowCount = rowCount;
this.pageCount = (int) Math.ceil((double) rowCount / size); //**获取页数的总数,ceil是进1取整
this.num = Math.min(this.num, pageCount); //当前页号最大值为pageCount
this.num = Math.max(1, this.num); //当前页号最小值为1
this.startRow = (this.num-1) * size; //开始行号为 0行
this.last = this.pageCount; //最后一页页号为pageCount
this.next = Math.min(this.pageCount, this.num+1); //最后一页为pageCount或者为当前页+1
this.prev = Math.max(1, this.num-1); //最前一页为1或者为当前页-1

// 计算page 控制
//当点击7的时候只出现5到9,当点击8的时候,只出现6到10
//若是出现1-5或者6-10,则可改为:if((this.num+1)%(numCount+1)==0){
//start=(this.num+1)%(numCount+1)-1*(numCount+1)+1;}
//else{ start=this.num/(numCount+1)*(numCount+1)+1;}
//比如15和14
//本页显示页号从多少页开始,eg:7-4/2=5(第七页就是从第五页开始)
start = Math.max(this.num - numCount/2, first); 

//本页显示页号在多少页号结束 eg:5+4=9(若本页是第七页,则本页开始页为5,结束页为9,每页显示5个)
end = Math.min(start + numCount, last);
if (end - start < numCount) { //如果记录数小于4
start = Math.max(end - numCount, 1); //开始页为1和结束页号-记录数的最大值
}
}

public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getFirst() {
return first;
}
public void setFirst(int first) {
this.first = first;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public int getNext() {
return next;
}
public void setNext(int next) {
this.next = next;
}
public int getPrev() {
return prev;
}
public void setPrev(int prev) {
this.prev = prev;
}
public int getStart() {
return start;
}

public void setStart(int start) {
this.start = start;
}

public int getEnd() {
return end;
}

public void setEnd(int end) {
this.end = end;
}

public int getNumCount() {
return numCount;
}

public void setNumCount(int numCount) {
this.numCount = numCount;
}
}

  也可参考http://www.cnblogs.com/liushuijinger/p/3271815.html

上一篇:C语言指针操作


下一篇:Tomcat启动时自动加载Servlet