JSP+JSTL+EL表达式,实现web页面的页面跳转功能(上一页下一页首页末页页面跳转)

一、分页显示无处不在
意义:
实际中不会将所有数据一起显示出来,所以需要考虑数据的分页显示
二、前期准备
(1)jar包
在这里插入图片描述
(2)导入c标签库

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

三、具体实现分析

  1. 实现:首页上一页
    通过超链接,跳转到BookServlet,并且传递pageNo来实现
<c:choose>
    <c:when test="${requestScope.page.pageNo>1}">
        <a href="BookServlet?action=page&pageNo=1">首页</a>
        <a href="BookServlet?action=page&pageNo=${requestScope.page.pageNo-1}">上一页</a>
    </c:when>
    <c:otherwise>
        <a style="color: silver">首页</a>
        <a style="color: silver">下一页</a>
    </c:otherwise>
</c:choose>
  1. 实现:末页下一页,可以同上方法,也可使用jQuery实现:
<a class="aclass" href="BookServlet?action=page&pageNo=${requestScope.page.pageNo+1}">下一页</a>
<a class="aclass" href="BookServlet?action=page&pageNo=${requestScope.page.pageTotal}">末页</a>
<input type="hidden" id="pageNo" value="${requestScope.page.pageNo}"><span id="pageTotal">${ requestScope.page.pageTotal}</span>
$(function () {
    var pageNo = parseInt($("#pageNo").val());
    var pageTotal = parseInt($("#pageTotal").html());
    if (pageNo >= pageTotal) {
        $("a.aclass").removeAttr("href").css("color", "silver");
    }
});

3.实现:输入页面跳转
通过Jquery绑定单击事件,响应跳转servlet调用page方法并且传递pageNo实现

$("#inputbtn").click(function () {
        location.href = "BookServlet?action=page&pageNo=" + $("#pn_input").val();
});
  1. 实现:显示 1 2【3】4 5,多页面选项
    |-- 如果总页码小于等于 5 的情况,页码的范围是:1-总页码
    |-- 如果总页码大于5,那么就需要分情况讨论
    |-- 开始前3个,显示1-5
    |-- 最后3个页码,显示(Total-4)-Total
    |-- 之外,始终保持当前页码在中间,,显示(当前页码-2)-(当前页码+2)
