JAVA工具类集锦1--分页类

1、分页类:

public class Page {
    /**
     * 一行显示的最大记录数
     */
    public static int MAX_ROW = 15;
    /**
     * 当前开始位置
     */
    private int index;
    /**
     * 总页数
     */
    private int page_num;
    /**
     * 查询的结果集大小
     */
    private int total;
    /**
     * 返回链接
     */
    private String url;

    public Page() {
        index = 0;
        page_num = 1;
        total = 0;
    }

    public int getTotal() {
        return total;
    }
    public int getIndex() {
        return index;
    }
    public int getPageNum() {
        return page_num;
    }
    public void setTotal(int num) {
        total = num;
        if (total % MAX_ROW == 0) {
            page_num = total / MAX_ROW;
        }
        else {
            page_num = total / MAX_ROW + 1;
        }
    }
    /**
     * 在文本中插入页面标号为num的标记
     */
    private void insertTag(StringBuffer text, int num) {
        int temp = index / MAX_ROW;
        if (num == temp) {
            text.append(num + 1).append("  ");
        }
        else {
            text.append("").append(num + 1).append("  ");
        }
    }

    /**
     * 在文本中插入位置为起始位置为num,页面标号为str的标记
     * @param text
     * @param num 起始位置
     * @param str 页面编号
     */
    private void insertTag(StringBuffer text, int num, String str) {
        text.append("").append(str).append("  ");
    }

    public String getFooter() {
        if (total <= MAX_ROW) {
            return "";
        }
        StringBuffer str = new StringBuffer("分页:");
        if (page_num <= MAX_ROW) {
            for (int i = 0; i < page_num; i++) {
                insertTag(str, i);
            }
            if (index + MAX_ROW < total) {
                insertTag(str, index + MAX_ROW, "下一页");
            }
            else {
                str.append("下一页");
            }
            return str.toString();
        }
        else {
            int unit = MAX_ROW * MAX_ROW;
            if (index < unit) {
                int i;
                for (i = 0; i < MAX_ROW; i++) {
                    insertTag(str, i);
                }
                String tt = "下" + MAX_ROW + "页";
                insertTag(str, i * MAX_ROW, tt);
                return str.toString();
            }
            else { //处理在中间或最后10页的问题
                //page表示页;section表示多个页组成的面
                //当前页面所在的页号。
                int currentPage = index / MAX_ROW;
                //当前页所在的面
                int currentSection = currentPage / MAX_ROW + 1;
                //上一面的开始页
                int lastPage = (currentSection - 1) * MAX_ROW - 1;
                //上一面的开始页的开始项的编号
                int lastPageRow = lastPage * MAX_ROW;
                //下一面的开始页编号
                int nextPage = currentSection * MAX_ROW;
                //下一面的开始页的开始项的编号
                int nextPageRow = nextPage * MAX_ROW;
                String tt = "上" + MAX_ROW + "页";
                insertTag(str, lastPageRow, tt);
                //如果还有下一面
                if (nextPageRow <= total - 1) {
                    for (int i = 0; i < MAX_ROW; i++) {
                        insertTag(str, i + lastPage + 1);
                    }
                    tt = "下" + MAX_ROW + "页";
                    insertTag(str, nextPageRow, tt);
                }
                else { //如果没有下一面

                    //当前面的页面总数
                    int lengthOfPage = total - (lastPage + 1) * MAX_ROW;
                    lengthOfPage = lengthOfPage / MAX_ROW + 1;

                    for (int i = 0; i < lengthOfPage; i++) {
                        insertTag(str, i + 1 + lastPage);
                    }
                }
                return str.toString();

            }
        }
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public void setIndex(int index) {
        this.index = index;
    }

}

 

//未完待续..........................................

 

 

你可能感兴趣的:(java,工具,string,url,class)