仿baidu,google的查询分页技术

 在上一节中我们简单观察与分析了仿baidu,google的查询分页技术,有很多学生纷纷的给我来了邮件,期待着使用Java技术的实现.在这里很感谢大家对我的关注,我会一如既往的在此平台之上发布自己的授课中一些知识点的总结.下将仿baidu,google查询分页技术的实现方式一种分享给大家,如有问题请及时发送邮件.希望同学们在这新的学期中有更大的收获。

在这里我简单说说我的实现思路,我将整个分页的技术全部封装在了一个Pagination的JavaBean中.具体代码如下:
Java代码
  1. package cn.csdn.util;   
  2.   
  3. import java.util.List;   
  4.   
  5. /**  
  6.  *   
  7.  * @author redarmy_chen  
  8.  *  
  9.  * @param <T>  
  10.  */  
  11. public class Pagination<T> {   
  12.   
  13.     // 分页信息   
  14.     private int nowpage;// 当前页   
  15.     private int countrecord;// 总记录   
  16.     private int countpage;// 总页数   
  17.   
  18.     public static final int PAGESIZE = 5;// 每页显示的记录数   
  19.   
  20.     private int startpage;// 页面中的起始页   
  21.     private int endpage;// 页面中的结束页   
  22.   
  23.     private final int SHOWPAGE = 6;// 页面中显示的总页数 baidu,google显示的总页数是20   
  24.     // 在测试我们才用6来测试   
  25.   
  26.     private List<T> allentities;   
  27.   
  28.     private String url;   
  29.   
  30.     /** 根据当前页及总记录数来构造分页对象 */  
  31.     public Pagination(int nowpage, int countrecord) {   
  32.         this.nowpage = nowpage;   
  33.         this.countrecord = countrecord;   
  34.   
  35.         /** 计算总页数 */  
  36.         this.countpage = this.countrecord % this.PAGESIZE == 0 ? this.countrecord   
  37.                 / this.PAGESIZE   
  38.                 : this.countrecord / this.PAGESIZE + 1;   
  39.   
  40.         /** 计算startpage与endpage的值 */  
  41.   
  42.         /** 总页数数是否小于4 */  
  43.         if (this.countpage < (this.SHOWPAGE / 2 + 1)) {   
  44.             this.startpage = 1// 页面中起始页就是1   
  45.             this.endpage = this.countpage;// 页面中的最终页就是总页数   
  46.         } else {   
  47.             /** else中是总页数大于4的情况 */  
  48.   
  49.             /** 首先当前页的值是否小于等于4 */  
  50.             if (this.nowpage <= (this.SHOWPAGE / 2 + 1)) {   
  51.                 this.startpage = 1;   
  52.                 this.endpage = this.nowpage + 2;   
  53.                 /** 判断页面的最终页是否大于总页数 */  
  54.                 if (this.endpage >= this.countpage) {   
  55.                     this.endpage = this.countpage;   
  56.                 }   
  57.             } else {   
  58.                 this.startpage = this.nowpage - 3;   
  59.                 this.endpage = this.nowpage + 2;   
  60.   
  61.                 if (this.endpage >= this.countpage) {   
  62.                     this.endpage = this.countpage;   
  63.                     if (this.countpage < this.SHOWPAGE) {   
  64.                         this.startpage = 1;   
  65.                     } else {   
  66.                         this.startpage = this.endpage - 5;   
  67.                     }   
  68.   
  69.                 }   
  70.             }   
  71.   
  72.         }   
  73.   
  74.     }   
  75.   
  76.     public int getNowpage() {   
  77.         return nowpage;   
  78.     }   
  79.   
  80.     public void setNowpage(int nowpage) {   
  81.         this.nowpage = nowpage;   
  82.     }   
  83.   
  84.     public int getCountrecord() {   
  85.         return countrecord;   
  86.     }   
  87.   
  88.     public void setCountrecord(int countrecord) {   
  89.         this.countrecord = countrecord;   
  90.     }   
  91.   
  92.     public int getCountpage() {   
  93.         return countpage;   
  94.     }   
  95.   
  96.     public void setCountpage(int countpage) {   
  97.         this.countpage = countpage;   
  98.     }   
  99.   
  100.     public int getStartpage() {   
  101.         return startpage;   
  102.     }   
  103.   
  104.     public void setStartpage(int startpage) {   
  105.         this.startpage = startpage;   
  106.     }   
  107.   
  108.     public int getEndpage() {   
  109.         return endpage;   
  110.     }   
  111.   
  112.     public void setEndpage(int endpage) {   
  113.         this.endpage = endpage;   
  114.     }   
  115.   
  116.     public List<T> getAllentities() {   
  117.         return allentities;   
  118.     }   
  119.   
  120.     public void setAllentities(List<T> allentities) {   
  121.         this.allentities = allentities;   
  122.     }   
  123.   
  124.     public String getUrl() {   
  125.         return url;   
  126.     }   
  127.   
  128.     public void setUrl(String url) {   
  129.         this.url = url;   
  130.     }   
  131.   
  132. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值