/** * */package com.sobanks.utils;
import java.util.List;
/** * * @author hh * */public class Pagination{ // 总共的数据量 private int total;
// 每页显示多少条 private static int pageSize=2;
// 共有多少页 private int totalPage;
// 当前是第几页 private int index;
// 数据 private List data;
// 连接路径 private String path = "";
/** * 页码HTML信息 */ @SuppressWarnings("unused") private String pageHTML;
private int startPage; // 开始页面
private int endPage; // 结束页面
private int displayNum = 20; // 显示的页数 public Pagination(int total, int index, String path) { super(); this.total = total; this.index = index; this.path = path; }
public Pagination() { super(); }
public static void setPageSize(int pageSize) { Pagination.pageSize = pageSize; }
/** * @return the startPage */ public int getStartPage() { return startPage; }
/** * @return the endPage */ public int getEndPage() { return endPage; }
/** * @return the path */ public String getPath() { return path; }
public void setIndex(int index) { this.index = index; } /** * 返回起始行数 * @param index * @return */ public static int getStartRow(int index){ return index-1; } public static int getPageSize(){ return pageSize; } /** * 设置路径前缀,页面第一页index为1 * * @param path * 此path含有参数形式,如/aa/article?page=,或者/bb/article/list/ */ public void setPath(String path) { this.path = path; }
public int getTotalPage() { return (this.total + pageSize - 1) / pageSize; }
public int getIndex() { return index; }
public List getData() { return data; }
public void setData(List data) { this.data = data; }
/** * @return the total */ public int getTotal() { return total; }
/** * @param total * the total to set */ public void setTotal(int total) { this.total = total; } public String getPageHTML() { totalPage = getTotalPage(); StringBuffer displayInfo = new StringBuffer(); if (totalPage != 0 && pageSize != 0) { displayInfo.append(""); displayInfo.append(" 共 " + totalPage + "页 " + total + "条记录 "); //displayInfo.append(" / " + total + "条记录"); // 判断如果当前是第一页 则“首页”和“第一页”失去链接 if (index <= 1) { displayInfo.append(" 首页 "); displayInfo.append(" 上一页 "); } else { displayInfo.append(" 首页 "); displayInfo.append(" 上一页 "); }
countPages(); displayInfo.append(" "); for (int i = startPage; i <= endPage; i++) { if (i == index) { displayInfo.append(" " + i + " "); } else { displayInfo.append(" " + i + " "); } } displayInfo.append(" ");
if (index >= totalPage) { displayInfo.append(" 下一页 "); displayInfo.append(" 尾页"); } else { displayInfo.append(" 下一页 "); displayInfo.append(" 尾页"); } displayInfo.append(""); } return displayInfo.toString(); }
/** * 计算起始页和结束页 */ public void countPages() {
if (index - displayNum / 2 < 1) { startPage = 1; endPage = displayNum > totalPage ? totalPage : displayNum; } else if (index + displayNum / 2 > totalPage) { int n = totalPage - displayNum + 1; startPage = n > 0 ? n : 1; endPage = totalPage; } else { startPage = index - displayNum / 2; endPage = startPage + displayNum - 1; } }
/** * @param pageHTML the pageHTML to set */ public void setPageHTML(String pageHTML) { this.pageHTML = pageHTML; }
public static void main(String[] args) { Pagination p = new Pagination(); p.setTotal(1002); p.setPath("/bb/article/list/"); p.setIndex(33); System.out.println(p.getPageHTML()); }}