zoukankan      html  css  js  c++  java
  • 分页之页面页码列表计算

    分页之页面页码列表计算

    我们通过看百度的分页可以发现以下规律:

    1.最多显示10个页码;

    2.当前页在页码中的位置定为六;

    所以只需要当前页码来定出来页码列表,定下来页码列表只需要两个数据:begin,end;

    需要使用pc(当前页码)来推算出begin和end:

    begin=pc-5;

    end=pc+4;

    计算公式:

      如果总页数<=10(列表长度),那么begin=1,end=10;否则使用计算公式:begin=pc-5以及end=pc+4;但是这样也会导致头溢出和尾溢出。

           头溢出:当begin<1时,让begin=1,end=10;

           尾溢出:当end=${tp(总页数)}时,让end=tp;

    代码实现:

     1.首先要创建一个pagebean:

    package pages;
    
    import java.util.List;
    
    public class PageBean<T> {
        private int pc;//当前页码page code
    //    private int tp;//总页数=总记录数/每页记录数
        private int tr;//总记录数
        private int ps;//每页记录数
        private List<T> beanlist;//当前页的记录
        public int getPc() {
            return pc;
        }
        public void setPc(int pc) {
            this.pc = pc;
        }
        public int getTp() {
            int num=tr/ps;
            int tp=tr%ps;
            return tp==0?num:num+1;
        }
        public int getTr() {
            return tr;
        }
        public void setTr(int tr) {
            this.tr = tr;
        }
        public int getPs() {
            return ps;
        }
        public void setPs(int ps) {
            this.ps = ps;
        }
        public List<T> getBeanlist() {
            return beanlist;
        }
        public void setBeanlist(List<T> beanlist) {
            this.beanlist = beanlist;
        }
        @Override
        public String toString() {
            return "PageBean [pc=" + pc +  ", tr=" + tr + ", ps=" + ps + ", beanlist=" + beanlist + "]";
        }
    }

    2.servlet层:

    public String  findall(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
            /*
             * 1.获取页面传递的pc
             * 2.给定ps的值
             * 3.使用pc和ps调用service方法,得到pagebean对象,保存到request域
             * 4.转发到list.jsp
             */
            int pc=getpc(request);
            int ps=10;
            PageBean<customer> pb=customerservice.findall(pc,ps);
            request.setAttribute("pb", pb);
                return "f:/list.jsp";    
        }
        /*
         * 获取pc
         */
       public int getpc(HttpServletRequest request){
           String value=request.getParameter("pc");
           if(value==null||value.trim().isEmpty()){
               return 1;
           }
           return Integer.parseInt(value);
       }

    3.service层:

    public PageBean<customer> findall(int pc, int ps){
        return customerdao.findall(pc,ps);
    }

    4.dao层:

    /*
     * 查询所有
     */
    public PageBean<customer> findall(int pc, int ps){
        
        try {
            PageBean<customer> pb=new PageBean<customer>();
            pb.setPc(pc);
            pb.setPs(ps);
            /*
             * 得到tr
             */
            String sql="select count(*) from t_customers";
        Number trnum=(Number)qr.query(sql, new ScalarHandler());
        int tr=trnum.intValue();
        pb.setTr(tr);
        /*
         * 得到beanlist
         */
        String sql1="select * from t_customers order by cname limit ?,?";
         List<customer>  beanlist=qr.query(sql1, new BeanListHandler<customer>(customer.class),
                (pc-1)*ps,ps);
         pb.setBeanlist(beanlist);
         return pb;
        } catch (SQLException e) {
        throw new RuntimeException(e);
        }    
    }

    4.jsp页面的设置:

    第${pb.pc}页/供${pb.tp}页
        <center>    
            <a href="<c:url value='/customerServlet?method=findall&pc=1'/>">首页</a>
            <c:if test="${pb.pc>1}">
            <a href="<c:url value='/customerServlet?method=findall&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>
                <!--当总页数大于10时,按公式计算  -->
                <c:otherwise>
                    <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="<c:url value='/customerServlet?method=findall&pc=${i}'/>">[${i}]</a>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            <c:if test="${pb.pc<tp}">        
            <a href="<c:url value='/customerServlet?method=findall&pc=${pb.pc+1}'/>">下一页</a>
            </c:if>
            <a href="<c:url value='/customerServlet?method=findall&pc=${pb.tp}'/>">尾页</a>
        </center>
  • 相关阅读:
    用Python实现QQ找茬游戏外挂工具
    Python常用模块
    将Qt 动态链接生成的exe及依赖dll打包方法
    Qt之VLFeat SLIC超像素分割(Cpp版)
    android studio下的NDK开发详解(一)
    条件注释判断浏览器版本<!--[if lt IE 9]>
    人脸识别中的八大难题,何时能解
    人脸识别简史与近期进展
    openCV之头文件分析
    看(学习)代码流程
  • 原文地址:https://www.cnblogs.com/java-7/p/7832497.html
Copyright © 2011-2022 走看看