以前一直没有自己写过分页对象,自己模仿着写了一个分页对象,写完之后感觉也是挺简单的
package com.css.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.formula.functions.T;
/**
*
Title:PageBean
*
Description: 分页对象demo
* @author jlmao
* @date 2017-6-14 上午11:20:22
*/
@SuppressWarnings("hiding")
public class PageBean implements Serializable {
private static final long serialVersionUID = -6710532081301192385L;
/**
* 基本属性分析:
* 1.当前页 currentPage
* 2.每页多少数据 pageSize
* 3.数据总条数 totalCount
* 4.总页数 pageCount
* 5.数据集合 List list
* 6.每页显示的最多的页码数 pageNumSize
* 7.拼接的分页字符串 pageHtml
*/
private int currentPage = 1;
private int pageSize = 5;
private int totalCount;
private int pageCount;
private List list;
private int pageNumSize = 5;
private String pageHtml;
/**
* 基本逻辑分析:
* 1.list总数判断得出totalCount和pageCount
* 2.currentPage*pageSize得出startRow和endRow
* 3.通过list.submit(startRow,endRow)得出需要展示的当前页的数据;
* 4.通过currentPage、pageSize、totalCount、pageCount拼出分页html字符串前台
* 需要拼接的项目有首页、上一页、下一页、末页以及中间具体页码
* 5.前台点击上述项目时,ajax传递currentPage和pageSize及查询条件至后台
* 6.根据查询条件查询出list,和currentPage和PageSize传入PageBean对象生成需要展示的list以及分页html
*/
/**
*
Description: pageBean初始化方法
* @param list 数据集合 currentPage 页面传递的当前页码 pageSize 页面传递的pageSize
* @author jlmao
* @date 2017-6-14 上午11:41:19
*/
public void initPage(List list, Integer currentPage){
//1.初始化各属性的值
if(currentPage != null && currentPage >= 1){
this.currentPage = currentPage;
}
this.list = list;
this.totalCount = this.list.size();
this.pageCount = totalCount%this.pageSize == 0?totalCount/this.pageSize:totalCount/this.pageSize+1;
int startRow = (this.currentPage-1)*this.pageSize;
int endRow = this.currentPage*this.pageSize;
if(endRow > list.size()){
endRow = list.size();
}
//2.拼接分页html
this.pageHtml = initPageHtml();
//3.截取list
if(!this.list.isEmpty()){
this.list = this.list.subList(startRow, endRow);
}
}
/**
*
Description: 拼接分页html
* @author jlmao
* @date 2017-6-14 下午1:53:56
*/
public String initPageHtml(){
/**
* 基本逻辑分析:
* 1.判断list是否为空
* 2.根据currentPage判断是否有首页、上一页、下一页、末页
*/
StringBuffer sb = new StringBuffer();
if(!this.list.isEmpty()){
/**
* 样式及hover
*/
String background = "background: #2db3c1;border: 1px solid #2db3c1;color: #fff;";
String style = "text-decoration:none;padding: 8px 10px;margin-left: 3px;border: 1px solid #cdcccc;color: #2db3c1;cursor: pointer;max-width: 50px;";
String hover = "οnmοuseοver=\"$(this).attr('style','"+style+background+"');\" οnmοuseοut=\"$(this).attr('style','"+style+"');\"";
//页面拼接开始
String firstPage = "首页";
sb.append(firstPage);
String prePage = "";
if(this.currentPage != 1){
prePage = "上一页";
}else{
prePage = "上一页";
}
sb.append(prePage);
//当前页小于每页页码数时,默认从第一个开始
int num = 1;
//当前页大于每页页码数时
if(this.currentPage >= pageNumSize){
if((this.currentPage+(pageNumSize-1)/2) >= this.pageCount ){
num = this.currentPage-(this.pageNumSize-(this.pageCount+1-this.currentPage));
}else{
num = this.currentPage - (pageNumSize-1)/2;
}
}
int endNum = (num+pageNumSize-1) > pageCount ?pageCount:(num+pageNumSize-1);
for(int i = num; i <= endNum; i++){
String select = "";
String pageNumHtml = ""+i+"";
if(this.currentPage == i){
select = background;
pageNumHtml = ""+i+"";
}
sb.append(pageNumHtml);
}
String nextPage = "";
if(this.currentPage != this.pageCount){
nextPage = "下一页";
}else{
nextPage = "下一页";
}
sb.append(nextPage);
String endPage = "末页";
sb.append(endPage);
String counyStr = "共"+totalCount+"条数据/"+pageCount+"页";
sb.append(counyStr);
}
return sb.toString();
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
if(currentPage >= 1){
this.currentPage = currentPage;
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize >= 1){
this.pageSize = pageSize;
}
}
public int getTotalCount() {
return totalCount;
}
public int getPageCount() {
return pageCount;
}
public int getPageNumSize() {
return pageNumSize;
}
public void setPageNumSize(int pageNumSize) {
if(pageNumSize >= 1){
this.pageNumSize = pageNumSize;
}
}
public List getList() {
return list;
}
public String getPageHtml() {
return pageHtml;
}
}