实现仿google分页

以下是实现仿google分页代码,供大家参考。
一、Pagination类:
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
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; }
}

二、AdminDaoImpl类中的方法:
public List<Admin> findByNowPage(int nowpage, int pagesize) {
// 1、定义返回结果
List<Admin> allentities = new ArrayList<Admin>();
// 2、获取连接对象
conn = DBConn.getConn();
// 3、定义预处理的sql语句
String sql = "select id,name,pass from admins limit ?,?";
try {
// 4、根据预处理的sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
// 定义下标
int index = 1;
pstmt.setInt(index++,(nowpage-1)*pagesize);
pstmt.setInt(index++,pagesize);

// 5、执行
rs = pstmt.executeQuery();
while (rs.next()) {
Admin entity = new Admin();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setPass(rs.getString("pass"));

allentities.add(entity);
}
} catch (SQLException e) {

e.printStackTrace();
} finally {
DBConn.close(rs, pstmt);
}
return allentities;
}

public int getCountRecord() {
// 1、定义返回结果
int countrecord = 0;
// 2、获取连接对象
conn = DBConn.getConn();
// 3、定义预处理的sql语句
String sql = "select count(*) from admins";
try {
// 4、根据预处理的sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
countrecord = rs.getInt(1);
}
} catch (SQLException e) {

e.printStackTrace();
} finally {
DBConn.close(rs, pstmt);
}
return countrecord;
}

Servlet中的代码:
int nowpage =1;
if(npage!=null){
nowpage=Integer.valueOf(npage);
}
AdminServiceImpl adminServiceImpl = new AdminServiceImpl();
int countrecord = adminServiceImpl.getCountRecord();
List<Admin> allentities =
Pagination<Admin> pageination = new Pagination<Admin>(nowpage, countrecord);
pageination.setAllentities(allentities); pageination.setUrl(request.getContextPath()+"/listAdmins.do");
request.setAttribute("pagination",pageination); request.getRequestDispatcher("admin/ListAdmins.jsp").forward(request, response);

pagination.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
总共${pagination.countpage}页 总共${pagination.countrecord} 记录
<c:if test="${pagination.nowpage>1}">
<a href="${pagination.url}?nowpage=${pagination.nowpage-1}">
上一页</a>
</c:if>
<c:forEach var="sequence" begin="${pagination.startpage}" end="${pagination.endpage}">
[<span><ahref="${pagination.url}?nowpage=${sequence}">${sequence}</a> </span>]
</c:forEach>

<c:if test="${pagination.nowpage<pagination.countpage}">
<a href="${pagination.url}?nowpage=${pagination.nowpage+1}"> 下一页</a>
</c:if>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值