jsp分页实现

第一步分析我们做分页需要什么数据:

我们从两个方向来分析:1 页面方向,2 servlet方向

一 ,页面方向

1 当前页 currPageCode

2  总 页数 totalPage

3一页中的记录数据 datas

二,servlet方向

1 当前页 currPageCode

2  总页数 totalPage

3 每页记录数 pagesize

4 一共多少条记录 totalRecord

5 一页中的记录数据 datas

6当前页第一条记录的行数 currPageBeginIndex

7 url

 

[html] view plain copy

  1. public class PageBean<T> {  
  2.     private int cp;//当前页  
  3.       
  4.     private int pc;//可以通过每页记录数和共多少条记录得到 (dt%md ==0 )?dt/md : dt/md+1 一共多少页  
  5.       
  6.     private int md;//每页记录数  
  7.       
  8.     private List<T> pd;//页中的记录数据   
  9.       
  10.     private int dt;//一共多少条记录  
  11.       
  12.     private int cd;//可以通过每页记录是*(当前页数-1) + 1得到,当前页第一条记录的行数  
  13.       
  14.     private String url;  
  15. //为了清楚起见,我就没有粘上我的setget方法  
  16. }  

嗯,有了javabean了,那摩就先让我们写一下servlet吧!

 

[java] view plain copy

  1. public String findAll(HttpServletRequest request, HttpServletResponse response)  
  2.             throws ServletException, IOException {  
  3.           
  4.           
  5.          /* 
  6.           * 1 通过页面回传的当前页信息设置当前页 
  7.           * 2 设置每页所显示的数据 
  8.           * 3 调用servlce的findAll方法获得一共多少条记录及当前页的数据 
  9.           *  
  10.           */  
  11.          PageBean<Customer> pb = new PageBean<Customer>();  
  12.            
  13.          if(request.getParameter("cp") != null && !request.getParameter("cp").trim().isEmpty()) {  
  14.              int cp = Integer.parseInt(request.getParameter("cp"));  
  15.              pb.setCp(cp);  
  16.          }    
  17.          else {  
  18.              pb.setCp(1);  
  19.          }  
  20.          pb.setMd(10);  
  21.          pb.setPd(service.findAll(pb.getCd(),pb.getMd()));  
  22.          pb.setDt(service.count());  
  23.          String url = getUrl(request);  
  24.          pb.setUrl(url);  
  25.          request.setAttribute("pb",pb);  
  26.          return "f:list.jsp";  
  27.     }  


嗯,有了数据了,这个方法会转发找list.jsp页面中,我们来看该页的技术点

 

