您的位置:首页 > 编程语言 > Go语言

仿Google分页的经典案例

2015-07-22 09:58 736 查看
仿Google分页的经典案例
一般分页的实现有:1、首页、上一页、下一页、最后一页
                                      2、上一页[1][2][3][4][5][6][7][8][9][10]下一页
                                      3、上一页[1][2][3][4][5][6][7]...下一页
下面以:上一页[1][2][3][4][5][6][7][8][9][10]下一页为例,实现一个仿Google得分页。
思路如下:
1、创建Page类,
package cn.csdn.domain;
import java.util.List;
public class Page {
private int nowpage;// 当前页
private int countrecord;// 总记录数
private int countpage;// 总页数
private int pageindex;// 当前页记录开始的位置 (nowpage-1)*PAGESIZE
public static final int PAGESIZE = 5;// 每页显示的记录数
private int sumindex = 6;// 索引的sum值 代表的是 google页面中最大显示页数
private int startindex;// 开始的索引值
private int endindex;// 结束的索引值
private List allentities;
public Page() {
}
public Page(int countrecord, int nowpage) { // 可变
// 计算当前页
this.nowpage = nowpage;
// 计算出当前页开始的位置
this.pageindex = (nowpage - 1) * PAGESIZE;
// 计算总页数
this.countrecord = countrecord;
if (this.countrecord % this.PAGESIZE == 0) {
this.countpage = this.countrecord / this.PAGESIZE;
} else {
this.countpage = this.countrecord / this.PAGESIZE + 1;
}
// 计算索引位置
//==============第一种方法========
/*if (this.nowpage <= 4) {
this.startindex = 1;
this.endindex = this.nowpage + 2;
if(this.endindex>this.countpage){
this.endindex=this.countpage;
}
}else if(this.nowpage>4){
this.startindex=this.nowpage-3;
this.endindex=this.nowpage+2;
if(this.endindex>this.countpage){
this.endindex=this.countpage;
this.startindex=this.countpage-5;
}
}*/
// ============第二种方法==========
if(nowpage<(lastpage-2)){
if(nowpage>=5){
//如果大于6的话,startindex要变大
startindex=nowpage-3;
}else{
startindex=1;
}
//endindex要变大
endindex=nowpage+2;
// 如果总页数小于6
if(endindex>lastpage){
endindex=lastpage;
}
}else{
endindex=lastpage;
startindex=endindex-5;
}
// ======================
}
//get、set方法
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 getPageindex() {
return pageindex;
}
public void setPageindex(int pageindex) {
this.pageindex = pageindex;
}
public int getSumindex() {
return sumindex;
}
public void setSumindex(int sumindex) {
this.sumindex = sumindex;
}
public int getStartindex() {
return startindex;
}
public void setStartindex(int startindex) {
this.startindex = startindex;
}
public int getEndindex() {
return endindex;
}
public void setEndindex(int endindex) {
this.endindex = endindex;
}
public List getAllentities() {
return allentities;
}
public void setAllentities(List allentities) {
this.allentities = allentities;
}
}
2、dao方法中的代码:
public int getCountRecord() {
// 1、定义返回结果
int countrecord = 0;
// 2、获取数据库连接对象
conn = DBConn.getConn();
// 3、创建预处理的sql语句
String sql = "select count(*) from student";
try {
// 4、根据预处理的sql语句创建预处理的操作对象
pstmt = conn.prepareStatement(sql);
// 5、查询的时候 直接执行
rs = pstmt.executeQuery();
 
// 6、判断
if (rs.next()) {
countrecord = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConn.close(rs, pstmt);
}
return countrecord;
}
// 获取当前页信息
public List<Student> getNowPageInfo(int pageindex,int pagesize) {
//1、定义返回结果变量
List<Student> allentities = new ArrayList<Student>();
//2、获取连接对象
conn = DBConn.getConn();
 try {
//3、根据预处理的sql语句创建预处理的操作对象
pstmt = conn.prepareStatement("select id,name,age,email from student limit ?,?");
//4、定义下标变量 并赋值
int index = 1;
pstmt.setInt(index++, pageindex);
pstmt.setInt(index++, pagesize);
//5、查询的时候 直接执行
rs = pstmt.executeQuery();
//判断
while(rs.next()){
//创建实体bean对象
Student entity = new Student();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setAge(rs.getInt("age"));
entity.setEmail(rs.getString("email"));
//添加到集合中
allentities.add(entity);
}
 } catch (SQLException e) {
e.printStackTrace();
}finally{
DBConn.close(rs, pstmt);
}
return allentities;
}
3、在servlet中的代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
 
throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("utf8");
//2.获取当前页
int nowpage=1;
String npage = req.getParameter("nowpage");
if(npage!=null){
nowpage = Integer.valueOf(npage);
}
//3、创建sevice服务操作对象
StudentServiceImpl ssi = new StudentServiceImpl();
//计算总记录数
int countrecord = ssi.getCountRecord();
//创建page对象
Page stupage = new Page(countrecord, nowpage);
//获取当前页信息
        List<Student> allentities = ssi.getNowPageInfo(stupage.getPageindex(),stupage.PAGESIZE);
//把当前页信息赋值给page对象的list集合
stupage.setAllentities(allentities);
//存入到reqeust中
req.setAttribute("stupage", stupage);
req.getRequestDispatcher("liststudents.jsp").forward(req, resp);
}
4、在jsp中的代码:
<div id="pg">
           <c:if test="${stupage.nowpage!=1}">
           <span>
             <a href="${pageContext.request.contextPath}/liststudents.do?nowpage=${stupage.nowpage-1>0?stupage.nowpage-1:1}">上一页</a>
           </span>
           </c:if>
        <c:forEach begin="${stupage.startindex}" end="${stupage.endindex}" var="indexnum">
           <span>
             [<a href="${pageContext.request.contextPath}/liststudents.do?nowpage=${indexnum}">${indexnum}</a>]
           </span>
         </c:forEach> 
           <c:if test="${stupage.nowpage!=stupage.countpage}">
           <span>
           <a href="${pageContext.request.contextPath}/liststudents.do?nowpage=${stupage.nowpage+1>stupage.countpage?stupage.countpage:stupage.nowpage+1}">下一页</a>
           </span>
           </c:if>
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  分页 谷歌