仿Baidu,Google查询分页技术实现分析
分页 (pagination) 一种自动分页机制,可以将移动 Web 窗体中的内容分割成一组组较小的页进行呈现,以适合于特定的设备。该机制还呈现可用于浏览到其他页的用户界面元素.在整个的web开发应用中分页是必会的知识点。应该熟练掌握与运用。
在实际开发的应用中,分页显的尤为重要,现将baidu,google查询分页显示的页面及分页效果分析如下:
1
、baidu搜索观察分页效果如下:

2
、Google搜索分页效果实现如下:

分析流程结构:
页面显示的页码
|
当前页
|
末页
|
分析
|
1 2 3 4 5 6 7 8 9 10
下一页
|
1
|
10
|
10=1+9
|
分析1:当总页数小于11时,
页面索引起始值=1;
页面索引最终值=总页数;
| |||
上一页 1 2 3 4 5 6 7 8 9 10 11 下一页
|
2
|
11
|
11=2+9
|
上一页 1 2 3 … … 9 10 11 12 下一页
|
3
|
12
|
12=3+9
|
上一页 1 2 3 … …10 11 12 13 下一页
|
4
|
13
|
13=4+9
|
上一页 1 2 3 … …11 12 13 14 下一页
|
5
|
14
|
14=5+9
|
上一页 1 2 3 … …12 13 14 15 下一页
|
6
|
15
|
15=6+9
|
上一页 1 2 3 … …13 14 15 16 下一页
|
7
|
16
|
16=7+9
|
上一页 1 2 3 … …16 17 18 20 下一页
|
11
|
20
|
20=11+9
|
注意观察
| |||
上一页 2 3 4 … …18 29 20 21 下一页
|
12
|
21
|
2=12-10 21=12+9
|
上一页 3 4 5 … …19 20 21 22 下一页
|
13
|
22
|
3=13-10 22=13+9
|
上一页 4 5 6 … … 20 21 22 23 下一页
|
14
|
23
|
4=14-10 23=13+9
|
分析2:当
总页数大于
11页面上显示的总共页码为20,其中以上观察可知:
页面索引起始值=当前页-10;
页面索引最终值=当前页+9;
注意:当页面的
索引最终值大于查询的总页数时,那么效果应该如下:
页面索引最终值=查询的总页数;
页面索引起始值=查询的总页数-19;
|
上面做了简单的分析与算法,那你是否能够用Java代码实现呢?
原创:仿Baidu,Google查询分页技术JAVA实现
http://student.youkuaiyun.com/space.php?uid=1394199&do=blog&id=56848
在上一节中我们简单观察与分析了仿baidu,google的查询分页技术,有很多学生纷纷的给我来了邮件,期待着使用Java技术的实现.在这里很感谢大家对我的关注,我会一如既往的在此平台之上发布自己的授课中一些知识点的总结.下将仿baidu,google查询分页技术的实现方式一种分享给大家,如有问题请及时发送邮件.希望同学们在这新的学期中有更大的收获。
在这里我简单说说我的实现思路,我将整个分页的技术全部封装在了一个Pagination的JavaBean中.具体代码如下:
在这里我简单说说我的实现思路,我将整个分页的技术全部封装在了一个Pagination的JavaBean中.具体代码如下:
- package cn.csdn.util;
- import java.util.List;
- /**
- *
- * @author redarmy_chen
- *
- * @param <T>
- */
- public class Pagination<T> {
- // 分页信息
- private int nowpage;// 当前页
- private int countrecord;// 总记录
- private int countpage;// 总页数
- public static final int PAGESIZE = 5;// 每页显示的记录数
- private int startpage;// 页面中的起始页
- private int endpage;// 页面中的结束页
- private final int SHOWPAGE = 6;// 页面中显示的总页数 baidu,google显示的总页数是20
- // 在测试我们才用6来测试
- private List<T> allentities;
- private String url;
- /** 根据当前页及总记录数来构造分页对象 */
- public Pagination(int nowpage, int countrecord) {
- this.nowpage = nowpage;
- this.countrecord = countrecord;
- /** 计算总页数 */
- this.countpage = this.countrecord % this.PAGESIZE == 0 ? this.countrecord
- / this.PAGESIZE
- : this.countrecord / this.PAGESIZE + 1;
- /** 计算startpage与endpage的值 */
- /** 总页数数是否小于4 */
- if (this.countpage < (this.SHOWPAGE / 2 + 1)) {
- this.startpage = 1; // 页面中起始页就是1
- this.endpage = this.countpage;// 页面中的最终页就是总页数
- } else {
- /** else中是总页数大于4的情况 */
- /** 首先当前页的值是否小于等于4 */
- if (this.nowpage <= (this.SHOWPAGE / 2 + 1)) {
- this.startpage = 1;
- this.endpage = this.nowpage + 2;
- /** 判断页面的最终页是否大于总页数 */
- if (this.endpage >= this.countpage) {
- this.endpage = this.countpage;
- }
- } else {
- this.startpage = this.nowpage - 3;
- this.endpage = this.nowpage + 2;
- if (this.endpage >= this.countpage) {
- this.endpage = this.countpage;
- if (this.countpage < this.SHOWPAGE) {
- this.startpage = 1;
- } else {
- this.startpage = this.endpage - 5;
- }
- }
- }
- }
- }
- public int getNowpage() {
- return nowpage;
- }
- public void setNowpage(int nowpage) {
- this.nowpage = nowpage;
- }
- public int getCountrecord() {
- return countrecord;
- }
- public void setCountrecord(int countrecord) {
- this.countrecord = countrecord;
- }
- public int getCountpage() {
- return countpage;
- }
- public void setCountpage(int countpage) {
- this.countpage = countpage;
- }
- public int getStartpage() {
- return startpage;
- }
- public void setStartpage(int startpage) {
- this.startpage = startpage;
- }
- public int getEndpage() {
- return endpage;
- }
- public void setEndpage(int endpage) {
- this.endpage = endpage;
- }
- public List<T> getAllentities() {
- return allentities;
- }
- public void setAllentities(List<T> allentities) {
- this.allentities = allentities;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- }
package cn.csdn.util;
import java.util.List;
/**
*
* @author redarmy_chen
*
* @param <T>
*/
public class Pagination<T> {
// 分页信息
private int nowpage;// 当前页
private int countrecord;// 总记录
private int countpage;// 总页数
public static final int PAGESIZE = 5;// 每页显示的记录数
private int startpage;// 页面中的起始页
private int endpage;// 页面中的结束页
private final int SHOWPAGE = 6;// 页面中显示的总页数 baidu,google显示的总页数是20
// 在测试我们才用6来测试
private List<T> allentities;
private String url;
/** 根据当前页及总记录数来构造分页对象 */
public Pagination(int nowpage, int countrecord) {
this.nowpage = nowpage;
this.countrecord = countrecord;
/** 计算总页数 */
this.countpage = this.countrecord % this.PAGESIZE == 0 ? this.countrecord
/ this.PAGESIZE
: this.countrecord / this.PAGESIZE + 1;
/** 计算startpage与endpage的值 */
/** 总页数数是否小于4 */
if (this.countpage < (this.SHOWPAGE / 2 + 1)) {
this.startpage = 1; // 页面中起始页就是1
this.endpage = this.countpage;// 页面中的最终页就是总页数
} else {
/** else中是总页数大于4的情况 */
/** 首先当前页的值是否小于等于4 */
if (this.nowpage <= (this.SHOWPAGE / 2 + 1)) {
this.startpage = 1;
this.endpage = this.nowpage + 2;
/** 判断页面的最终页是否大于总页数 */
if (this.endpage >= this.countpage) {
this.endpage = this.countpage;
}
} else {
this.startpage = this.nowpage - 3;
this.endpage = this.nowpage + 2;
if (this.endpage >= this.countpage) {
this.endpage = this.countpage;
if (this.countpage < this.SHOWPAGE) {
this.startpage = 1;
} else {
this.startpage = this.endpage - 5;
}
}
}
}
}
public int getNowpage() {
return nowpage;
}
public void setNowpage(int nowpage) {
this.nowpage = nowpage;
}
public int getCountrecord() {
return countrecord;
}
public void setCountrecord(int countrecord) {
this.countrecord = countrecord;
}
public int getCountpage() {
return countpage;
}
public void setCountpage(int countpage) {
this.countpage = countpage;
}
public int getStartpage() {
return startpage;
}
public void setStartpage(int startpage) {
this.startpage = startpage;
}
public int getEndpage() {
return endpage;
}
public void setEndpage(int endpage) {
this.endpage = endpage;
}
public List<T> getAllentities() {
return allentities;
}
public void setAllentities(List<T> allentities) {
this.allentities = allentities;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}