首页 > 代码库 > java 分页代码

java 分页代码

java========================

/**
* 分页实体类
* @version [版本号, Nov 9, 2011]
*/
public class Page {

private int currentPage;//当前页
private int pageSize; //分页大小
private int totalNum; //总条数
private int totalPage; //总页数
private String url; //url地址
//private Object obj;
private String firstPageUrl;//首页
private String nextPageUrl; //下一页
private String prePageUrl; //上一页
private String lastPageUrl; //尾页


public Page(int currentPage, int pageSize, int totalNum, String url) {
super();
this.pageSize = pageSize;
this.totalNum = totalNum;
this.url = url;
totalPage = totalNum/pageSize;
//如果总条数能不被每页条数整除,总页数加1
if (0!=totalNum%pageSize){
totalPage ++;
}

if (currentPage>totalPage) {//当前页大于总页数时,把总页数的值赋给当前页
this.currentPage = totalPage;
}else if (currentPage<1) {//当前页大于小于0时,当前页为赋值为1
this.currentPage = 1;
}else {
this.currentPage = currentPage;
}

}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}

/**
* 设置首页地址
* create on Nov 10, 2011
*/
public void setFirstPageUrl(){
StringBuffer firstUrl = new StringBuffer(url);
firstUrl.append("currentPage=1&pageSize=").append(pageSize);
this.firstPageUrl = firstUrl.toString();
}
/**
* 得到首页地址
* create on Nov 9, 2011
*/
public String getFirstPageUrl(){
setFirstPageUrl();
return this.firstPageUrl;
}

/**
* 设置尾页地址
* create on Nov 10, 2011
*/
public void setLastPageUrl(){
StringBuffer lastUrl = new StringBuffer(url);
lastUrl.append("currentPage=")
.append(totalPage)
.append("&pageSize=")
.append(pageSize);
this.lastPageUrl = lastUrl.toString();
}

/**
* 得到尾页地址
* create on Nov 9, 2011
*/
public String getLastPageUrl(){
setLastPageUrl();
return this.lastPageUrl;
}

/**
* 设置下一页地址
* create on Nov 10, 2011
*/
public void setNextPageUrl(){
StringBuffer nextUrl = new StringBuffer(url);
nextUrl.append("currentPage=")
.append(currentPage+1)
.append("&pageSize=")
.append(pageSize);
this.nextPageUrl = nextUrl.toString();
}

/**
* 得到下一页地址
* create on Nov 9, 2011
*/
public String getNextPageUrl(){
setNextPageUrl();
return this.nextPageUrl;
}

/**
* 设置上一页地址
* create on Nov 10, 2011
*/
public void setPrePageUrl(){
StringBuffer preUrl = new StringBuffer(url);
preUrl.append("currentPage=")
.append(currentPage-1)
.append("&pageSize=")
.append(pageSize);
this.prePageUrl = preUrl.toString();
}

/**
* 得到上一页地址
* create on Nov 9, 2011
*/
public String getPrePageUrl(){
setPrePageUrl();
return this.prePageUrl;
}

}

 

 

 

jsp=========================

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div align="center">
<c:choose>
<c:when test="${page.currentPage != 1}">
<!-- 当前页不是首页时,首页和上一页为连接 -->
<a href="http://www.mamicode.com/${page.firstPageUrl}">首页</a>&nbsp;
<a href="http://www.mamicode.com/${page.prePageUrl}">上一页</a>&nbsp;
</c:when>
<c:otherwise>
<!-- 当前页是首页时,首页和上一页不为连接 -->
首页&nbsp;上一页&nbsp;
</c:otherwise>
</c:choose>

<!-- 当总页数小于5时,全部页数都列出 -->
<c:if test="${page.totalPage<=5 }">
<c:forEach begin="1" end="${page.totalPage}" var="i" step="1">
<c:if test="${page.currentPage==i}">
${i}
</c:if>
<c:if test="${page.currentPage!=i}">
<a href="http://www.mamicode.com/${page.url}currentPage=${i}&pageSize=${page.pageSize }">[${i}]</a>
</c:if>
</c:forEach>
</c:if>
<!-- 当总页数大于5时,只列5页,分布在当前页前后 -->
<c:if test="${page.totalPage>5 }">

<!-- begin为比当前页小2页的页数, end为比当前页大2页的页数 -->
<c:set var="begin">${page.currentPage-2}</c:set>
<c:set var="end">${page.currentPage+2}</c:set>

<!-- 当前页小于3时,为了使显示页数为5,把显示页的最后一个向后推相应的页数 -->
<c:if test="${begin<1}">
<c:forEach begin="1" end="${end-begin+1}" var="i" step="1">
<c:if test="${page.currentPage==i}">
${i}
</c:if>
<c:if test="${page.currentPage!=i}">
<a href="http://www.mamicode.com/${page.url}currentPage=${i}&pageSize=${page.pageSize }">[${i}]</a>
</c:if>
</c:forEach>
</c:if>

<!-- 当前页大于和总页数相差小于3时,为了使显示页数为5,把显示页的最前端的页数向前推 -->
<c:if test="${end>page.totalPage}">
<c:forEach begin="${begin-(end-page.totalPage)}" end="${page.totalPage}" var="i" step="1">
<c:if test="${page.currentPage==i}">
${i}
</c:if>
<c:if test="${page.currentPage!=i}">
<a href="http://www.mamicode.com/${page.url}currentPage=${i}&pageSize=${page.pageSize }">[${i}]</a>
</c:if>
</c:forEach>
</c:if>

<!-- 当前页大于2,且与总页数的差大于2时,显示当前页前2页到当前页后2页的页数 -->
<c:if test="${begin>=1 and end<=page.totalPage}">
<c:forEach begin="${begin}" end="${end}" var="i" step="1">
<c:if test="${page.currentPage==i}">
${i}
</c:if>
<c:if test="${page.currentPage!=i}">
<a href="http://www.mamicode.com/${page.url}currentPage=${i}&pageSize=${page.pageSize }">[${i}]</a>
</c:if>
</c:forEach>
</c:if>

</c:if>
<!-- 哇哈哈 -->
<c:choose>
<c:when test="${page.currentPage != page.totalPage}">
<!-- 当前页不是尾页时,下一页和尾页为连接 -->
&nbsp;<a href="http://www.mamicode.com/${page.nextPageUrl}">下一页</a>&nbsp;
<a href="http://www.mamicode.com/${page.lastPageUrl}">尾页</a>&nbsp;
</c:when>
<c:otherwise>
<!-- 当前页是尾页时,下一页和尾页不为连接 -->
&nbsp;下一页&nbsp;尾页&nbsp;
</c:otherwise>
</c:choose>

共${page.totalNum}条&nbsp;${page.totalPage}页&nbsp;

<!-- 总页数大于5时,显示跳转框 -->
<c:if test="${page.totalPage>5 }">
<form action="${page.url}pageSize=${page.pageSize }" method="post" style="display:inline">
到<input type="text" name="currentPage" onkeyup="this.value=http://www.mamicode.com/this.value.replace(/[^/d]/g,‘‘)" onafterpaste="this.value=http://www.mamicode.com/this.value.replace(/[^/d]/g,‘‘)" size="4" maxlength="9" />页<input type="submit" value="http://www.mamicode.com/跳转">
</form>
</c:if>
</div>

java 分页代码