JSP中实现分页


一、findAllEmps(currentPage,pageSize)方法中分页查询的sql语句

String sql="select * from (select emp.*,rownum rn from emp where rownum<=currentPage*pageSize) where rn>=(currentPage-1)*pageSize+1";
然后返回查询结果rs以 List<Emp>给调用它的方法。

要查询第2页的内容,currentPage*pageSize=2*5=10       (currentPage-1)*pageSize+1=(2-1)*5+1=6

先到表中查询前10条记录,再查询第6条以后的记录,这样,记录很多时,效率比较高。


、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

二、jsp页面中改变当前页码的值,查询表中的一部分记录。


<%  
   int pageSize=5;
   int rowCount=empDao.getRowCount();   //查询表,计算总记录条数
   int pageCount=(rowCount/pageSize==0)?(rowCount/pageSize):(rowCount/pageSize+1); //计算可以分为多少页显示
   
   String currentPage_s=request.getParameter("currentPage");//reques获取当前页码
   int currentPage=1;       //初始化为第一页
   if(currentPage_s!=null){      
      currentPage=Integer.valueOf(currentPage_s);
   }
   if(currentPage<1){
      currentPage=1;
   }
   if(currentPage>pageCount){
      currentPage=pageCount;
   }
   //改变当前页的值,调用查询数据的方法,返回数据集合
   List<Emp> emplist=empDao.findAllEmps(currentPage,pageSize);
%>

页面中显示数据

 

 <body>
  <center>
    <table cellspacing="0" cellpadding="3" border="1" borderColor="#8080ff" width="80%">
  <tr align="center">
  <td>工号</td>
   <td>姓名</td>
    <td>职位</td>
     <td>上司</td>
      <td >雇佣日期</td>
       <td>工资</td>
        <td>奖金</td>
         <td>所属部门</td>
  </tr>
   <%for(int i=0;i<emplist.size();i++){ 
   out.print("<tr align='center'>");
  out.print("<td>"+emplist.get(i).getEmpno()+"</td>");
   out.print("<td>"+emplist.get(i).getEname()+"</td>");
    out.print("<td>"+emplist.get(i).getJob()+"</td>");
     out.print("<td>"+emplist.get(i).getMgr()+"</td>");
      out.print("<td>"+emplist.get(i).getHiredate()+"</td>");
       out.print("<td>"+emplist.get(i).getSal()+"</td>");
        out.print("<td>"+emplist.get(i).getComm()+"</td>");
         out.print("<td>"+emplist.get(i).getDeptno()+"</td>");
  out.println("</tr>");
  } 
  out.println("</table>");
  out.println("<br/>");
  //判断首页链接是否要显示
    if(currentPage>1){
    out.println("<a href=viewempByPage.jsp?currentPage="+1+" target='_self' >首页</a>");
    }
    else{
        out.println("首页");
    }
   //判断上一页链接是否要显示
    if(currentPage>1){
    out.println("  ");
    out.println("<a href=viewempByPage.jsp?currentPage="+(currentPage-1)+" target='_self' >上一页</a>");
    }
    else{
   out.println("  上一页");
   }
    for(int i=1;i<=pageCount;i++){
       if(i!=currentPage){ 
       out.println("  ");
       out.println("<a href=viewempByPage.jsp?currentPage="+i+" target='_self' >"+i+"</a>");
   }else{
      out.println("  "+i);
   }
   }
   //判断下一页链接是否要显示
   if(currentPage<pageCount){
      out.println("  ");
      out.println("<a href=viewempByPage.jsp?currentPage="+(currentPage+1)+" target='_self' >下一页</a>"); 
   }
   else{
      out.println("  下一页");
   }
  
   //判断末链接是否要显示
    if(currentPage<pageCount){
      out.println("  ");
      out.println("<a href=viewempByPage.jsp?currentPage="+pageCount+" target='_self' >末页</a>");
    }
    else{
      out.println("   末页");
   }
   %>
  </center>
  </body>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package com; public class Pager { private int totalRows = 0; // 记录总数 private int totalPages = 0; // 总页数 private int pageSize = 10; // 每页显示数据条数,默认为10条记录 private int currentPage = 1; // 当前页数 private boolean hasPrevious = false; // 是否有上一页 private boolean hasNext = false; // 是否有下一页 public int getSearchFrom() { return (currentPage - 1) * pageSize; } public Pager() { } public void init(int totalRows) { this.totalRows = totalRows; this.totalPages = ((totalRows + pageSize) - 1) / pageSize; refresh(); // 刷新当前页面信息 } /** * * @return Returns the currentPage. * */ public int getCurrentPage() { return currentPage; } /** * * @param currentPage * current page * */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; refresh(); } /** * * @return Returns the pageSize. * */ public int getPageSize() { return pageSize; } /** * * @param pageSize * The pageSize to set. * */ public void setPageSize(int pageSize) { this.pageSize = pageSize; refresh(); } /** * * @return Returns the totalPages. * */ public int getTotalPages() { return totalPages; } /** * * @param totalPages * The totalPages to set. * */ public void setTotalPages(int totalPages) { this.totalPages = totalPages; refresh(); } /** * * @return Returns the totalRows. * */ public int getTotalRows() { return totalRows; } /** * * @param totalRows * The totalRows to set. * */ public void setTotalRows(int totalRows) { this.totalRows = totalRows; refresh(); } // 跳到第一页 public void first() { currentPage = 1; this.setHasPrevious(false); refresh(); } // 取得上一页(重新设定当前页面即可) public void previous() { if (currentPage > 1) { currentPage--; } refresh(); } // 取得下一页 public void next() { //System.out.println("next: totalPages: "

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值