[html] view plain copy

  1. <body>  
  2.     <h3 align="center">客户列表</h3>  
  3.     <table border="1" width="70%" align="center">  
  4.         <tr>  
  5.             <th>客户姓名</th>  
  6.             <th>性别</th>  
  7.             <th>生日</th>  
  8.             <th>手机</th>  
  9.             <th>邮箱</th>  
  10.             <th>描述</th>  
  11.             <th>操作</th>  
  12.         </tr>  
  13.           
  14.         <!-- 第几页                             current page  cp  
  15.         共几页                         page count   pc  
  16.         一页显示多少数据                many date md  
  17.         每页所显示的数据                page date pd  
  18.         一共多少条记录                 date total  dt  
  19.         当前页的第一条记录的行数        current date cd -->  
  20.           
  21.           
  22.         <!-- 1,这一点就不用多说了吧,我们把我们查找的数据进行遍历 -->  
  23.         <c:forEach var="customer" items="${pb.pd}">  
  24.             <tr>  
  25.             <!-- private String cname;         
  26.                 private String gender;  
  27.                 private String birthday;      
  28.                 private String cellphone;     
  29.                 private String email;             
  30.                 private String description;   
  31.             -->  
  32.                 <td>${customer.cname }</td>  
  33.                 <td>${customer.gender }</td>  
  34.                 <td>${customer.birthday}</td>  
  35.                 <td>${customer.cellphone }</td>  
  36.                 <td>${customer.email }</td>  
  37.                 <td>${customer.description }</td>  
  38.                 <td>  
  39.                     <a href="<c:url value='/customer2?method=forEdit&cid=${customer.cid }'/>">编辑</a>  
  40.                     <a href="<c:url value='/customer2?method=delete&cid=${customer.cid }'/>">删除</a>  
  41.                 </td>  
  42.             </tr>  
  43.         </c:forEach>  
  44.     </table>  
  45.     <!-- 这是我们分页技术页面部分的实现; -->  
  46.     <center>  
  47.       
  48.         第${requestScope.pb.cp }页/共 ${pb.pc }页  
  49.           
  50.         <a href="<c:url value="${pb.url }"/>">首页</a>   <!--  因为首页也是第一页,所以我们没有加当前页码参数,servlet会自动认为是第一页 -->  
  51.           
  52.         <c:if test="${pb.cp > 1 }">        <!-- 如果当前页面是第一页德华,那摩就禁用上一页 -->  
  53.             <a href="<c:url value='${pb.url }&cp=${pb.cp - 1 }'/>">上一页</a>  
  54.         </c:if>  
  55.           
  56.         <%--   
  57.             用来进行中间页码的显示及使用超链接所带来的的问题的解决方案  
  58.             如何显示页码呢,我们其实只需要两个数据,begin和end就哦了,  
  59.             我们常见的百度页面有什么特点呢,你可以去看看我就不多说了;下面我给出他的计算公式:  
  60.                 如果总页数<=10(列表长度),那么begin=1end=总页数  
  61.                 否则  
  62.                 使用公式计算;begin=pc-5, end=pc + 4;    
  63.                     两种特殊情况:  
  64.                     头溢出:当begin<1时,让begin=1  
  65.                     尾溢出:当end>${tp}时,让end=${tp}  
  66.          --%>  
  67.   
  68.         <c:choose>  
  69.             <c:when test="${pb.pc <= 10 }">             <!--  判断总页数小于页面大小吗 -->  
  70.                 <c:set var="begin" value="1"/>  
  71.                 <c:set var="end" value="${pb.pc }"/>  
  72.             </c:when>  
  73.             <c:otherwise>                               
  74.                 <c:set var="begin" value="${pb.cp-4 }"/>  
  75.                 <c:set var="end" value="${pb.cp+5 }"/>  
  76.                 <c:choose>  
  77.                     <c:when test="${begin < 1 }">  
  78.                         <c:set var="begin" value="1"/>  
  79.                         <c:set var="end" value="10"/>               
  80.                     </c:when>  
  81.                     <c:when test="${end > pb.pc }">  
  82.                         <c:set var="begin" value="${pb.pc-9 }"/>  
  83.                         <c:set var="end" value="${pb.pc }"/>            
  84.                     </c:when>  
  85.                 </c:choose>  
  86.             </c:otherwise>      
  87.         </c:choose>  
  88.         <!-- 通过上面的操作我们可以得到正确的begin和end了下面我们就对其进行遍历 -->  
  89.           
  90.         <c:forEach begin="${begin }" end="${end }" var="i">  
  91.             <c:choose>  
  92.                 <!-- 当前页面显示为普通文本 -->  
  93.                 <c:when test="${i == pb.cp }">${i }</c:when>  
  94.                 <!-- 其他页面显示为超链接 -->  
  95.                 <c:otherwise><a href="<c:url value='${pb.url }&cp=${i }'/>"> [${i }] </a></c:otherwise>  
  96.             </c:choose>  
  97.         </c:forEach>  
  98.           
  99.           
  100.           
  101.           
  102.           
  103.         <c:if test="${pb.cp < pb.pc }">  <!-- 如果当前页面时最后一页的话,我们就禁用下一页 -->  
  104.             <a href="<c:url value='${pb.url }&cp=${pb.cp + 1 }'/>">下一页</a>  
  105.         </c:if>  
  106.         <a href="<c:url value='${pb.url }&cp=${pb.pc }'/>">尾页</a>  
  107.     </center>  
  108.   </body>  

哦了多余的话我就不解释了,我想你看过这段代码之后会明白的吧。

 

那摩接下来让我们来写一个service和dao就算完事了,service我就不说了我们直接来看dao吧

 

[java] view plain copy

  1. public List<Customer> findAll(int index,int length) {  
  2.         String sql  = "select * from t_customer limit ?,?";  
  3.         List<Customer> l = null;  
  4.         Object[] p = {index,length};  
  5.         try {  
  6.             l = queryRunner.query(sql, new BeanListHandler<Customer>(Customer.class),p);  
  7.         } catch (SQLException e) {  
  8.             // TODO Auto-generated catch block  
  9.             e.printStackTrace();  
  10.         }  
  11.         return l;  
  12.     }  

[java] view plain copy

  1. public int count() {  
  2. <span style="white-space:pre;">     </span>String sql = "select count(*) from t_customer";  
  3. <span style="white-space:pre;">     </span>Number n = null;  
  4. <span style="white-space:pre;">     </span>try {  
  5. <span style="white-space:pre;">         </span>n = (Number)queryRunner.query(sql, new ScalarHandler());  
  6. <span style="white-space:pre;">         </span>  
  7. <span style="white-space:pre;">     </span>} catch (SQLException e) {  
  8. <span style="white-space:pre;">         </span>// TODO Auto-generated catch block  
  9. <span style="white-space:pre;">         </span>e.printStackTrace();  
  10. <span style="white-space:pre;">     </span>}  
  11. <span style="white-space:pre;">     </span>return n.intValue();  
  12. <span style="white-space:pre;"> </span>}  

 

是滴,全部查找就是这摸简单,只要还是得利用当前行数,和每页记录数

 

哦了

有人会问我getUrl()方法是什么鬼,还有encoding()方法又是你妈什么?

应为我们是使用get提交的所以会造成乱码,嗯对,我们获取的customer对象他爹都不认识他,我们的encoding方法就是用来进行解码的

 

