简单分页

一个简单util类
package util;

public class Pager {
	private int currentPage;
	//初始化了一页能显示的item个数 这里item为comment
	private int pageSize=10;
	//totalSize是必须获得的数据,没有totalSize就不能算出到底该分为多少页   totalSize/pageSize=页数
	private int totalSize;
	private int totalPage;
	private boolean hasFirst;
	private boolean hasPrevious;
	private boolean hasNext;
	private boolean hasLast;
	public Pager(int currentPage,int totalSize){
		this.currentPage=currentPage;
		this.totalSize=totalSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}
	public int getTotalPage() {
		totalPage=totalSize/pageSize;
		if(totalSize%pageSize!=0)
			totalPage++;
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public boolean isHasFirst() {
		if(currentPage==1){
			return false;
		}
		return true;
	}
	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}
	public boolean isHasPrevious() {
		if(isHasFirst())
			return true;
		else
			return false;
	}
	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}
	public boolean isHasNext() {
		if(isHasLast())
			return true;
		else
			return false;
	}
	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}
	public boolean isHasLast() {
		if(currentPage == getTotalPage())
			return false;
		else
			return true;
	}
	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}
}
daoImpl中
@Override
	public List<Demand> getDemandByPaging(int currentPage, int pageSize) {
		Session session = getSession();
		String hql = "from Demand d order by d.id desc";
		Query query = session.createQuery(hql);
		int startRow = (currentPage - 1) * pageSize;
		query.setFirstResult(startRow);
		query.setMaxResults(pageSize);
		@SuppressWarnings("unchecked")
		List<Demand> demands = query.list();
		session.close();
		return demands;
	}

	/**
	 * 
	 */
	@Override
	public int getTotalDemand() {
		Session session = getSession();
		String hql = "from Demand d";
		Query query = session.createQuery(hql);
		@SuppressWarnings("unchecked")
		List<Demand> demands = query.list();
		return demands.size();
	}
action
	public String showAllDemand() throws Exception {
		context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		// 总评论条数 必须获得
		int totalSize = demandService.getTotalDemand();
		Pager pager = new Pager(currentPage, totalSize);
		// 评论列表
		@SuppressWarnings("unchecked")
		List<Demand> demands = demandService.getDemandByPaging(currentPage,
				pager.getPageSize());
		@SuppressWarnings("unchecked")
		Map<String, Object> request = (Map<String, Object>) context
				.get("request");
		request.put("demands", demands);
		request.put("totalSize", totalSize);
		request.put("pager", pager);
		return SUCCESS;
	}
jsp中
<pre name="code" class="plain"><div class="pager">
				<s:set name="pager" value="#request.pager" />
				<s:if test="#pager.hasFirst">
					<a href="showAllDemand.action?currentPage=1">首页</a>
				</s:if>
				<s:if test="#pager.hasPrevious">
					<a
						href="showAllDemand.action?currentPage=<s:property  value="#pager.currentPage-1"/>">上一页</a>
				</s:if>
				<s:if test="#pager.hasNext">
					<a
						href="showAllDemand.action?currentPage=<s:property  value="#pager.currentPage+1"/>">下一页</a>
				</s:if>
				<s:if test="#pager.hasLast">
					<a
						href="showAllDemand.action?currentPage=<s:property  value="#pager.totalPage"/>">尾页</a>
				</s:if>
				<br> 当前第
				<s:property value="#pager.currentPage" />
				页, 总共
				<s:property value="#pager.totalPage" />
				页
			</div>



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值