PageUtils.java 自己写的分页。记录一下。
package com.targ.knowledge.utils;
import java.util.ArrayList;
import java.util.List;
/**
* 分页通用类
*
* 使用方法
* 1.设置当前页
* 2.设置总记录数 setTotalRows
* 3.设置数据,激发分页处理方法
*
* 注:以上步骤按顺序进行。切记、切记
* @author yangzc
*
*/
public class PageUtils implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 7822326584568796627L;
private int currentPage=1;//当前页
private int totalPages=0;//总页数
private boolean hasNextPage=false;//没有下一页
private boolean hasPreviousPage=false;//没有上一页
private int pageStartRow=0;//每页的起始行
private int pageEndRow=0;//每页显示的终止数
private int pageStartPage = 1;//页面显示开始页数
private int pageEndPage = 1;//页面显示终止页数
private int showPages=21;//分页要显示的页数
private int pageRecorders=4;//每页记录
private int totalRows=0;//总行数
private List<?> list;//传过一个list,就可以对list进行分页
private float time = 0.00f;
public PageUtils(){}
//初始化当前页
public PageUtils(int currentPage){
this.currentPage = currentPage;
}
public void clone(PageUtils pages){
this.currentPage = pages.getCurrentPage();
this.totalPages = pages.getTotalPages();
this.hasNextPage = pages.isHasNextPage();
this.hasPreviousPage = pages.isHasPreviousPage();
this.pageStartPage = pages.getPageStartPage();
this.pageEndPage = pages.getPageEndPage();
this.totalRows = pages.getTotalRows();
this.list = pages.getList();
this.time = pages.getTime();
}
/**
* 设定检索到的总记录数
* @param totalRows
*/
public void setTotalRows(int totalRows){
this.totalRows = totalRows;
setData();
}
public static void main(String[] args) {
int recordSize = 189;
PageUtils page = new PageUtils(1);
page.setTotalRows(recordSize);
// page.setData(null);;
int totalPage = page.getTotalPages();
// System.out.println("数据范围为:("+page.getPageStartRow() + "," + page.getPageEndRow() + ")");
// System.out.print("当前页:"+page.getCurrentPage() +" ");
int startPage = page.getPageStartPage();
int endPage = page.getPageEndPage();
// for(int j=startPage; j<=endPage; j++){
// System.out.print(j + ",");
// }
// System.out.print(" 总页数为:"+page.getTotalPages());
// System.out.println();
// System.out.println("------------------------------------------");
for(int i=1;i<= totalPage; i++){
page = new PageUtils(i);
page.setTotalRows(recordSize);
// page.setData(null);
System.out.println("数据范围为:("+page.getPageStartRow() + "," + page.getPageEndRow() + ")");
System.out.print("当前页:"+page.getCurrentPage() +" ");
startPage = page.getPageStartPage();
endPage = page.getPageEndPage();
for(int j=startPage; j<=endPage; j++){
if(page.getCurrentPage() != j)
System.out.print(j + ",");
else
System.out.print("<" + j + ">,");
}
System.out.print(" 总页数为:"+page.getTotalPages());
System.out.println();
System.out.println("------------------------------------------");
}
}
public List<String> getPageLst(){
List<String> pagelst = new ArrayList<String>();
for(int i=pageStartPage; i<= pageEndPage; i++){
pagelst.add(i+"");
}
return pagelst;
}
/**
* 设定记录
*/
public void setData(){
//设定总页数
if(totalRows%pageRecorders == 0){
totalPages = totalRows/pageRecorders;
}else{
totalPages = totalRows/pageRecorders + 1;
}
//设置是否有下一页
if((currentPage+1) > totalPages){
hasNextPage = false;
}else{
hasNextPage = true;
}
//设置是否有上一页
if((currentPage -1) > 0){
hasPreviousPage = true;
}else{
hasPreviousPage = false;
}
//设置每页的起始行
if(hasPreviousPage){
pageStartRow = (currentPage - 1)*pageRecorders + 1;
}else{
pageStartRow = 1;
}
//设定每页的终止行
if(hasNextPage){
pageEndRow = currentPage* pageRecorders;
}else{
pageEndRow = totalRows;
}
//页面显示开始页数
if(currentPage <= showPages/2){
pageStartPage = 1;
}else{
pageStartPage = currentPage - showPages/2;
if(showPages%2 ==0){
pageStartPage = pageStartPage + 1;
}
if(pageStartPage + showPages > totalPages){
pageStartPage = totalPages - showPages + 1;
if(pageStartPage <= 0){
pageStartPage = 1;
}
}
}
//页面显示终止页数
if(currentPage + showPages/2 >= totalPages){
pageEndPage = totalPages;
}else{
pageEndPage = currentPage + showPages/2;
if(pageEndPage - showPages < 0){
pageEndPage = showPages;
if(showPages > totalPages){
pageEndPage = totalPages;
}
}
}
}
/**
* 当前页开始行
* @return
*/
public int getPageStartRow(){
return pageStartRow;
}
/**
* 当前页结束行
* @return
*/
public int getPageEndRow(){
return pageEndRow;
}
/**
* 是否有前一页
* @return
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* 是否有后一页
* @return
*/
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* 总页数
* @return
*/
public int getTotalPages(){
return totalPages;
}
/**
* 当前页
* @return
*/
public int getCurrentPage(){
return currentPage;
}
/**
* 当前页显示开始页链接
* @return
*/
public int getPageStartPage() {
return pageStartPage;
}
/**
* 当前页显示终止页链接
* @return
*/
public int getPageEndPage() {
return pageEndPage;
}
/**
* 数据列表
* @return
*/
public List<?> getList() {
return list;
}
/**
*
* @param list
*/
public void setList(List<?> list) {
this.list = list;
}
public int getTotalRows() {
return totalRows;
}
public float getTime() {
return time;
}
public void setTime(float time) {
this.time = time;
}
}