zoukankan      html  css  js  c++  java
  • 分页技术

    分页

    1 什么分页

    第N页/共M页 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 尾页 口go

      分页的优点:只查询一页,不用查询所有页!

    2 分页数据

    页面的数据都是由Servlet传递来的!

    Servlet:

    l  当前面:pageCode,pc;

    • pc:如果页面没有传递当前页码,那么Servlet默认是第一页,或者按页面传递的来准!

    l  总页数:totalPages,tp

    • tp:总记录数/每页记录数

    l  总记录数:totalRecored,tr

    • tr:dao来获取,select count(*) from t_customer;

    l  每页记录数:业务数据或叫系统数据!10行!

    l  当前页数据:beanList

    l  url

    3 分页Bean的设计

    这些分页数据总要在各层之间来回的传递!

    我们把这些分页数据封装到一个javabean中,它就叫分页Bean,例如:PageBean

    PageBean.java

    public class PageBean<T> {
        private int pc;// 当前页码page code
    //    private int tp;// 总页数total page
        private int tr;// 总记录数total record
        private int ps;// 每页记录数page size
        private List<T> beanList;// 当前页的记录
    

    4 分页在各层中的处理

    l  页面:给出分页相关的链接们!

    • 页面需要给Servlet传递什么:有可能传递pc

    l  Servlet:创建PageBean对象,给PageBean所有的属性赋值,然后传递给页面;

    • Servlet需要给DAO传递pc、ps

    l  Service:略

    l  Dao:

    • tr:select count(*) t_customer
    • beanList:select * from t_customer limit x, y

    5 显示分页页码列表

    1 2 3 4 5 6 7 8 9 10

    l  最多显示多少个页码!定为10;

    l  当前页,在页码列表中的位置,定为6;

    只需要当前页码来定出来页码列表!

    定下来页码列表只需要两样数据:

    l  begin

    l  end

    10 11 12 13 14 (15) 16 17 18 19

    需要使用pc来推算出begin和end

    begin = pc – 5

    end = pc + 4

    计算公式:

    l  如果总页数<=10(列表长度),那么begin=1,end=总页数

    l  使用公式计算;begin=pc-5, end=pc + 4;

    l  头溢出:当begin<1时,让begin=1

    l  尾溢出:当end>${tp}时,让end=${tp}

    <%-- 
    给出分页相差的链接
     --%>
    <center>
    第${pb.pc }页/共${pb.tp }页
    
    <a href="${pb.url }&pc=1">首页</a>
    <c:if test="${pb.pc > 1 }">
    <a href="${pb.url }&pc=${pb.pc-1}">上一页</a>
    </c:if>
    
    <%-- 计算begin、end --%>
    <c:choose>
        <%-- 如果总页数不足10页,那么把所有的页数都显示出来! --%>
        <c:when test="${pb.tp <= 10 }">
            <c:set var="begin" value="1" />
            <c:set var="end" value="${pb.tp }" />
        </c:when>
        <c:otherwise>
            <%-- 当总页数>10时,通过公式计算出begin和end --%>
            <c:set var="begin" value="${pb.pc-5 }" />
            <c:set var="end" value="${pb.pc+4 }" />    
            <%-- 头溢出 --%>
            <c:if test="${begin < 1 }">
                <c:set var="begin" value="1" />
                <c:set var="end" value="10" />
            </c:if>    
            <%-- 尾溢出 --%>
            <c:if test="${end > pb.tp }">
                <c:set var="begin" value="${pb.tp - 9 }" />
                <c:set var="end" value="${pb.tp }" />
            </c:if>    
        </c:otherwise>
    </c:choose>
    <%-- 循环遍历页码列表 --%>
    <c:forEach var="i" begin="${begin }" end="${end }">
        <c:choose>
            <c:when test="${i eq pb.pc }">
                [${i }]
            </c:when>
            <c:otherwise>
                <a href="${pb.url }&pc=${i}">[${i }]</a>    
            </c:otherwise>
        </c:choose>
        
    </c:forEach>
    
    
    <c:if test="${pb.pc < pb.tp }">
    <a href="${pb.url }&pc=${pb.pc+1}">下一页</a>
    </c:if>
    <a href="${pb.url }&pc=${pb.tp}">尾页</a>

    6. 在超链接中要保留参数

    当使用多条件查询后,然后在点击第2 页时,这个第2页超链接没有条件了,所以会丢失条件,所以我们需要在页面上的所有链接都要保留条件!

    我们要把条件以一个字符串的形式保存到PageBean的url中!这个任务交给Servlet!

  • 相关阅读:
    创建被访问的swf文件
    BFS寻路算法的实现
    Flex里的命名空间,fx、mx、s【转】
    Flex的基础用法【转】
    Pow(x, n)
    Roman to Integer
    Integer to Roman
    Divide Two Integers
    Single Number II
    Single Number I
  • 原文地址:https://www.cnblogs.com/lxp503238/p/7058211.html
Copyright © 2011-2022 走看看