①分页效果:

分页(完整版)
分页(完整版)

②分页PageBean模型设计

③Page.jsp公用分页页面

(使用方法:在要分页的页面利用<%@ include file" " %>静态包含此页面,注意此分页跳转链接中带了“分页条件,根据自己“分页条件需求”作出更改即可)
1
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3
    <!-- 如果查询出的数据总条数为0,则不显示分页 -->
4
    <c:if test="${not empty pb.totalPageCounts }">
5
        <!-- 如果当前页为1,则不显示首页,否则显示首页 -->
6
        <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=1&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==1?"":"首页" }</a>&nbsp;&nbsp;&nbsp;
7
        
8
        <!-- 如果当前页为1,则不显示上一页,否则显示上一页 -->
9
        <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=${(pb.currentPageNumber-1)<=0?1:pb.currentPageNumber-1}&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==1?'':'上一页' }</a>&nbsp;&nbsp;&nbsp;
10
        <!-- 若左端还有页码,则显示省略号,有上一页功能 -->
11
        <c:if test="${pb.beginPageNumber>1 }">
12
            <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=${pb.currentPageNumber-1}&categoryId=${sessionScope.categoryId}" style="text-decoration: none;"><strong>...&nbsp;&nbsp;</strong></a>
13
        </c:if>
14
        <!-- 显示数字页码,当前页数字显示红色 -->
15
        <c:forEach begin="${pb.beginPageNumber }" end="${pb.endPageNumber }" var="count">
16
            <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=${count}&categoryId=${sessionScope.categoryId}"><span style="${pb.currentPageNumber==count?'color:red':''}">${count }</span></a>&nbsp;&nbsp;&nbsp;
17
        </c:forEach>
18
        <!-- 若右端还有页码,则显示省略号 ,有下一页功能 -->
19
        <c:if test="${pb.endPageNumber<pb.totalPageCounts }">
20
            <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=${pb.currentPageNumber+1}&categoryId=${sessionScope.categoryId}" style="text-decoration: none;"><strong>...&nbsp;&nbsp;</strong></a>
21
        </c:if>
22
        
23
        <!-- 如果当前页为总页数,则不显示下一页,否则显示下一页 -->
24
        <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=${(pb.currentPageNumber+1)>pb.totalPageCounts?pb.totalPageCounts:pb.currentPageNumber+1}&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==pb.totalPageCounts?'':'下一页' }</a>&nbsp;&nbsp;&nbsp;
25
        <!-- 如果当前页为总页数,则不显示尾页,否则显示尾页 -->
26
        <a href="${pageContext.request.contextPath }${pb.pageUri }&currentPageNumber=${pb.totalPageCounts }&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==pb.totalPageCounts?'':'尾页' }</a>&nbsp;&nbsp;&nbsp;
27
        
28
        <!-- 输入框只能输入不为0的数字,为空或者为不存在的页码则还是显示当前页 -->
29
        <input type="text" id="jumpPageNum" size="1" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"/>&nbsp;&nbsp;<a href="#" onclick="jumpPageNum(this);">跳转</a>&nbsp;&nbsp;&nbsp;
30
        当前第<span style="color:red">${pb.currentPageNumber }</span>页/共<span style="color:red">${pb.totalPageCounts }</span>&nbsp;<span style="color:red">${pb.totalRecordCounts }</span>条数据&nbsp;&nbsp;
31
        
32
        <!-- 改变每页显示的数据条数 -->
33
        每页显示<select name="pageSize" onchange="changePageSize(this)">
34
            <option value="5"  ${sessionScope.pageSize==5?"selected='selected'":"" }>5</option>
35
            <option value="10" ${sessionScope.pageSize==10?"selected='selected'":"" }>10</option>
36
            <option value="15" ${sessionScope.pageSize==15?"selected='selected'":"" }>15</option>
37
        </select>条数据
38
    </c:if>
39
    <script type="text/javascript">
40
        //跳转框输入数字,跳转时调用