<c:choose>
    <c:when test="${requestScope.page.pageTotal<=5}">
        <c:set var="begin" scope="page" value="1"></c:set>
        <c:set var="begin" scope="page" value="${requestScope.page.pageTotal}"></c:set>
    </c:when>
    <c:when test="${requestScope.page.pageTotal>5}">
        <c:choose>
            <c:when test="${requestScope.page.pageNo<=3}">
                <c:set var="begin" value="1"></c:set>
                <c:set var="end" value="5"></c:set>
            </c:when>
            <c:when test="${requestScope.page.pageNo > requestScope.page.pageTotal-3}">
                <c:set var="begin" value="${requestScope.page.pageTotal-4}"></c:set>
                <c:set var="end" value="${requestScope.page.pageTotal}"></c:set>
            </c:when>
            <%--其余其他情况,显示(当前页码-2-(当前页码+2--%>
            <c:otherwise>
                <c:set var="begin" value="${requestScope.page.pageNo-2}"></c:set>
                <c:set var="end" value="${requestScope.page.pageNo+2}"></c:set>
            </c:otherwise>
        </c:choose>
    </c:when>
</c:choose>

<c:forEach begin="${begin}" end="${end}" var="i">
    <c:if test="${i==requestScope.page.pageNo}"><span id="pageNo">${i}</span></c:if>
    <c:if test="${i!=requestScope.page.pageNo}">
        <a href="${requestScope.page.url}&pageNo=${i}">${i}</a>
    </c:if>
</c:forEach>

四、总的分析
|-- 分页显示的功能:实际中不会将所有数据一起显示出来,加载费时,效率也低,所以需要考虑分页显示
分页类:

public class Page<T> {
    //每页显示数据
    public static final Integer PAGE_SIZE = 8;
    // 当前页码
    private Integer pageNo;
    // 总页码
    private Integer pageTotal;
    // 当前页显示数量
    private Integer pageSize = PAGE_SIZE;
    // 总记录数
    private Integer pageTotalCount;
    // 当前页数据
    private List<T> items;
}

javaBean:Page类
DAO部分:BookDAO里面新增方法,新增测试
查询总记录数

@Override
public Integer queryForPageTotalCount() {
    return getValue("select count(*) from t_book").intValue();
}

查询当前页面的图书数据

@Override
public List<Book> queryForPageItems(int begin, int pageSize) {
    return getBeanList("select * from t_book order by id desc limit ?,?", begin, pageSize);
}
    2)分页模型 Page 的抽取(当前页数,总页数,总记录数,当前页数据,每页记录数)
        Service部分,新增page()方法
        方法体内调用DAO,得到数据库数据,将总记录数、总页码数、当前页面数据打包成page 对象
@Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page<>();
        // 设置当前页码
        page.setPageNo(pageNo);
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = bookDao.queryForPageTotalCount();
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 设置总页码
        if (page.getPageTotalCount() % page.getPageSize() == 0) {
            //总记录数/当前显示数量=总页数
            page.setPageTotal(page.getPageTotalCount() / pageSize);
        } else {
            page.setPageTotal(page.getPageTotalCount() / pageSize + 1);
        }
        // 通过当前页和每页显示数求 起始页
        int begin = (page.getPageNo() - 1) * page.getPageSize();
        // 求当前页数据
        List<Book> items = bookDao.queryForPageItems(begin, page.getPageSize());
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

Servlet部分,新增page部分,获取用户交互页面的信息,调用service实现功能:获取当前页码、页面显示数量pagesize;传递显示数据

 protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        System.out.println("pageNo = " + pageNo);
        System.out.println("pageSize = " + pageSize);
        //2 调用BookService.page(pageNo,pageSize):Page 对象
        Page<Book> page = bookService.page(pageNo, pageSize);
        //3 保存Page 对象到Request 域中
        req.setAttribute("page", page);
        System.out.println("page = " + page);
        //4 请求转发到pages/manager/book_manager.jsp 页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

最终massger_book分页功能:

<div id="page_nav">
  <c:choose>
        <c:when test="${requestScope.page.pageNo>1}">
            <a href="BookServlet?action=page&pageNo=1">首页</a>
            <a href="BookServlet?action=page&pageNo=${requestScope.page.pageNo-1}">上一页</a>
        </c:when>
        <c:otherwise>
            <a style="color: silver">首页</a>
            <a style="color: silver">下一页</a>
        </c:otherwise>
    </c:choose>
    <c:choose>
        <c:when test="${requestScope.page.pageTotal<=5}">
            <%--循环输出这些页码--%>
            <c:forEach begin="1" end="${requestScope.page.pageTotal}" var="i">
                <%--如果是当前页面,则显示【当前页码】--%>
                <c:if test="${i == requestScope.page.pageNo}">
                    <span>【${i}</span>
                </c:if>
                <%--如果不是当前页码则直接显示数字,并加上超链接实现可以跳转的功能--%>
                <c:if test="${i != requestScope.page.pageNo}">
                    <a href="BookServlet?action=page&pageNo=${i}">${i}</a>
                </c:if>
            </c:forEach>
        </c:when>
        <c:when test="${requestScope.page.pageTotal>5}">
            <c:choose>
                <%--页码在前三,显示1-5--%>
                <c:when test="${requestScope.page.pageNo<=3}">
                    <c:forEach begin="1" end="5" var="i">
                        <%--如果是当前页面,则显示【当前页码】--%>
                        <c:if test="${i == requestScope.page.pageNo}">
                            <span>【${i}</span>
                        </c:if>
                        <%--如果不是当前页码则直接显示数字,并加上超链接实现可以跳转的功能--%>
                        <c:if test="${i != requestScope.page.pageNo}">
                            <a href="BookServlet?action=page&pageNo=${i}">${i}</a>
                        </c:if>
                    </c:forEach>
                </c:when>
                <%--页码在后三,显示(Total-4)-Total--%>
                <c:when test="${requestScope.page.pageNo > requestScope.page.pageTotal-3}">
                    <c:forEach begin="${requestScope.page.pageTotal-4}" end="${requestScope.page.pageTotal}"
                               var="i">
                        <%--如果是当前页面,则显示【当前页码】--%>
                        <c:if test="${i == requestScope.page.pageNo}">
                            <span>【${i}</span>
                        </c:if>
                        <%--如果不是当前页码则直接显示数字,并加上超链接实现可以跳转的功能--%>
                        <c:if test="${i != requestScope.page.pageNo}">
                            <a href="BookServlet?action=page&pageNo=${i}">${i}</a>
                        </c:if>
                    </c:forEach>
                </c:when>
                <%--其余其他情况,显示(当前页码-2-(当前页码+2--%>
                <c:otherwise>
                    <c:forEach begin="${requestScope.page.pageNo-2}" end="${requestScope.page.pageNo+2}" var="i">
                        <%--如果是当前页面,则显示【当前页码】--%>
                        <c:if test="${i == requestScope.page.pageNo}">
                            <span>【${i}</span>
                        </c:if>
                        <%--如果不是当前页码则直接显示数字,并加上超链接实现可以跳转的功能--%>
                        <c:if test="${i != requestScope.page.pageNo}">
                            <a href="BookServlet?action=page&pageNo=${i}">${i}</a>
                        </c:if>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </c:when>
    </c:choose>
    <a class="aclass" href="BookServlet?action=page&pageNo=${requestScope.page.pageNo+1}">下一页</a>
    <a class="aclass" href="BookServlet?action=page&pageNo=${requestScope.page.pageTotal}">末页</a>
    <input type="hidden" id="pageNo" value="${requestScope.page.pageNo}"><span id="pageTotal">${ requestScope.page.pageTotal}</span>,${requestScope.page.pageTotalCount}条记录
    到第<input value="${requestScope.page.pageNo}" name="pn" id="pn_input"/><input type="button" value="确定" id="inputbtn">
</div>
<script type="text/javascript">
    $("#inputbtn").click(function () {
        location.href = "BookServlet?action=page&pageNo=" + $("#pn_input").val();
    });
    $(function () {
        var pageNo = parseInt($("#pageNo").val());
        var pageTotal = parseInt($("#pageTotal").html());
        if (pageNo >= pageTotal) {
            $("a.aclass").removeAttr("href").css("color", "silver");
        }
    });
</script>

你可能感兴趣的:(java,sql,开发语言,tomcat)