java web分页代码


在web开发中,经常会遇到一些数据很多时,我们便需要分页存放,写一个小例子,供以后自己使用


/**
 *java分页程序
 * @author yzz
 */
public class Pagination {
    /***
     *当前页码
     * 总页数
     * 每页显示记录数
     * 总记录数
     */
    private int pageNo = 0;
    private int pageCount = 0;   //总页数
    private int pageSize = 10;    //每页显示记录
    private int countSize = 0;      //总记录数
    
    private String pageBar;
    private String numPageBar;
    private String url;

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageCount() {
        if (countSize != 0)
        {
            /***
             * 如果总记录数能够整除每页的记录数 
             */
            if(countSize%pageSize == 0){
                pageCount = countSize/pageSize;
            }else {
                pageCount = countSize/pageSize + 1;
            }
        }
        return pageCount;
    }

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

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCountSize() {
        return countSize;
    }

    public void setCountSize(int countSize) {
        this.countSize = countSize;
    }

    public String getPageBar() {
        pageCount = getPageCount();
        if(pageCount != 0) {
            
            if(pageNo < 1) {
                pageNo = 1; //如果当前页小于1则默认1
            }
            if(pageNo > pageCount) {
                pageNo = pageCount;
            }
            StringBuilder sb = new StringBuilder();
            sb.append("每页 :").append(pageSize).
                    append("  页次: ").append(pageNo).append("/").append(pageCount).
                    append(" 总计: ").append(countSize).append(" ");
             
            sb.append("
    "); if (pageNo == 1) { sb.append("
  • 首页
  • "); sb.append("
  • 上页
  • "); } else { sb.append("
  • 首页
  • "); sb.append("
  • 上页
  • "); } if (pageNo == pageCount) { sb.append("
  • 下页
  • "); sb.append("
  • 尾页
  • "); } else { sb.append("
  • 下页
  • "); sb.append("
  • 尾页
  • "); } sb.append("
"); pageBar = sb.toString(); } else { pageBar = ""; } return pageBar; } public String getNumPageBar() { pageCount = getPageCount(); if (pageCount != 0) { //判断当前页号的合法性 if (pageNo < 1) { pageNo = 1; } if (pageNo > pageCount) { pageNo = pageCount; } StringBuilder sb = new StringBuilder(); //计算当前页所在的组 int group = pageNo / 5 + (pageNo % 5 == 0 ? 0 : 1); int start = (group - 1) * 5 + 1; int end = start + 4; // System.out.println("group:" + group + ";start:" + start + ";end:" + end); if (end > pageCount) { end = pageCount; } sb.append("
    "); if (start > 1) { sb.append("
  • <
  • "); } for (int i = start; i <= end; i++) { if (pageNo != i) { sb.append("
  • ").append(i).append("
  • "); } else { sb.append("
  • ").append(i).append("
  • "); } } if (end < pageCount) { sb.append("
  • >
  • "); } sb.append("
"); numPageBar = sb.toString(); } else { numPageBar = ""; } return numPageBar; } public void setUrl(String url) { this.url = url; } }

你可能感兴趣的:(web)