41
        function jumpPageNum(node){
42
            var jumpPageNum = document.getElementById("jumpPageNum").value;
43
            if(jumpPageNum==null || jumpPageNum==""){//如果没有输入跳转页
44
                jumpPageNum = "${pb.currentPageNumber}";//保持当前页
45
            }
46
            var totalPageCounts = "${pb.totalPageCounts}";
47
            if(jumpPageNum<=0){
48
                jumpPageNum = "${pb.currentPageNumber}";//保持当前页 
49
            }
50
            /*注意js中
51
                ①数字和字符串比较:javascript都会把字符串转换成数字,然后按照数字顺序比较它们。
52
                ②字符串与字符串的比较:
53
                    情况1:能找到对应位置上的不同字符,那么就比较第一个不同字符的大小。
54
                    情况2:不能找到对应位置上的不同字符,这时候比较的是两个字符串的长度。
55
            */
56
            if(parseInt(jumpPageNum)>parseInt(totalPageCounts)){//这里需要将字符串转换成数字,才能正确比较
57
                jumpPageNum = "${pb.currentPageNumber}";//保持当前页 
58
            } 
59
            node.href = "${pageContext.request.contextPath }${pb.pageUri }&categoryId=${sessionScope.categoryId}&currentPageNumber="+jumpPageNum;
60
        }
61
        
62
        //改变每页显示的数据条数
63
        function changePageSize(node){
64
            var pageSize = node.value;
65
            //注意每次改变每页显示数据条数,当前页不能作为参数代入,只要更改了每页显示条数,就必须从第一页查起,因为假如之前当前页是第5页,这样数据库查询时起始索引为(当前页-1)*每页显示条数,若结果过大,这样很可能查询不到数据。
66
            window.location.href="${pageContext.request.contextPath }${pb.pageUri }&categoryId=${sessionScope.categoryId}&pageSize="+pageSize;
67
        }
68
    </script>
69

④分页Servlet

⑤分页Service

1
@Override
2
    public PageBean<Book> findPageBooksByCategory(String currentPageNumber,
3
            String categoryId, String pageSize) {
4
        /**
5
         * 设置当前页页码
6
         */
7
        int number = 1;//默认为第一页
8
        //判断要显示的当前页码
9
        if(currentPageNumber!=null && !"".equals(currentPageNumber)){
10
            number = Integer.valueOf(currentPageNumber);
11
        }
12
        int totalRecordCounts = 0;
13
        if(categoryId==null || categoryId=="" || categoryId.hashCode()==0){//不按分类
14
            //根据获取所有书总记录数
15
            totalRecordCounts = bDao.getTotalRecordCounts();
16
        }else{
17
            //根据图书种类,获取记录数
18
            totalRecordCounts = bDao.getTotalRecordCounts(categoryId);
19
        }
20
        //创建PageBean对象
21
        PageBean<Book> pb = new PageBean<Book>(number, totalRecordCounts, Integer.valueOf(pageSize));
22
23
        //调用dao,按图书种类获取图书
24
        List<Book> bs = null;
25
        //TODO 分析 == null || ==""    和  !=null  && !=""
26
        if(categoryId==null || categoryId=="" || categoryId.hashCode()==0){//不按分类
27
             bs = bDao.findPageBooks(pb.getStartIndex(), pb.getPageSize());
28
        }else{
29
            bs = bDao.findPageBooksByCategory(pb.getStartIndex(), pb.getPageSize(), categoryId);
30
        }
31
        //封装到PageBean对象中
32
        pb.setPageList(bs);
33
        return pb;
34
    }

⑥分页Dao

1
@Override
2
    public int getTotalRecordCounts(String categoryId) {
3
        try{
4
            //sql语句
5
            String sql = "select count(*) from book where category_id = ?";
6
            return qr.query(sql, new ScalarHandler<Long>(), categoryId).intValue();
7
        }catch(Exception e){
8
            throw new DaoRuntimeException(e);
9
        }
10
    }
11
12
    @Override
13
    public List<Book> findPageBooksByCategory(int startIndex, int pageSize,
14
            String categoryId) {
15
        try{
16
            //sql语句
17
            String sql = "select id, bName, author, price, image, bDescription, category_id from book where category_id = ? limit ?, ?";
18
            return qr.query(sql, new BeanListHandler<Book>(Book.class), categoryId, startIndex, pageSize);
19
        }catch(Exception e){
20
            throw new DaoRuntimeException(e);
21
        }
22
    }

相关文章: