码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet实现后台分页查询

时间:2020-12-14 12:46:16      阅读:3      评论:0      收藏:0      [点我收藏+]

标签:static   art   创建   分页显示   lis   nal   service   parameter   star   

创建实体类

public class PageBean<T> implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int currentPage; //当前第几页 ,请求传过来
     private int pageCount;  // 每页显示多少条数据
     private int totalCount; //总记录数,查询数据获得
     
     private int totalPage;  //总页数, 通过  totalCount 和 pageCount计算获得
     
     private int startIndex; //开始索引,与pageCount 组成 limit 条件
     
     private List<T> pageData;
     
     //分页显示的页数,如 1,2,3,4
     private int start;
     private int end;
     
     public PageBean(int currentPage,int pageCount,int totalCount){
         this.currentPage=currentPage;
         this.pageCount=pageCount;
         this.totalCount=totalCount;
         
         if(totalCount % pageCount==0){
             this.totalPage = totalCount/ pageCount;
         }else{
             this.totalPage= totalCount/pageCount+1;
         }
         
         this.startIndex = (currentPage-1)*pageCount;
         
         this.start=1;
         this.end=5;
         
         if(pageCount<=5){
             this.end= this.totalPage;
         }else{

             this.start=currentPage-2;
             this.end = currentPage+2;
             
             if(start<=0){
                 this.start=1;
                 this.end=5;
             }
             if(this.end > this.totalPage){
                 this.end=totalPage;
                 this.start=end-4;
             }
         }
     }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getPageData() {
        return pageData;
    }

    public void setPageData(List<T> pageData) {
        this.pageData = pageData;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
     
     
     
}

web层

int currentPage = Integer.valueOf(request.getParameter("currentPage"));

        int pageCount=7;

        BlogTypeService blogType = new BlogTypeServiceImpl();

        PageBean<BlogType> page = blogType.page(currentPage, pageCount);
        request.setAttribute("page", page);
        request.getRequestDispatcher("/jsp/blogType.jsp").forward(request, response);

 service层

public PageBean<BlogType> page(int currentPage,int pageCount) {
        int totalCount=blogType.getTotal();
        
        PageBean<BlogType> pageBean = new PageBean<>(currentPage, pageCount, totalCount);
        int startIndex = pageBean.getStartIndex();
        pageBean.setPageData(blogType.page(startIndex, pageCount));
        return pageBean;
    }

 dao层

public PageBean<BlogType> page(int currentPage,int pageCount) {
        int totalCount=blogType.getTotal();
        
        PageBean<BlogType> pageBean = new PageBean<>(currentPage, pageCount, totalCount);
        int startIndex = pageBean.getStartIndex();
        pageBean.setPageData(blogType.page(startIndex, pageCount));
        return pageBean;
    }

 

Servlet实现后台分页查询

标签:static   art   创建   分页显示   lis   nal   service   parameter   star   

原文地址:https://www.cnblogs.com/cjw554551/p/14099359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!