JSTL实现分页展示查询内容及跳转页面

这篇博客详细介绍了使用JSP和JSTL实现用户数据的分页展示,包括计算总页数、处理上一页和下一页的跳转逻辑、在页面中持久化每页展示数据量以及动态生成跳转页码。同时,还展示了如何通过JavaScript处理用户选择的每页展示条数的变化。
摘要由CSDN通过智能技术生成

个人觉得分页的重点在于

  1. 拿到数据后,根据每页不同的数据条数,来计算总页数
  2. 上一页 和 下一页跳转时需要考虑第一页和最后一页 这种特殊情况的出现,进行合理的判断
  3. 修改每页的数据展示量时,如果在页面中持久保存这个展示量,而不会因为跳转而发生改变
  4. 上一页和下一页中间的跳转页码,如何根据当前页码和总页数进行动态变化

以上均是个人学习分页展示的思考,不喜勿喷

界面比较丑,见谅:

页面实现

代码如下,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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值