1.jsp页面代码:
<!-- 分页开始 -->
<div class="paging">
<ul>
<li class="click">
<c:if test="${currentPage>1}">
<a href="#">首页</a>
</c:if>
</li>
<li class="click">
<c:if test="${currentPage>1}">
<a href="#">上一页</a>
</c:if>
</li>
<c:if test="${countPage<=6}">
<c:forEach var="i" begin="1" end="${countPage}">
<c:if test="${i==currentPage}">
<li class="click">
${i}
</li>
</c:if>
<c:if test="${i!=currentPage}">
<li>
<a
href="#">${i}</a>
</li>
</c:if>
</c:forEach>
</c:if>
<c:if test="${countPage>6}">
<c:if test="${currentPage<=3}">
<c:forEach var="i" begin="1" end="${currentPage+1}">
<c:if test="${i==currentPage}">
<li class="click">
${i}
</li>
</c:if>
<c:if test="${i!=currentPage}">
<li>
<a href="#">${i}</a>
</li>
</c:if>
</c:forEach>
<li class="click">
...
</li>
<c:forEach var="i" begin="${countPage-1}" end="${countPage}">
<li>
<a href="#">${i}</a>
</li>
</c:forEach>
</c:if>
<c:if test="${currentPage>3}">
<c:if test="${currentPage+6>countPage}">
<c:if test="${currentPage==4}">
<c:forEach var="i" begin="1" end="2">
<li>
<a href="#">${i}</a>
</li>
</c:forEach>
</c:if>
<c:if test="${currentPage!=4}">
<c:forEach var="i" begin="1" end="3">
<li>
<a href="#">${i}</a>
</li>
</c:forEach>
</c:if>
<li class="click">
...
</li>
<c:forEach var="i" begin="${currentPage-1}" end="${countPage}">
<c:if test="${i==currentPage}">
<li class="click">
${i}
</li>
</c:if>
<c:if test="${i!=currentPage}">
<li>
<a href="#">${i}</a>
</li>
</c:if>
</c:forEach>
</c:if>
<c:if test="${currentPage+6<=countPage}">
<c:if test="${currentPage==4}">
<c:forEach var="i" begin="1" end="2">
<li>
<a href="#">${i}</a>
</li>
</c:forEach>
</c:if>
<c:if test="${currentPage!=4}">
<c:forEach var="i" begin="1" end="3">
<li>
<a href="#">${i}</a>
</li>
</c:forEach>
</c:if>
<li class="click">
...
</li>
<c:forEach var="i" begin="${currentPage-1}"
end="${currentPage+3}">
<c:if test="${i==currentPage}">
<li class="click">
${i}
</li>
</c:if>
<c:if test="${i!=currentPage}">
<li>
<a href="#">${i}</a>
</li>
</c:if>
</c:forEach>
<li class="click">
...
</li>
<c:forEach var="i" begin="${countPage-1}" end="${countPage}">
<li>
<a href="#">${i}</a>
</li>
</c:forEach>
</c:if>
</c:if>
</c:if>
<c:if test="${currentPage<countPage}">
<li class="click">
<a href="#">下一页</a>
</li>
</c:if>
<c:if test="${currentPage<countPage}">
<li class="click">
<a href="#">末页</a>
</li>
</c:if>
</ul>
</div>
<!-- 分页结束 -->
2.Action代码:
//当前页面
private int currentPage = 1;
//页面大小
private int pageSize = 15;
//总页数
static int countPage = 1;
//countPage的getter和setter方法
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
//execute()中的代码
//查询总页数
countPage = DAO对象.pageCount(表名,pageSize, 数据库连接);
request.setAttribute("countPage", countPage);
request.setAttribute("currentPage", currentPage);
3.DAO代码
public int pageCount(String tableName, int pageSize,
DataBaseService dataBaseService) {
int count = dataBaseService.findRows("select count(*) from "
+ tableName);
return count / pageSize + 1;
}