JAVA工具类(2)- 获取分页起止页大小和分页大小PageNumberUtil


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;


public final class PageNumberUtil {

    /**
     * 默认的分页大小
     */
    public static final int pageSize = 15;
    /**
     * 默认的分页大小
     */
    public static final int PAGE_SIZE_50 = 50;
    /**
     * 默认起止页大小
     */
    public static final int currentPage = 1;

    /**
     * 从HashMap里获取pageSize
     * 
     * @param HashMap
     * @return
     */
    public static int getPageSizeFromParams(Map<String, Object> params) {

        int pageSizeReturn = pageSize;
        if (params.get("rows") == null || String.valueOf(params.get("rows")).trim().length() == 0) {
            Object strPageSize = MapUtils.getString(params,"pageSize");
            if (null == strPageSize) {
                return pageSize;
            }

            try {
                pageSizeReturn = Integer.parseInt(String.valueOf(strPageSize));
            } catch (NumberFormatException e) {
                pageSizeReturn = pageSize;
            }
        } else {
            int rows = PageNumberUtil.pageSize;
            try {
                rows = Integer.parseInt(String.valueOf(params.get("rows")));
            } catch (NumberFormatException e) {
                pageSizeReturn = pageSize;
            }
            pageSizeReturn = rows;
        }

        return pageSizeReturn;
    }

    /**
     * 从request里获取pageSize
     * 
     * @param request
     * @return
     */
    public static int getPageSizeFromRequest(HttpServletRequest request) {

        Object strPageSize = request.getParameter("pageSize");
        if (null == strPageSize) {
            return pageSize;
        }
        int pageSizeReturn;
        try {
            pageSizeReturn = Integer.parseInt(String.valueOf(strPageSize));
        } catch (NumberFormatException e) {
            pageSizeReturn = pageSize;
        }
        return pageSizeReturn;
    }

    /**
     * 从HashMap里获取currentPage
     * 
     * @param HashMap
     * @return
     */
    public static int getCurrentPageFromParams(Map<String, Object> params) {

        Object strCurrentPage = MapUtils.getString(params,"page");
        if (null == strCurrentPage) {
            return currentPage;
        }
        int currentPageReturn;
        try {
            currentPageReturn = Integer.parseInt(String.valueOf(strCurrentPage));
        } catch (NumberFormatException e) {
            currentPageReturn = currentPage;
        }
        return currentPageReturn;
    }

    /**
     * 从HashMap里获取currentPage
     * 
     * @param HashMap
     * @return
     */
    public static int getPageFromParams(Map<String, Object> params) {

        Object strCurrentPage = MapUtils.getString(params,"page");
        if (null == strCurrentPage) {
            return currentPage;
        }
        int currentPageReturn;
        try {
            currentPageReturn = Integer.parseInt(String.valueOf(strCurrentPage));
        } catch (NumberFormatException e) {
            currentPageReturn = currentPage;
        }
        return currentPageReturn;
    }


    /**
     * 从request里获取currentPage
     * 
     * @param request
     * @return
     */
    public static int getCurrentPageFromRequest(HttpServletRequest request) {

        Object strCurrentPage = request.getParameter("currentPage");
        if (null == strCurrentPage) {
            return currentPage;
        }
        int currentPageReturn;
        try {
            currentPageReturn = Integer.parseInt(String.valueOf(strCurrentPage));
        } catch (NumberFormatException e) {
            currentPageReturn = currentPage;
        }
        return currentPageReturn;
    }

    /**
     * 从HashMap里获取pageSize
     * 
     * @param HashMap
     * @return
     */
    public static int getPageSizeFromParams(Map<String, Object> params, String pageSizeName) {

        Object strPageSize = MapUtils.getString(params,pageSizeName);
        if (null == strPageSize) {
            return pageSize;
        }
        int pageSizeReturn;
        try {
            pageSizeReturn = Integer.parseInt(String.valueOf(strPageSize));
        } catch (NumberFormatException e) {
            pageSizeReturn = pageSize;
        }
        return pageSizeReturn;
    }

    /**
     * 从request里获取pageSize
     * 
     * @param request
     * @return
     */
    public static int getPageSizeFromRequest(HttpServletRequest request, String pageSizeName) {

        Object strPageSize = request.getParameter(pageSizeName);
        if (null == strPageSize) {
            return pageSize;
        }
        int pageSizeReturn;
        try {
            pageSizeReturn = Integer.parseInt(String.valueOf(strPageSize));
        } catch (NumberFormatException e) {
            pageSizeReturn = pageSize;
        }
        return pageSizeReturn;
    }

    /**
     * 从HashMap里获取currentPage
     * 
     * @param HashMap
     * @return
     */
    public static int getCurrentPageFromParams(Map<String, Object> params, String currentPageName) {

        Object strCurrentPage = MapUtils.getString(params,currentPageName);
        if (null == strCurrentPage) {
            return currentPage;
        }
        int currentPageReturn;
        try {
            currentPageReturn = Integer.parseInt(String.valueOf(strCurrentPage));
        } catch (NumberFormatException e) {
            currentPageReturn = currentPage;
        }
        return currentPageReturn;
    }

    /**
     * 从request里获取currentPage
     * 
     * @param request
     * @return
     */
    public static int getCurrentPageFromRequest(HttpServletRequest request, String currentPageName) {

        Object strCurrentPage = request.getParameter(currentPageName);
        if (null == strCurrentPage) {
            return currentPage;
        }
        int currentPageReturn;
        try {
            currentPageReturn = Integer.parseInt(String.valueOf(strCurrentPage));
        } catch (NumberFormatException e) {
            currentPageReturn = currentPage;
        }
        return currentPageReturn;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值