All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.power.common.model.PageVo Maven / Gradle / Ivy

The newest version!
package com.power.common.model;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.List;

/**
 * 主要用于前端内容展示分页,分页链接由后台输出
 * PageVo根据自定义的url直接生成链接,messageFormat占位替换生成页码
 * USAGE:
 * RequestMapping(value="news/p{pageIndex}.htm",method = RequestMethod.GET)
 * public String list(@PathVariable int pageIndex,Model model){
 * try{
 * PageVo pageDto = this.newsService.getPage(0,pageIndex,12);
 * pageDto.setUrl("news/p{0}.htm");
 * model.addAttribute("list",pageDto);
 * }catch (Exception e){
 * return "";
 * }
 * return "forward:/news.jsp";
 * }
 *
 * @param  Generics
 * @author sunyu
 */
public class PageVo implements Serializable {
    private static final long serialVersionUID = 1923401118856169487L;

    /**
     * total record
     */
    private Long total;

    /**
     * 使用messageFormat占位替换
     */
    private String url;

    /**
     * page index
     */
    private Integer pageIndex;

    /**
     * page size
     */
    private Integer pageSize;

    /**
     * result items
     */
    private List items;

    /**
     * 连续分页主体部分显示的分页条目数
     */
    private int displayEntries = 5;

    /**
     * 两侧显示的首尾分页的条目数
     */
    private int edgeEntries = 1;

    //default
    public PageVo() {
        super();
    }

    public PageVo(Long total, Integer pageIndex, Integer pageSize, List items) {
        super();
        this.total = total;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.items = items;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public List getItems() {
        return items;
    }

    public void setItems(List items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return "PageVo [total=" + total + ", pageIndex=" + pageIndex
                + ", pageSize=" + pageSize + ", items=" + items + "]";
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getNavigation() {
        Integer totalPage = (int) (total % pageSize > 0 ? (total
                / pageSize + 1) : (total / pageSize));
        //From jQuery pagination
        //int ne_half = ((displayEntries&1)==1)?(displayEntries/2+1):displayEntries/2;
        int ne_half = ((displayEntries & 1) == 1) ? (displayEntries >> 1) + 1 : displayEntries >> 1;
        int upper_limit = totalPage - displayEntries;
        pageIndex = (pageIndex < 1) ? 1 : pageIndex;
        int currentPage = pageIndex - 1;

        int start = currentPage > ne_half ? Math.max(Math.min(currentPage - ne_half, upper_limit), 0) : 0;
        int end = currentPage > ne_half ? Math.min(currentPage + ne_half, totalPage) : Math.min(displayEntries, totalPage);
        StringBuilder buffer = new StringBuilder();
        buffer.append("
"); // Generate "Previous"-Link if (pageIndex == 1 || totalPage == 0) { buffer.append("上一页"); } else { String str = MessageFormat.format("上一页"); } // Generate starting points if (start > 0 && edgeEntries > 0) { int end1 = Math.min(edgeEntries, start); for (int i = 0; i < end1; i++) { String str = MessageFormat.format("").append(i + 1).append(""); } if (edgeEntries < start) { buffer.append("..."); } } // Generate interval links for (int i = start; i < end; i++) { if (i + 1 == pageIndex) { //current page buffer.append("").append(i + 1).append(""); } else { String str = MessageFormat.format("").append(i + 1).append(""); } } // Generate ending points if (end < totalPage && edgeEntries > 0) { if (totalPage - edgeEntries > end) { buffer.append("..."); } int begin = Math.max(totalPage - edgeEntries, end); for (int i = begin; i < totalPage; i++) { String str = MessageFormat.format("").append(i + 1).append(""); } } // Generate "Next"-Link if (pageIndex == totalPage || totalPage == 0) { buffer.append("下一页"); } else { String str = MessageFormat.format("下一页"); } buffer.append("
"); return buffer.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy