博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 分页
阅读量:7082 次
发布时间:2019-06-28

本文共 3702 字,大约阅读时间需要 12 分钟。

hot3.png

/** *  */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()); }}

转载于:https://my.oschina.net/u/1169079/blog/205719

你可能感兴趣的文章
黄聪:php计算获取页面执行时间
查看>>
iOS 三种定时器
查看>>
[状压DP][二分]JZOJ 3521 道路覆盖
查看>>
【错误】 “=” 与 "==" 不分
查看>>
Java技术回顾之JNDI:命名和目录服务基本概念(转)
查看>>
0622 总结与回顾
查看>>
[转]SharePoint 2010 Download as Zip File Custom Ribbon Action
查看>>
getprop 获取android系统属性
查看>>
【C++】traits classes
查看>>
jquery实现的3D缩略图悬停效果
查看>>
为什么要使用Handler
查看>>
XCode4.2下SVN怎么配置?如何进行版本控制?
查看>>
ETL随笔(一)zz
查看>>
SharePoint 2010:部署.resx(资源)文件到App_GlobalResources的简单方法
查看>>
Netflix新放出来的开源工具Chaos Monkey
查看>>
Python 会提高程序员的审美标准。
查看>>
SQL - 17.存储过程
查看>>
【梦话区】学习嵌入式需要哪些知识——转
查看>>
Qt添加信号槽
查看>>
vs2008调试 Release 工程
查看>>