个人觉得分页的重点在于
- 拿到数据后,根据每页不同的数据条数,来计算总页数
- 上一页 和 下一页跳转时需要考虑第一页和最后一页 这种特殊情况的出现,进行合理的判断
- 修改每页的数据展示量时,如果在页面中持久保存这个展示量,而不会因为跳转而发生改变
- 上一页和下一页中间的跳转页码,如何根据当前页码和总页数进行动态变化
以上均是个人学习分页展示的思考,不喜勿喷
界面比较丑,见谅:
代码如下,JSTL 配合 EL表达式
<%@ page import="java.util.List" %>
<%@ page import="entity.User" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!--输出,条件,迭代标签库-->
<%@ page isELIgnored="false"%> <!--支持EL表达式,不设的话,EL表达式不会解析-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="userDB" class="Dao.iml.UserDaoImpl"></jsp:useBean>
<html>
<head>
<title>用户分页展示</title>
<style>
.tr_color{
background-color: #739E39;
}
</style>
<script>
function changeSize(){
document.getElementById("f1").submit();
}
</script>
</head>
<%
int count = -1;
if(session.getAttribute("count") == null){
count = userDB.getUserCount();
session.setAttribute("count",count);
}
int pageSize = 10;
int pageIndex = 1;
if(request.getParameter("pageSize") != null && request.getParameter("pageSize") != ""){
pageSize = Integer.parseInt(request.getParameter("pageSize"));
}
int temp = (int)session.getAttribute("count")/pageSize;
int pageCount = (int)session.getAttribute("count")%pageSize == 0 ? temp : temp+1;
String requestPage = request.getParameter("pageIndex");
if (requestPage != null && requestPage != ""){
int requestIndex = Integer.parseInt(request.getParameter("pageIndex"));
if (requestIndex >= 1 && requestIndex <= pageCount) {
pageIndex = requestIndex;
}
}
List<User> userList = userDB.getUserList(pageIndex,pageSize);
request.setAttribute("userList",userList);
request.setAttribute("pageIndex",pageIndex);
request.setAttribute("pageSize",pageSize);
request.setAttribute("pageCount",pageCount);
%>
<body>
<div>
<table border="1px yellow solid">
<tr>
<td>id</td>
<td>用户名</td>
<td>密码</td>
<td>电话号码</td>
<td>住址</td>
</tr>
<c:forEach items="${userList}" var="user" varStatus="status">
<tr <c:if test="${status.count%2 == 1}">class="tr_color"</c:if>>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.phoneNumber}</td>
<td>${user.address}</td>
</tr>
</c:forEach>
<tr>
<a href="
<c:url value="testJSTL.jsp">
<c:param name="pageIndex" value="1"></c:param>
<c:param name="pageSize" value="${pageSize}"></c:param>
</c:url>
">首页</a>
<a href="
<c:url value="testJSTL.jsp">
<c:param name="pageIndex" value="${pageIndex > 1? pageIndex-1 : pageIndex}"></c:param>
<c:param name="pageSize" value="${pageSize}"></c:param>
</c:url>
">上一页</a>
<%-- 根据页码展示5个跳转链接,如果最后一页则不显示--%>
<c:forEach var="i" varStatus="status" begin="1" end="5" step="1">
<c:if test="${pageIndex+status.index <= pageCount}">
<a href="
<c:url value="testJSTL.jsp">
<c:param name="pageIndex" value="${pageIndex + status.index}"></c:param>
<c:param name="pageSize" value="${pageSize}"></c:param>
</c:url>
">${pageIndex + status.index}</a>
</c:if>
</c:forEach>
<a href="
<c:url value="testJSTL.jsp">
<c:param name="pageIndex" value="${pageIndex < pageCount? pageIndex+1 : pageIndex}"></c:param>
<c:param name="pageSize" value="${pageSize}"></c:param>
</c:url>
">下一页</a>
<a href="
<c:url value="testJSTL.jsp">
<c:param name="pageIndex" value="${pageCount}"></c:param>
<c:param name="pageSize" value="${pageSize}"></c:param>
</c:url>
">尾页</a>
</tr>
<form action="testJSTL.jsp" method="post" name="f1" id="f1">
<input type="text" id="pageIndex" name="pageIndex" >
<input type="submit" value="go">
<select name="pageSize" id="pageSize" onchange="changeSize()">
<option value="${pageSize}">选择每页展示条数</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
</form>
</table>
</div>
</body>
</html>