您的位置:首页 > 其它

分页技术总结

2015-12-22 14:43 260 查看
分页在Web开发中是很常用的技术,下面介绍下最简单的分页实现:

首先介绍一下实现分页需要的类:

Page:分页信息类,封装了分页所需的所有信息,如每页显示记录数、总记录数、总页数、当前页、查询起始点、是否有上一页、是否有下一页。

PageUtil:分页辅助类,进行分页的时候,必须知道3个信息,分别是当前页、每页显示记录数、总记录数,其他信息都可以通过这三个信息计算得到。

Page类:

public class Page {

private int everyPage;      //每页显示记录数

private int totalCount;     //总记录数

private int totalPage;      //总页数

private int currentPage;    //当前页

private int beginIndex;     //查询起始点

private boolean hasPrePage;    //是否有上一页

private boolean hasNextPage;   //是否有下一页

public Page(int everyPage, int totalCount, int totalPage, int currentPage,
int beginIndex, boolean hasPrePage, boolean hasNextPage) {
super();
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}

public int getEveryPage() {
return everyPage;
}

public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}

public int getTotalPage() {
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public int getBeginIndex() {
return beginIndex;
}

public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}

public boolean isHasPrePage() {
return hasPrePage;
}

public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}

public boolean isHasNextPage() {
return hasNextPage;
}

public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}

}


PageUtil类:

public class PageUtil {

public static Page createPage (int everyPage, int totalCount, int currentPage) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}

public static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}

public static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}

public static int getTotalPage(int everyPage, int totalCount) {
int totalPage = 0;
if (totalCount != 0 && totalCount%everyPage == 0) {
totalPage = totalCount/everyPage;
} else {
totalPage = totalCount/everyPage + 1;
}
return totalPage;
}

public static int getBeginIndex (int everyPage, int currentPage) {
return (currentPage - 1)*everyPage;
}

public static boolean getHasPrePage(int currentPage) {
return currentPage != 1 ? true : false;
}

public static boolean getNextPage(int totalPage, int currentPage) {
return totalPage == currentPage || totalPage == 0 ? false : true;
}

}


在Controller中封装好Page,传入Service获取相应的数据,然后将数据列表和Page对象返回前台,

页面写法:

<div>
<c:choose>
<c:when test="${page.hasPrePage}">
<a href="xxx?currentPage=1">首页</a>
<a href="xxx?currentPage=${page.currentPage-1}">上一页</a>
</c:when>
<c:otherwise>首页|上一页</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.hasNextPage}">
<a href="xxx?currentPage=${page.currentPage+1}">下一页</a>
<a href="xxx?currentPage=${page.totalPage}">尾页</a>
</c:when>
<c:otherwise>下一页|尾页</c:otherwise>
</c:choose>
当前为第${page.currentPage}页,共${page.totalPage}页
</div>


到此一个简单的分页实现完毕。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: