java后台通用分页代码,兼容前端

java后台通用分页代码,兼容前端

引用包

import java.util.List;
import net.sf.json.JSONObject;

声明属性

private List list; // 分装分页数据
private int totalPage; // 总页数
private int count; // 总记录数
private int pageSize; // 页的大小
private int pageNum; // 用户想看的页码
private int startIndex; // 代表用户想看的页的数据从数据库哪个地方开始取
private int endIndex; // 代表用户想看的页的数据从数据库哪个地方截止取
private JSONObject pageInfo;// 分页信息

初始化对象,入参JSON对象和数据

	/**
	 * 初始化,传入页面显示数量,当前所在页码
	 * 
	 * @auther zhuteng
	 * @time 2019年7月25日
	 */
	public PageLimit(JSONObject pageInfo, List list) throws Exception
	{
		this.pageSize = pageInfo.getInt("pageSize");
		this.list = list;
		this.count = this.list.size();
		// 计算分页
		this.totalPage = this.count / this.pageSize;
		int num = this.count % this.pageSize;
		if (num > 0)
		{
			this.totalPage++;
		}
		if (pageInfo.get("pageNum") != null && pageInfo.get("startIndex") != null)
		{
			this.pageNum = pageInfo.getInt("pageNum");
			this.startIndex = pageInfo.getInt("startIndex") - 1;
			this.endIndex = this.pageNum * this.pageSize;// 截止下标位置
		}
		else if (pageInfo.get("pageNum") != null)// 按普通分页计算
		{
			this.pageNum = pageInfo.getInt("pageNum");
			this.endIndex = this.pageNum * this.pageSize;// 截止下标位置
			this.startIndex = this.endIndex - this.pageSize;// 起始下标位置
		}
		else if (pageInfo.get("startIndex") != null)// 前端分页,根据起始位置计算
		{
			this.startIndex = pageInfo.getInt("startIndex") - 1;
			this.endIndex = this.startIndex + this.pageSize;
			if (this.endIndex > count)
			{
				this.endIndex = count;
			}
			if (this.startIndex < this.pageSize && this.startIndex <= 1)
			{
				this.pageNum = 1;
			}
			else if (this.startIndex > this.pageSize)
			{
				this.pageNum = this.startIndex / this.pageSize + 1;
			}
			else
			{
				this.pageNum = 2;
			}
		}
		else
		{
			throw new Exception("未设置页码或数据起始下标");
		}
	}

返回数据方法,截取数组数据

/**
	 * 获取数据,并计算数据量,返回数据
	 * 
	 * @auther zhuteng
	 * @time 2019年7月25日
	 */
	public List getList() throws Exception
	{
		if (startIndex > count)// 起始下标大于总数据量,返回空
		{
			this.startIndex = count;
			this.endIndex = count;
			return list.subList(count, count);
		}
		else if (endIndex > count)// 截止数据下标大于总数据量,返回剩余数据
		{
			this.endIndex = count;
			return list.subList(startIndex, count);
		}
		else
		{
			return list.subList(startIndex, endIndex);
		}
	}

##返回分页信息

	/**
	 * 获取分页信息
	 * 
	 * @auther zhuteng
	 * @time 2019年7月25日
	 */
	public JSONObject getPageInfo() throws Exception
	{
		pageInfo = new JSONObject();// 实例化对象
		pageInfo.put("count", this.count);// 总数量
		pageInfo.put("totalPage", this.totalPage);// 总页数
		pageInfo.put("pageNum", this.pageNum);// 当前页码
		pageInfo.put("pageSize", this.pageSize);// 当前页显示数量
		pageInfo.put("startIndex", this.startIndex + 1);// 当前页显示数量
		pageInfo.put("endIndex", this.endIndex);// 当前页显示数量
		return pageInfo;
	}

以下为源代码汇总代码块

package com.hms.common.constant;

import java.util.List;

import net.sf.json.JSONObject;

/**
 * 分页公共通用对象
 * 
 * @auther zhuteng
 * @time 2019年7月25日
 */
public class PageLimit
{
	private List list; // 分装分页数据
	private int totalPage; // 总页数
	private int count; // 总记录数
	private int pageSize; // 页的大小
	private int pageNum; // 用户想看的页码
	private int startIndex; // 代表用户想看的页的数据从数据库哪个地方开始取
	private int endIndex; // 代表用户想看的页的数据从数据库哪个地方截止取
	private JSONObject pageInfo;// 分页信息
	
