孟繁森

javaweb分页工具类的封装
我们在展示数据时总是离不开分页,因为大量的数据无法让我们都展示在一个页面之中,为了用户的体验,减少加载速度,提高可...
扫描右侧二维码阅读全文
08
2019/08

javaweb分页工具类的封装

前言:

我们在展示数据时总是离不开分页,因为大量的数据无法让我们都展示在一个页面之中,为了用户的体验,减少加载速度,提高可读性,我们总是把数据分成每页均等的份数展示出来,让页面更美观。
代码:

public class PageTool {
    private int pageSize;
    private int totalCount;
    private int currentPage;
    private int lastPage;
    private int nextPage;
    private int pageCount;
    private int startIndex;
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getLastPage() {
        return lastPage;
    }
    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }
    public int getNextPage() {
        return nextPage;
    }
    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }
    public int getPageCount() {
        return pageCount;
    }
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
    public int getStartIndex() {
        return startIndex;
    }
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }
    //总数据量与当前页码,其他值可以算出来
    public PageTool(int totalCount, String currentPage) {
        super();
        this.totalCount = totalCount;
        pageCount = 5;//每页固定五条
        //初始化总页数
        initialPageSize();
        //初始化页数
        intialCurrentPage(currentPage);
        //初始化第一页
        intialLastPage();
        //初始化下一页
        intialNextPage();
        //初始化起始下标
        intialStartIndex();
    }
    private void intialStartIndex() {
     startIndex = (currentPage -1) *pageCount;
    }
    private void intialNextPage() {
        if(currentPage == pageSize) {
            nextPage = pageSize;
        }else {
            nextPage = currentPage+1;
        }
    }
    private void intialLastPage() {
        if (currentPage ==1) {
            lastPage = 1;
        }else {
            lastPage =currentPage-1;
        }
    }
    private void intialCurrentPage(String currentPage) {
        /*
         * 因为当前页码currentpage时页面传过来的。
         */
         if(currentPage == null || "".equals(currentPage)) {
             this.currentPage = 1;
         }else {
             this.currentPage = Integer.valueOf(currentPage);
         }
    }
    private void initialPageSize() {
        pageSize  = totalCount/pageCount+(totalCount%pageCount==0?0:1);
        
    }
    
}
Last modification:September 19th, 2019 at 12:09 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment