//entity层实体类 import java.util.List; //分页展示
//相关属性:当前页,页大小(每页显示的条数),总页数,总条数,数据
//select * from t_user limit 3,3
public class Page {
private Integer currentPage; //当前页
private Integer pageSize; //页大小
private Integer pageCount; //页数量
private Integer totalCount; //总条数
private List<?> list; //数据
private String url; //参数路径
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
} @Override
public String toString() {
return "Page [currentPage=" + currentPage + ", pageSize=" + pageSize + ", pageCount=" + pageCount
+ ", totalCount=" + totalCount + ", list=" + list + ", url=" + url + "]";
} public Page(Integer currentPage, Integer pageSize, Integer pageCount, Integer totalCount, List<?> list,
String url) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.pageCount = pageCount;
this.totalCount = totalCount;
this.list = list;
this.url = url;
} public Page() {
} public Page(Integer currentPage, Integer pageSize) {
this.currentPage = currentPage;
this.pageSize = pageSize;
}
}
// servlet层 (分页相关代码)
//-----直接做分页展示-----
String current = request.getParameter("currentPage"); // 获取前端传入的当前页
Page page = goodsInfoService.getPage(current); // 在业务层给page对象赋值
request.setAttribute("page", page);
//service层相关代码
public Page getPage(String current) {
Integer currentPage = 1; // 默认为第一页
Integer pageSize = 5; // 每页显示5条记录
if(current != null){
currentPage = Integer.parseInt(current);
}
Page page = new Page(currentPage, pageSize); //当前页和页大小的赋值 Integer totalCount = goodsInfoDao.getTotalCount();
page.setTotalCount(totalCount); //设置总条数 // 页数量==总条数/页大小--如果整除就是该值,否则+1
Integer pageCount = totalCount/pageSize;
pageCount=totalCount%pageSize==0?pageCount:pageCount+1;
page.setPageCount(pageCount); //设置页数量 Integer startIndex = (currentPage-1)*pageSize; // 起始下标为(当前页-1)*页大小
List<GoodsInfo> list = goodsInfoDao.getGoodsInfoListPage(startIndex, pageSize);
page.setList(list); //设置数据 page.setUrl("GoodsInfoServlet?action=goodsInfoList"); //设置url return page;
}
//Dao相关分页代码
public int getTotalCount() { // 获取总记录数
String sql="select count(1) from t_goods_info";
return CommonUtils.getTotalCount(sql);
}
//jquery 相关分页代码
<a href="${page.url}¤tPage=1">首页</a>
<c:if test="${page.currentPage!=1}">
<a href="${page.url}¤tPage=${page.currentPage-1}">上一页</a>
</c:if>
<c:if test="${page.currentPage!=page.pageCount}">
<a href="${page.url}¤tPage=${page.currentPage+1}">下一页</a>
</c:if>
<a href="${page.url}¤tPage=${page.pageCount}">尾页</a>
共${page.currentPage}/${page.pageCount}页