Servlet实现后台分页查询

Posted cjw554551

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet实现后台分页查询相关的知识,希望对你有一定的参考价值。

创建实体类

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实现后台分页查询的主要内容,如果未能解决你的问题,请参考以下文章

phpcms v9后台多表查询分页代码

jsp+servlet实现模糊查询和分页效果

⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台论坛管理demo

⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台论坛管理demo

分页,条件查询

课上测试-实现分页显示和模糊查询(代码部分)