[java] view plain copy

  1. //这是其中的一个,其他的属性也如同  
  2. if(c.getCname() != null && !c.getCname().trim().isEmpty()) {  
  3.             byte[] b = null;  
  4.             try {  
  5.                 b = c.getCname().getBytes("iso-8859-1");  
  6.             } catch (UnsupportedEncodingException e) {  
  7.                 // TODO Auto-generated catch block  
  8.                 e.printStackTrace();  
  9.             }  
  10.             String name=null;  
  11.             try {  
  12.                 name = new String(b,"utf-8");  
  13.             } catch (UnsupportedEncodingException e) {  
  14.                 // TODO Auto-generated catch block  
  15.                 e.printStackTrace();  
  16.             }  
  17.             c.setCname(name);  
  18.         }  

 

getUrl是用来获取参数路径并去除&cp参数的:

 

[java] view plain copy

  1. private String getUrl(HttpServletRequest request) {  
  2.         //String c = request.getContextPath();  
  3.         String q = request.getQueryString();  
  4.         String s = request.getServletPath();  
  5.           
  6.         if(q != null && !q.trim().isEmpty()) {  
  7.             if( q.contains("&cp=")) {  
  8.                 int i = q.indexOf("&cp=");  
  9.                 q = q.substring(0, i);  
  10.             }  
  11.         }  
  12.         String url =  s + "?" + q;  
  13.         return url;  
  14.     }  

 


哦了,大致的技术点我们就鸡巴说没了,让我们提升点难度,试试多条件查询:

 

 

[java] view plain copy

  1. //多条件查询的servlet方法  
  2. public String query(HttpServletRequest request, HttpServletResponse response)  
  3.             throws ServletException, IOException {  
  4.           
  5.         Customer customer = new Customer();  
  6.           
  7.         try {  
  8.             BeanUtils.populate(customer,request.getParameterMap());  
  9.         } catch (IllegalAccessException e) {  
  10.             // TODO Auto-generated catch block  
  11.             e.printStackTrace();  
  12.         } catch (InvocationTargetException e) {  
  13.             // TODO Auto-generated catch block  
  14.             e.printStackTrace();  
  15.         }  
  16.           
  17.         Customer c = encoding(customer);  
  18.           
  19.           
  20.         PageBean<Customer> pb = new PageBean<Customer>();  
  21.           
  22.            
  23.          if(request.getParameter("cp") != null && !request.getParameter("cp").trim().isEmpty()) {  
  24.              int cp = Integer.parseInt(request.getParameter("cp"));  
  25.              pb.setCp(cp);  
  26.          }    
  27.            
  28.          else {  
  29.              pb.setCp(1);  
  30.          }  
  31.            
  32.          pb.setMd(10);  
  33.            
  34.          pb.setPd(service.query(customer,pb.getCd(),pb.getMd()));  
  35.            
  36.          pb.setDt(service.count());  
  37.            
  38.          String url = getUrl(request);  
  39.            
  40.          pb.setUrl(url);  
  41.            
  42.          request.setAttribute("pb",pb);  
  43.            
  44.          return "f:list.jsp";  
  45.     }  


多条件查询的dao

 

[java] view plain copy

  1. public List<Customer> query(Customer customer ,int index,int length) {  
  2.         //我们将其参数放入ArrayList中  
  3.         List<Object> l = new ArrayList();  
  4.         //我们使用StringBuffer连接我们所需要的条件  
  5.         StringBuffer sql = new StringBuffer  
  6.                 ("select * from t_customer where 1=1 ");  
  7.           
  8.         if(customer.getCname() != null && !customer.getCname().trim().isEmpty()) {  
  9.             sql.append("and cname=? ");  
  10.             l.add(customer.getCname());  
  11.         }  
  12.           
  13.         if(customer.getBirthday() != null && !customer.getBirthday().trim().isEmpty()) {  
  14.             sql.append("and birthday=? ");  
  15.             l.add(customer.getBirthday());  
  16.         }  
  17.           
  18.         if(customer.getCellphone() != null && !customer.getCellphone().trim().isEmpty()) {  
  19.             sql.append("and cellphone=? ");  
  20.             l.add(customer.getCellphone());  
  21.         }  
  22.           
  23.         if(customer.getDescription() != null && !customer.getDescription().trim().isEmpty()) {  
  24.             sql.append("and description=? ");  
  25.             l.add(customer.getDescription());  
  26.         }  
  27.           
  28.         if(customer.getEmail() != null && !customer.getEmail().trim().isEmpty()) {  
  29.             sql.append("and email=? ");  
  30.             l.add(customer.getEmail());  
  31.         }  
  32.           
  33.         if(customer.getGender() != null && !customer.getGender().trim().isEmpty()) {  
  34.             sql.append("and gender=? ");  
  35.             l.add(customer.getGender());  
  36.         }  
  37.         sql.append("limit ?,?");  
  38.         l.add(index);  
  39.         l.add(length);  
  40.           
  41.         List<Customer> lc= null;  
  42.         try {  
  43.             lc = queryRunner.query(sql.toString(), new BeanListHandler<Customer>(Customer.class), l.toArray());  
  44.         } catch (SQLException e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.         return lc;  
  49.     }  

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值