1.jsp页面代码:
<!--分页开始 -->
<divclass="paging">
<ul>
<liclass="click">
<c:iftest="${currentPage>1}">
<ahref="#">首页</a>
</c:if>
</li>
<liclass="click">
<c:iftest="${currentPage>1}">
<ahref="#">上一页</a>
</c:if>
</li>
<c:iftest="${countPage<=6}">
<c:forEachvar="i" begin="1" end="${countPage}">
<c:iftest="${i==currentPage}">
<liclass="click">
${i}
</li>
</c:if>
<c:iftest="${i!=currentPage}">
<li>
<a
href="#">${i}</a>
</li>
</c:if>
</c:forEach>
</c:if>
<c:iftest="${countPage>6}">
<c:iftest="${currentPage<=3}">
<c:forEachvar="i" begin="1" end="${currentPage+1}">
<c:iftest="${i==currentPage}">
<liclass="click">
${i}
</li>
</c:if>
<c:iftest="${i!=currentPage}">
<li>
<ahref="#">${i}</a>
</li>
</c:if>
</c:forEach>
<liclass="click">
...
</li>
<c:forEachvar="i" begin="${countPage-1}"end="${countPage}">
<li>
<ahref="#">${i}</a>
</li>
</c:forEach>
</c:if>
<c:iftest="${currentPage>3}">
<c:iftest="${currentPage+6>countPage}">
<c:iftest="${currentPage==4}">
<c:forEachvar="i" begin="1" end="2">
<li>
<ahref="#">${i}</a>
</li>
</c:forEach>
</c:if>
<c:iftest="${currentPage!=4}">
<c:forEachvar="i" begin="1" end="3">
<li>
<ahref="#">${i}</a>
</li>
</c:forEach>
</c:if>
<liclass="click">
...
</li>
<c:forEachvar="i" begin="${currentPage-1}"end="${countPage}">
<c:iftest="${i==currentPage}">
<liclass="click">
${i}
</li>
</c:if>
<c:iftest="${i!=currentPage}">
<li>
<ahref="#">${i}</a>
</li>
</c:if>
</c:forEach>
</c:if>
<c:iftest="${currentPage+6<=countPage}">
<c:iftest="${currentPage==4}">
<c:forEachvar="i" begin="1" end="2">
<li>
<ahref="#">${i}</a>
</li>
</c:forEach>
</c:if>
<c:iftest="${currentPage!=4}">
<c:forEachvar="i" begin="1" end="3">
<li>
<ahref="#">${i}</a>
</li>
</c:forEach>
</c:if>
<liclass="click">
...
</li>
<c:forEachvar="i" begin="${currentPage-1}"
end="${currentPage+3}">
<c:iftest="${i==currentPage}">
<liclass="click">
${i}
</li>
</c:if>
<c:iftest="${i!=currentPage}">
<li>
<ahref="#">${i}</a>
</li>
</c:if>
</c:forEach>
<liclass="click">
...
</li>
<c:forEachvar="i" begin="${countPage-1}"end="${countPage}">
<li>
<ahref="#">${i}</a>
</li>
</c:forEach>
</c:if>
</c:if>
</c:if>
<c:iftest="${currentPage<countPage}">
<liclass="click">
<ahref="#">下一页</a>
</li>
</c:if>
<c:iftest="${currentPage<countPage}">
<liclass="click">
<ahref="#">末页</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, intpageSize,
DataBaseServicedataBaseService) {
int count =dataBaseService.findRows("select count(*) from "
+tableName);
return count / pageSize +1;
}