java Page分页显示

//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);
request.setAttribute("page", page);

 

//service层相关代码
public Page getPage(String current) {
    Integer currentPage = 1;
    Integer pageSize    = 5;
    if(current != null){
        currentPage = Integer.parseInt(current);
    }
    Page page = new Page(currentPage, pageSize);  //当前页和页大小的赋值
    
    Integer totalCount = goodsInfoDao.getTotalCount();
    page.setTotalCount(totalCount);  //设置总数
    
    Integer pageCount = totalCount/pageSize;
    pageCount=totalCount%pageSize==0?pageCount:pageCount+1;
    page.setPageCount(pageCount);   //设置页数量
    
    Integer startIndex = (currentPage-1)*pageSize;
    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}&currentPage=1">首页</a>
    <c:if test="${page.currentPage!=1}">
        <a href="${page.url}&currentPage=${page.currentPage-1}">上一页</a>
    </c:if>
    <c:if test="${page.currentPage!=page.pageCount}">
        <a href="${page.url}&currentPage=${page.currentPage+1}">下一页</a>
    </c:if>
    <a href="${page.url}&currentPage=${page.pageCount}">尾页</a>
共${page.currentPage}/${page.pageCount}页

 

上一篇:上传图片到服务器,第一次是404,需要进入项目才会加载图片的问题


下一篇:ADO SQL手写分页