• 实用分页小方法


    1 编辑一个class:包含当前页面、总条数、每页显示的条数的属性和构造方法。

      具体代码如下:

    public class PageUtilServices {
        private int pageSize;//每页显示的条数
        private int recordCount;//总共的条数
        private int currentPage;//当前页面
        //构造方法
        public PageUtilServices(int pageSize, int recordCount, int currentPage) {
            super();
            this.pageSize = pageSize;
            this.recordCount = recordCount;
            this.currentPage = currentPage;
        }
        public PageUtilServices() {
            super();
        }
          
            //总页数
            public int getPageCount() {
                int size = recordCount/pageSize;//总条数/每页显示的条数=总页数
                int mod = recordCount % pageSize;//最后一页的条数
                if(mod != 0)
                    size++;
                return recordCount == 0 ? 1 : size;
            }
            //当前页的起始索引为0
            public int getFromIndex() {
                return (currentPage-1) * pageSize;
            }
            //当前页应展示的最后索引
            public int getToIndex() {
                return  Math.min(recordCount, currentPage * pageSize);
            }
           
            public int getCurrentPage() {
                return currentPage;
            }
            public void setCurrentPage(int currentPage) {
               
                this.currentPage = currentPage;
            }
            public int getPageSize() {
                return pageSize;
            }
            public void setPageSize(int pageSize) {
                this.pageSize = pageSize;
            }
            public int getRecordCount() {
                return recordCount;
            }
            public void setRecordCount(int recordCount) {
                this.recordCount = recordCount;
            }
    }

    2.在jsp内实例化该class,并调用相关属性和方法。具体代码如下:

      
         <% BookInfoDAO  dao=new BookInfoDAO();
           SimpleDateFormat sdf=new  SimpleDateFormat("yyyy-MM-dd");
           List<Book> books=dao.getAllBook();
           
           
           String pageStr = request.getParameter("page");
          int  current_size=10;
          int currentPage = 1;
          if (pageStr==null){  
              currentPage = 1;
          }else if(Integer.parseInt(pageStr)<=0){
              currentPage = 1;
          }else if(Integer.parseInt(pageStr)>books.size()/current_size){
              currentPage =books.size()/current_size+1;
          }else{
              currentPage=Integer.parseInt(pageStr);
          }
          PageUtilServices pUtil = new PageUtilServices(current_size, books.size(), currentPage);
          currentPage = pUtil.getCurrentPage();    %>
        <table border="1px">
           <tr><td>bookId</td>
               <td>Name</td>
               <td>author</td>
               <td>price</td>
               <td>date</td>
              
               <td>修改</td>
               <td>删除</td>
          </tr>
          <%for(int i = pUtil.getFromIndex(); i < pUtil.getToIndex();i++){ %>
          <tr><td><%=books.get(i).getBookId()%></td>
              <td><%=books.get(i).getName() %></td>
              <td><%=books.get(i).getAuthor() %></td>
              <td><%=books.get(i).getPrice() %></td>
              <td><%=sdf.format(books.get(i).getDate()) %></td>
          
              <td><a href="servlet/do_upDateBookServlet?id=<%=books.get(i).getBookId() %>">修改</a></td>
              <td><a href="servlet/do_delBookServlet?id=<%=books.get(i).getBookId() %>">删除</a></td>
            
                 
          </tr>
      <%}%>
      <tr><td bgcolor="#eeeeee" colspan=13 align="center">
                        记录总数<%=pUtil.getRecordCount()%>条 当前页/总页数<%=currentPage%>
                        /<%=pUtil.getPageCount()%> 每页显示<%=pUtil.getPageSize()%><a href="bookList.jsp?page=1">首页</a>
                        <a href="bookList.jsp?page=<%=(currentPage - 1)%>">上页</a>
                        <a href="bookList.jsp?page=<%=(currentPage + 1)%>">下页</a>
                        <a href="bookList.jsp?page=<%=pUtil.getPageCount()%>">末页</a>
                        </td></tr>
        </table>

    红线部分表示该class的具体使用地方。页面效果:

  • 相关阅读:
    android 75 新闻列表页面
    android 74 下载文本
    android 73 下载图片
    android 72 确定取消对话框,单选对话框,多选对话框
    android 71 ArrayAdapter和SimpleAdapter
    android 70 使用ListView把数据显示至屏幕
    maven如何将本地jar安装到本地仓库
    Centos6.7搭建ISCSI存储服务器
    解决maven打包编译出现File encoding has not been set问题
    MySQL 解决 emoji表情 的方法,使用utf8mb4 字符集(4字节 UTF-8 Unicode 编码)
  • 原文地址:https://www.cnblogs.com/wei1228565493/p/4131106.html
Copyright © 2020-2023  润新知