PaginationSupport加强版的分页代码

Hibernate 专栏收录该内容
44 篇文章 0 订阅
PaginationSupport.java,这个类网上有,我把它加强了一下,看下面

import java.util.ArrayList;
import java.util.List;

public class PaginationSupport {
public static int PAGESIZE = 10;

private int pageSize = PAGESIZE;

private List items;

private int totalCount;

private int[] indexes = new int[0];

private int startIndex = 0;

private int lastStartIndex;

public PaginationSupport(){
}

public PaginationSupport(List items, int totalCount) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(0);
}

public PaginationSupport(List items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}

public PaginationSupport(List items, int totalCount, int pageSize,
int startIndex) {
setPageSize(pageSize);
setTotalCount(totalCount);
setItems(items);
setStartIndex(startIndex);
}

public List getItems() {
return items;
}

public void setItems(List items) {
this.items = items;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
if (totalCount > 0) {
this.totalCount = totalCount;
int count = totalCount / pageSize;
if (totalCount % pageSize > 0)
count++;
indexes = new int[count];
for (int i = 0; i < count; i++) {
indexes[i] = pageSize * i;
}
} else {
this.totalCount = 0;
}
}

public int[] getIndexes() {
return indexes;
}

public void setIndexes(int[] indexes) {
this.indexes = indexes;
}

public int getStartIndex() {
return startIndex;
}

public void setStartIndex(int startIndex) {
if (totalCount <= 0)
this.startIndex = 0;
else if (startIndex >= totalCount)
this.startIndex = indexes[indexes.length - 1];
else if (startIndex < 0)
this.startIndex = 0;
else {
this.startIndex = indexes[startIndex / pageSize];
}
}

public int getNextIndex() {
int nextIndex = getStartIndex() + pageSize;
if (nextIndex >= totalCount)
return getStartIndex();
else
return nextIndex;
}

public int getPreviousIndex() {
int previousIndex = getStartIndex() - pageSize;
if (previousIndex < 0)
return 0;
else
return previousIndex;
}

public int getLastIndex(){
int[] indexes = this.getIndexes();
if(indexes != null && indexes.length >0){
lastStartIndex = indexes[indexes.length-1];
}
return lastStartIndex;

}

// 一共多少页
public int getPages(){
if(getTotalCount()%pageSize==0){
return getTotalCount()/pageSize;
}
return (getTotalCount()/pageSize)+1;
}
//当前第几页
public int getCurrentPage(){
return (getStartIndex()/pageSize)+1;
}

//感谢dengyin2000提供如下算法
/**
* 记录显示的页码组
* @param totalPages
* @param currentPage
* @return
*/
public ArrayList<Integer> page(int totalPages, int currentPage) {
int adjacents = 3;//这个参数可以调ArrayList的长度,试试就知道了
ArrayList<Integer> result = new ArrayList<Integer>();
if (totalPages < (5 + (adjacents * 2))){ // not enough links to make it worth breaking up
writeNumberedLinks(1, totalPages, result);
}else{
if ((totalPages - (adjacents * 2) > currentPage) && (currentPage > (adjacents * 2))){ // in the middle
writeNumberedLinks(1, 1, result);
writeElipsis(result);
writeNumberedLinks(currentPage - adjacents - 1, currentPage + adjacents, result);
writeElipsis(result);
writeNumberedLinks(totalPages, totalPages, result);
}else if (currentPage < (totalPages / 2)){
writeNumberedLinks(1, 3 + (adjacents * 2), result);
writeElipsis(result);
writeNumberedLinks(totalPages, totalPages, result);
}else{ // at the end
writeNumberedLinks(1, 1, result);
writeElipsis(result);
writeNumberedLinks(totalPages - (2 + (adjacents * 2)), totalPages, result);
}
}
return result;
}

/**
* @param result
*
*/
private void writeElipsis(ArrayList<Integer> result) {
result.add(-1);//-1是用来打点的
}

/**
* @param i
* @param lastIndex
* @param result
*/
private void writeNumberedLinks(int i, int lastIndex, ArrayList<Integer> result) {
for (int d=i; d <= lastIndex; d++) {
result.add(d);
}
}

}

在jsp中的用法:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html:hidden property="startIndex"/>
<table width="100%" border="0" cellpadding="2" cellspacing="0" bordercolor="#4DB0E7" class="TableMain">
<tr bgcolor="#FFFFFF">
<td width="17%">总记录 ${ requestScope.pagination.totalCount } 条
<input title="首页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value='0';" value="首 页">
<input title="上一页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value=${ requestScope.pagination.previousIndex };" value="上一页">
<c:forEach var="i" items="${requestScope.pages}">
<c:choose>
<c:when test="${i==-1}" >
...
</c:when>
<c:when test="${i==requestScope.pagination.currentPage}" >
<a href="userManage.portal?action=listUser&page=${ i }" target="mainFrame" style="color:red;">${ i }</a>
</c:when>
<c:otherwise>
<a href="userManage.portal?action=listUser&page=${ i }" target="mainFrame" >${ i }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<input title="下一页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value=${ requestScope.pagination.nextIndex };" value="下一页">
<input title="尾页" class="menuList" name="1" type="submit" onClick="this.document.forms[0].startIndex.value=${ requestScope.pagination.lastIndex };" value="尾页">
共有${ requestScope.pagination.pages }页,当前是第${ requestScope.pagination.currentPage }页
</td>
</tr>
</table>


上面的首页,上一页,下一页,尾页我使用了submit提交,其实也可以用<a href="...">
只是我一开始做的时候只有首页,上一页,下一页,尾页四个button,没有考虑中间的1,2,3,4,5,6....所以有些混乱,勉强看吧。
下面的是在action中用法:

String action=request.getParameter("action");
if("listUser".equals(action)){
String page=request.getParameter("page");
if(page!=null){
int p=Integer.parseInt(page);
form.setStartIndex((p-1)*Constant.PAGESIZE);//p总是从1开始,但记录从0开始,所以p-1
}
PaginationSupport pagination=this.getBaseService().findPageByCriteria(detachedCriteria, Constant.PAGESIZE, form.getStartIndex());//该方法网上有,查一下
List list = pagination.getItems();
request.setAttribute("list", list);
request.setAttribute("pagination", pagination);
request.setAttribute("pages", pagination.page(pagination.getPages(), pagination.getCurrentPage()));
return mapping.findForward("listUser");
}

这里要传递的是startIndex的值,所以form中需要定义int startIndex的getter,setter

这个分页我个人认为比较好了,代码比较优美。
类似如下效果:
总记录 108 条 首页 上一页 1 2 3 4 5 ...10 下一页 尾页 共有10页,当前是第1页
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值