手写基于bootstrap的分页后端代码
1、后端实体对捧盆象:public class Page<T> {
private Long pageNo; // 当前页码
private int pageSize = Base.pageSize; // 页面大小
private int length = Base.pageLength;// 显示页面长度
private Long count;// 总记录数,
private Long countPage;// 总页数
private Long first = 1L;// 分页开始数据
private Long last;// 分页肺罪注结束数据
private Long prev;// 上一页索引
private Long next;// 下一页索引
private String pageHtml;
private Long begin;
private T t;
private List<T> list = new ArrayList<T>();

2、/**
* 构造方法,
* @param pageNo 页码
* @param count 总数
*/
public Page(Long pageNo, Long count) {
this.begin = (pageNo-1)*pageSize;
this.pageNo = pageNo;
this.pageSize = getPageSize();
this.length = getLength();
this.count = count;

3、this.countPage = getCountPage();
this.first = getFirst();
this.last = getLast();
this.prev = getPrev();
this.next = getNext();
this.list = getList();
this.pageHtml = getPageHtml();
}

4、// 返回页码
public Long getPageNo() {
return pageNo;
}
public void setPageNo(Long pageNo) {
this.pageNo = pageNo;
}
// 返回上一页著销数据
public Long getPrev() {
if (pageNo <= 1L) {
return 1L;
} else {
return pageNo - 1;
}
}
public Long getNext() {
if (pageNo >= getCountPage()) {
return getCountPage();
} else {
return pageNo + 1;
}
}
// 返回总页数
public Long getCountPage() {
if (count % pageSize == 0) {
return count / pageSize;
}
return count / pageSize + 1;
}

5、public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
public Long getFirst() {
return first;
}
public Long getLast() {
return getCountPage();
}
public void setPrev(Long prev) {
this.prev = prev;
}
public void setNext(Long next) {
this.next = next;
}

6、public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}

7、@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("<ul id = 'pageUl' class='pagination'>").append("<li onclick='prev()'>")
.append("<a href='#' aria-label='Previous'>")
.append("<span aria-hidden='true'>«</span></a></li>");
//判断开始页
Long begin = (long) (pageNo - length/2) ;
if(begin <= 1L){
begin = 1L;
}
//判断结束页
Long end = begin + length - 1;
if(end >= countPage){
end = countPage;
begin = end - length + 1;
if (begin < first) {
begin = first;
}
}
for(Long i= begin;i <= end;i++){
sb.append("<li onclick='change(this,"+"\"\""+")'><a href='#'>"+i+"</a></li>");
}
//下一页
sb.append("<li onclick='next()'><a href='#' aria-label='Next'>"+
"<span aria-hidden='true'>»</span></a></li>");
sb.append("<li><a href='#'>共<span id='countPage'> "+countPage+" </span>页</a></li></ul>");
return sb.toString();
}
}