	/**
	 * 初始化,传入页面显示数量,当前所在页码
	 * 
	 * @auther zhuteng
	 * @time 2019年7月25日
	 */
	public PageLimit(JSONObject pageInfo, List list) throws Exception
	{
		this.pageSize = pageInfo.getInt("pageSize");
		this.list = list;
		this.count = this.list.size();
		// 计算分页
		this.totalPage = this.count / this.pageSize;
		int num = this.count % this.pageSize;
		if (num > 0)
		{
			this.totalPage++;
		}
		if (pageInfo.get("pageNum") != null && pageInfo.get("startIndex") != null)
		{
			this.pageNum = pageInfo.getInt("pageNum");
			this.startIndex = pageInfo.getInt("startIndex") - 1;
			this.endIndex = this.pageNum * this.pageSize;// 截止下标位置
		}
		else if (pageInfo.get("pageNum") != null)// 按普通分页计算
		{
			this.pageNum = pageInfo.getInt("pageNum");
			this.endIndex = this.pageNum * this.pageSize;// 截止下标位置
			this.startIndex = this.endIndex - this.pageSize;// 起始下标位置
		}
		else if (pageInfo.get("startIndex") != null)// 前端分页,根据起始位置计算
		{
			this.startIndex = pageInfo.getInt("startIndex") - 1;
			this.endIndex = this.startIndex + this.pageSize;
			if (this.endIndex > count)
			{
				this.endIndex = count;
			}
			if (this.startIndex < this.pageSize && this.startIndex <= 1)
			{
				this.pageNum = 1;
			}
			else if (this.startIndex > this.pageSize)
			{
				this.pageNum = this.startIndex / this.pageSize + 1;
			}
			else
			{
				this.pageNum = 2;
			}
		}
		else
		{
			throw new Exception("未设置页码或数据起始下标");
		}
	}

	/**
	 * 获取数据,并计算数据量,返回数据
	 * 
	 * @auther zhuteng
	 * @time 2019年7月25日
	 */
	public List getList() throws Exception
	{
		if (startIndex > count)// 起始下标大于总数据量,返回空
		{
			this.startIndex = count;
			this.endIndex = count;
			return list.subList(count, count);
		}
		else if (endIndex > count)// 截止数据下标大于总数据量,返回剩余数据
		{
			this.endIndex = count;
			return list.subList(startIndex, count);
		}
		else
		{
			return list.subList(startIndex, endIndex);
		}
	}

	/**
	 * 获取分页信息
	 * 
	 * @auther zhuteng
	 * @time 2019年7月25日
	 */
	public JSONObject getPageInfo() throws Exception
	{
		pageInfo = new JSONObject();// 实例化对象
		pageInfo.put("count", this.count);// 总数量
		pageInfo.put("totalPage", this.totalPage);// 总页数
		pageInfo.put("pageNum", this.pageNum);// 当前页码
		pageInfo.put("pageSize", this.pageSize);// 当前页显示数量
		pageInfo.put("startIndex", this.startIndex + 1);// 当前页显示数量
		pageInfo.put("endIndex", this.endIndex);// 当前页显示数量
		return pageInfo;
	}
}

代码调用实例

		JSONObject jsonObject = new JSONObject();
		try
		{
			List<ProductDetailsBean> productDetailsList = new ArrayList<ProductDetailsBean>();
			productDetailsList = hcompanyproductdao.get_ProductDetailBean_ByInfo(oneBean);
			// 分页数据,如果用户不要求分页,默认不做分页处理
			if (oneBean.getPageInfo() != null)
			{
				List<ProductDetailsBean> pageList = new ArrayList<ProductDetailsBean>();
				pageList.addAll(productDetailsList);// 添加数据到临时容器
				productDetailsList.clear();// 清空栈内存
				PageLimit page = new PageLimit(oneBean.getPageInfo(), pageList);
				productDetailsList.addAll(JSONArray.fromObject(page.getList()));
				jsonObject.put("pageInfo", page.getPageInfo());
			}
			jsonObject.put("productDetailsList", productDetailsList);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		InteractiveWithAjax printWriteAjax = new InteractiveWithAjax();
		printWriteAjax.printWriter(jsonObject, response);

写的不好,多多包含

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TorZhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值