• jsp分页代码之pageUtil类


    pageUtil类负责得到每页的开始数和结束数

    package control;
    
    public class PageUtil {
    	  private int pageSize;//每页显示的条数 
    	    private int recordCount;//总共的条数 
    	    private int currentPage;//当前页面 
    	    public PageUtil(int pageSize, int recordCount, int currentPage){ 
    	            this.pageSize = pageSize; 
    	            this.recordCount = recordCount; 
    	            setCurrentPage(currentPage); 
    	        } 
    	         //构造方法 
    	        public PageUtil(int pageSize, int recordCount) { 
    	            this(pageSize, recordCount, 1); 
    	        } 
    	        //总页数 
    	        public int getPageCount() { 
    	            int size = recordCount/pageSize;//总条数/每页显示的条数=总页数 
    	            int mod = recordCount%pageSize;//最后一页的条数 
    	            if(mod != 0) 
    	                size++; 
    	            return recordCount == 0 ? 1 : size; 
    	        } 
    	        //包含,起始索引为0 
    	        public int getFromIndex() { 
    	            //System.out.println("from index:"+(currentPage-1) * pageSize); 
    	            return (currentPage-1) * pageSize; 
    	        } 
    	        //不包含 
    	        public int getToIndex() { 
    	            //System.out.println("to index:"+Math.min(recordCount, currentPage * pageSize)); 
    	            return  Math.min(recordCount, currentPage * pageSize); 
    	        } 
    	        //得到当前页 
    	        public int getCurrentPage(){ 
    	            return currentPage; 
    	        }//设置当前页 
    	        public void setCurrentPage(int currentPage) { 
    	            int validPage = currentPage <= 0 ? 1 : currentPage; 
    	            validPage = validPage > getPageCount() ? getPageCount() : validPage; 
    	            this.currentPage = validPage; 
    	        }//得到每页显示的条数 
    	        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; 
    	        } 
    }
    

     下面的代码是放在jsp的

    <%
    PublishersDAO dao = PublishersDAO.getInstance();
    List records = dao.getModels();
    String pageStr = request.getParameter("page");
    int currentPage = 1;
    if (pageStr != null)
    currentPage = Integer.parseInt(pageStr);
    PageUtil pUtil = new PageUtil(10, records.size(), currentPage);
    currentPage = pUtil.getCurrentPage();
    %>
    

      下面的代码是放在变量的上面

    <%
    for (int i = pUtil.getFromIndex(); i < pUtil.getToIndex(); i++) {
    PublisherModel model = (PublisherModel) records.get(i);

    out.print("<TD>"+model.getA()+"</TD>");
    out.print("<TD>"+model.getB()+"</TD>");

    %>
    中间是显示的代码
    <%}%>

      

    ----------这个是结尾的

    <tr><td width=100% bgcolor="#eeeeee" colspan=4 align="center"> 
    记录总数<%=pUtil.getRecordCount()%>条 当前页/总页数<%=currentPage%> 
    /<%=pUtil.getPageCount()%>每页显示<%=pUtil.getPageSize()%>条 
    <a href="showTodolist.jsp?page=1">首页</a> 
    <a href="showTodolist.jsp?page=<%=(currentPage - 1)%>">上页</a> 
    <a href="showTodolist.jsp?page=<%=(currentPage + 1)%>">下页</a> 
    <a href="showTodolist.jsp?page=<%=pUtil.getPageCount()%>">末页</a> 
    </td></tr> 
    

      

  • 相关阅读:
    常用设计模式
    文件上传相关报错: The current request is not a multipart request或is a MultipartResolver configured?
    Intellij IDEA 与 Gitlab 实现代码上传与下载
    Oracle两表关联,只取B表的第一条记录
    notepad++ 调整行间距
    Ubuntu18.04直接安装python3.7或者升级自带的python3.6版本之后导致终端无法打开的解决办法
    黑苹果之DELL台式机安装Mac OS X 10.13.6版本操作系统
    Windows Ping | Tracert 's Bat 脚本并行测试
    centos 7 修改 sshd | 禁止 root 登录及 sshd 端口脚本定义
    C语言中malloc函数的理解
  • 原文地址:https://www.cnblogs.com/zzlp/p/5169070.html
Copyright © 2020-2023  润新知