Web分页工具类

  JavaEE的企业应用都少不了分页,有个通用的分页工具类就比较方便了。

  一般分页类应该包含记录集合、当前页、每页显示记录数、总记录数、总页数等属性,扩展一下,还可以有判断是否有上一页、是否有下一页等属性。

1、分页工具类

package cn.luxh.app.util;

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


/**
 * The <code>Pagination</code> class 分页
 * 
 * @author Luxh
 * @version 1.0  
 */
public class Pagination<T>  implements Serializable{
    
    private static final long serialVersionUID = 5104811017362151385L;

    /**当前页*/
    private int currentPage;
    
    /**每页显示记录数*/
    private int pageSize;
    
    /**总记录数*/
    private long recordCount = 1L;
    
    /**记录集合*/
    private List<T> recordList;
    
    /**总页数*/
    private int pageCount;
    
    /**偏移数*/
    private int offset;
    
    /**上一页*/
    private int prePage;
    
    /**下一页*/
    private int nextPage;
    
    /**是否有上一页*/
    private boolean hasPrePage;
    
    /**是否有下一页*/
    private boolean hasNextPage;
    
    
    /**
     * 默认的空参构造数
     *
     */
    public Pagination() {
        
    }
    
    /**
     * 构造函数,计算总页数、是否有上一页、下一页等.
     * @param currentPage    当前页
     * @param pageSize        每页显示记录数
     * @param recordCount   总记录数
     * @param recordList    记录集合
     */
    public  Pagination(int currentPage,int pageSize,long recordCount,List<T> recordList) {
        this.currentPage = currentPage;
        if(currentPage < 1) {
            this.currentPage = 1;
        }
        
        this.pageSize = pageSize;
        this.recordCount = recordCount;
        this.recordList = recordList;
        
        //上一页等于当前页减一
        this.prePage = this.currentPage - 1;
        if(this.prePage < 1) {
            this.hasPrePage = false;//没有上一页
            this.prePage = 1;
        }else {
            this.hasPrePage = true;//有上一页
        }
        
        //计算总页数
        this.pageCount = (int)Math.ceil(recordCount / (double)pageSize);
        if(this.currentPage > this.pageCount) {
            this.currentPage = this.pageCount;
        }
        
        //下一页等于当前页加一
        this.nextPage = this.currentPage + 1;
        if(this.nextPage > this.pageCount) {
            this.hasNextPage = false;//没有下一页
            this.nextPage = this.pageCount;
        }else {
            this.hasNextPage = true;//有下一页
        }
        
        //偏移量
        this.offset = (this.currentPage - 1)*pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean isHasPrePage() {
        return hasPrePage;
    }

    public void setHasPrePage(boolean hasPrePage) {
        this.hasPrePage = hasPrePage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getPageCount() {
        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 getPrePage() {
        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    

    public long getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(long recordCount) {
        this.recordCount = recordCount;
    }

    public List<T> getRecordList() {
        return recordList;
    }

    public void setRecordList(List<T> recordList) {
        this.recordList = recordList;
    }
    
    
}

2、在JSP页面上的展现

  1)表单展现数据

<form id="ListForm" method="post">
    <%--迭代展现分页数据--%>        
    <table>
        <tr>
            <th>姓名</th>
            <th>账号</th>
        </tr>
        <c:forEach items="${pagination.recordList}" var="user">
            <tr>
                    <td>${user.name}</td>
                    <td>${user.account}</td>
            </tr>
        </c:forEach>
    </table>
    <br>
    <%--控制分页--%>
    <table>
        <tr>
            <td align="right">
                页数:<font color="blue">${pagination.currentPage}/${pagination.pageCount}</font>
                <input type="button"  onclick="paginationQuery('First')" value="首页">&nbsp;
                <input type="button"  onclick="paginationQuery('Previous')" <c:if test="${!pagination.hasPrePage}">disabled="disabled"</c:if> value="上一页">&nbsp;
                <input type="button"  onclick="paginationQuery('Next')"<c:if test="${!pagination.hasNextPage}">disabled="disabled"</c:if> value="下一页">&nbsp;
                <input type="button"  onclick="paginationQuery('Last')"value="尾页">&nbsp;
            </td>
        </tr>
    </table>
    
    <%--当前页--%>
    <input type="hidden" id="currentPage" name="currentPage" value="${pagination.currentPage}">
    <%--总页数--%>
    <input type="hidden" id="totalPageCount" value="${pagination.pageCount}">
</form>

  2)Javascript控制分页

//分页查询脚本
function paginationQuery(flag) {
    var value = parseInt($("#currentPage").val());
    if(flag=="First") {
        $("#currentPage").val(1);//首页
    }else if(flag=="Previous") {
        $("#currentPage").val(value-1);//上一页
    }else if(flag=="Next") {
        $("#currentPage").val(value+1);//下一页
    }else if(flag=="Last") {
        $("#currentPage").val($("#totalPageCount").val());//尾页
    }
    $("#ListForm").attr("action","user/userList.action").submit();
}

  

  

原文地址:https://www.cnblogs.com/luxh/p/2633844.html