分页封装

来源:互联网 发布:淘宝绑定别人的银行卡 编辑:程序博客网 时间:2024/04/28 08:11
分页封装
<pre name="code" class="java">import java.util.ArrayList;import java.util.List;/** * 分页对象,表示一页数据和上下文信息 * * */public class Page<T> {    private static int DEFAULT_PAGE_SIZE = 15;    private static int DEFAULT_PAGE_NO = 1;    /**     * 第几页     */    private int pageNo = DEFAULT_PAGE_NO;    /**     * 每页显示对象个数     */    private int pageSize = DEFAULT_PAGE_SIZE;    /**     * 对象总数量     */    private int totalCount;    /**     * 每页的数据列表     */    private List<T> result = new ArrayList<T>();    public Page(int pageNo, int pageSize) {        this.setPageNo(pageNo);        this.setPageSize(pageSize);    }    public Page(int pageNo) {        this.setPageNo(pageNo);        this.pageSize = DEFAULT_PAGE_SIZE;    }    public Page() {        this.pageNo = DEFAULT_PAGE_NO;        this.pageSize = DEFAULT_PAGE_SIZE;    }    // 每页的第一条记录在结果集中的位置    public int getPageFirst() {        return ((pageNo - 1) * pageSize);    }    // 总页数,这个是根据totalcount和pageSize计算的    public int getTotalPages() {        if (totalCount == 0)            return 0;        int count = totalCount / pageSize;        if (totalCount % pageSize > 0) {            count++;        }        return count;    }    /**     * 是否还有下一页.     */    public boolean isHasNext() {        return (pageNo + 1 <= getTotalPages());    }    /**     * 返回下页的页号,序号从1开始.     */    public int getNextPage() {        if (isHasNext())            return pageNo + 1;        else            return pageNo;    }    /**     * 是否还有上一页.     */    public boolean isHasPre() {        return (pageNo - 1 >= 1);    }    /**     * 返回上页的页号,序号从1开始.     */    public int getPrePage() {        if (isHasPre())            return pageNo - 1;        else            return pageNo;    }    /**     * @return the pageNo     */    public int getPageNo() {        return pageNo;    }    /**     * @param pageNo the pageNo to set     */    public void setPageNo(int pageNo) {        if (pageNo > 0) {            this.pageNo = pageNo;        }    }    /**     * @return the pageSize     */    public int getPageSize() {        return pageSize;    }    //和getPageSize一样    public int getLimit() {        return pageSize;    }    /**     * @param pageSize the pageSize to set     */    public void setPageSize(int pageSize) {        if (pageSize > 0) {            this.pageSize = pageSize;        }    }    /**     * @return the totalCount     */    public int getTotalCount() {        return totalCount;    }    /**     * @param totalCount the totalCount to set     */    public void setTotalCount(int totalCount) {        this.totalCount = totalCount;    }    /**     * @return the result     */    public List<T> getResult() {        return result;    }    /**     * @param result the result to set     */    public void setResult(List<T> result) {        this.result = result;    }}

应用示例:

public Page<ReportDemand> getReportDemandPage(QueryParam param,int pageNo, int pageSize){Page<ReportDemand> page = new Page<ReportDemand>();page.setPageSize(pageSize);page.setTotalCount(reportDemandMapper.getReportDemandCount(param));if(pageNo > page.getTotalPages() || page.getTotalCount() == 0){return new Page<ReportDemand>(pageNo, pageSize);}page.setPageNo(pageNo);param.setStart(page.getPageFirst());param.setLimit(page.getLimit());List<ReportDemand> list = reportDemandMapper.getReportDemandPage(param);page.setResult(list);return page;}

@Action( value = "page",results = {@Result(name = "success", type = "dispatcher", location = "../report_list.jsp")})public String reportPage(){HttpServletResponse response = ServletActionContext.getResponse();        HttpServletRequest request = ServletActionContext.getRequest();        response.setContentType("text/html;charset=UTF-8");        int pageNo = RequestUtil.getInt(request, "pageNo", 1);        int pageSize = RequestUtil.getInt(request, "pageSize", 15);        String url = "?pageSize=" + pageSize;        QueryParam param = getQueryParam(request);        Page<ReportDemand> page = reportDemandService.getReportDemandPage(param, pageNo, pageSize);        request.setAttribute("param", url + param.getUrl());        if (page.getTotalCount() > 0) {            request.setAttribute("pageBar", PageProcessor.process(page));            request.setAttribute("page", page);        }return SUCCESS;}private QueryParam getQueryParam(HttpServletRequest request) { UserInfo user = ContextUtil.getCurrentUser();        StringBuilder str = new StringBuilder("");                int dcId = RequestUtil.getInt(request, "dcId", 0);        if (dcId > 0) {            str.append("&dcId=").append(dcId);            request.setAttribute("dcId", dcId);        }        String keyWord = RequestUtil.getString(request, "keyWord");        if (StringUtils.isNotBlank(keyWord)) {            str.append("&keyWord=").append(keyWord);            request.setAttribute("keyWord", keyWord);        }        String submitterAccount = RequestUtil.getString(request, "submitterAccount");        if (StringUtils.isNotBlank(submitterAccount)) {            str.append("&submitterAccount=").append(submitterAccount);        }        String applicantAccount = RequestUtil.getString(request, "applicantAccount");        if (StringUtils.isNotBlank(applicantAccount)) {            str.append("&applicantAccount=").append(applicantAccount);        }        String accepterAccount = RequestUtil.getString(request, "accepterAccount");        if (StringUtils.isNotBlank(accepterAccount)) {            str.append("&accepterAccount=").append(accepterAccount);        }        String status = RequestUtil.getString(request, "status");        if (StringUtils.isNotBlank(status)) {            str.append("&status=").append(status);            request.setAttribute("status", status);        }                Boolean commented = RequestUtil.getBoolean(request, "commented");        if (null != commented) {            str.append("&commented=").append(commented);            request.setAttribute("commented", commented);        }        Boolean current = RequestUtil.getBoolean(request, "current", false);        if (current) {            request.setAttribute("current", true);            str.append("¤t=").append(true);        }        String userType = RequestUtil.getString(request, "ut");        if (StringUtils.isNotBlank(userType) && userType.equals("submit")) {            request.setAttribute("ut", "submit");            submitterAccount = current ? user.getAccount() : "";            str.append("&ut=").append("submit");        } else if (StringUtils.isNotBlank(userType) && userType.equals("accept")) {            request.setAttribute("ut", "accept");            accepterAccount = current ? user.getAccount() : "";            str.append("&ut=").append("accept");        }        QueryParam param = new QueryParam();        param.setDcId(dcId);        param.setTitle(keyWord);        param.setSubmitterAccount(submitterAccount);        param.setAccepterAccount(accepterAccount);        param.setApplicantAccount(applicantAccount);        param.setStatus(status);        param.setCommented(commented);        param.setUrl(str.toString());        return param;}

/** * 分页条处理类,让分页条和分页对象分离 * <p> */public class PageProcessor {    private static final int THRESHOLDFORELLIPSIS = 5;    public static <T> PageBar process(Page<T> page) {        int totalPage = page.getTotalPages();        int currentPage = page.getPageNo();        PageBar cssPageBar = new PageBar();        cssPageBar.setLinkNums(linkNums(totalPage, currentPage));        return cssPageBar;    }    private static int[] linkNums(int totalPage, int currentPage) {        int[] pre = new int[0];        int[] next = new int[0];        if (totalPage > 0) {            if (currentPage <= THRESHOLDFORELLIPSIS) {                pre = new int[currentPage];                for (int i = 0; i < pre.length; i++) {                    pre[i] = i + 1;                }            } else if (currentPage == totalPage) {                pre = new int[THRESHOLDFORELLIPSIS + 1];                pre[0] = 1;                pre[1] = 2;                pre[2] = -1;                pre[3] = currentPage - 2;                pre[4] = currentPage - 1;                pre[5] = currentPage;            } else {                pre = new int[THRESHOLDFORELLIPSIS];                pre[0] = 1;                pre[1] = 2;                pre[2] = -1;                pre[3] = currentPage - 1;                pre[4] = currentPage;            }            if ((totalPage - currentPage) <= (THRESHOLDFORELLIPSIS - 1)) {                next = new int[totalPage - currentPage];                for (int i = 0; i < next.length; i++) {                    next[i] = currentPage + i + 1;                }            } else if (currentPage == 1) {                next = new int[THRESHOLDFORELLIPSIS];                next[0] = 2;                next[1] = 3;                next[2] = -1;                next[3] = totalPage - 1;                next[4] = totalPage;            } else {                next = new int[THRESHOLDFORELLIPSIS - 1];                next[0] = currentPage + 1;                next[1] = -1;                next[2] = totalPage - 1;                next[3] = totalPage;            }        }        int[] linkNums = new int[pre.length + next.length];        int index = 0;        for (int num : pre) {            linkNums[index++] = num;        }        for (int num : next) {            linkNums[index++] = num;        }        return linkNums;    }}


public class PageBar {private int [] linkNums;public int[] getLinkNums() {return linkNums;}public void setLinkNums(int[] linkNums) {this.linkNums = linkNums;}}




                                             
0 0
原创粉丝点击