`

分页栏

阅读更多
DAO层的代码分页代码:
public PageModel findByPageModel(String hql,PageModel pm) {
		pm.setTotalCount(this.getHibernateTemplate().find(hql).size());
		pm.setGoToHref(ServletActionContext.getRequest().getServletPath().replace("/",""));
		int totalCount = pm.getTotalCount();
		int pageSize = pm.getPageSize();
		int totalPage = (totalCount+pageSize-1)/pageSize ;
		int currentPage = pm.getCurrentPage() ;
		pm.setTotalPage(totalPage);
		int offset = (currentPage-1)*pageSize;
		pm.setList(this.getSession().createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list());
		return pm;
	}


分页的JAVABEAN:
public class PageModel {
	private int currentPage;
	private int pageSize;
	private int totalCount;
	private int totalPage;
	private List list ;
	private String goToHref;
	
	public int getCurrentPage() {
		if(currentPage<=0) currentPage=1;
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		if(pageSize<=0) pageSize=10;
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public String getGoToHref() {
		return goToHref;
	}
	public void setGoToHref(String goToHref) {
		this.goToHref = goToHref;
	}
}


JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<link rel="stylesheet" type="text/css" href="<%=basePath %>findByHql/pagingBar/css/pagingBar.css">

	<input type="button" class="firstPage commonPage"  alt="首页"   title="首页"/>
	<input type="button" class="beforePage commonPage" alt="上一页" title="上一页"/>
	<input type="button" class="nextPage commonPage"   alt="下一页" title="下一页"/>
	<input type="button" class="lastPage commonPage"   alt="尾页"   title="尾页" />
	
	<input type="hidden" id="currentPage" value="${requestScope.pm.currentPage }" />
	<input type="hidden" id="totalPage"	value="${requestScope.pm.totalPage }" />
	<input type="hidden" id="goToHref"	value="${requestScope.pm.goToHref }" />
	
	<span class="cp">当前第${requestScope.pm.currentPage }页</span>
	<span class="tc"> 相关资讯:${requestScope.pm.totalCount }条</span>
	<span class="ps">每页${requestScope.pm.pageSize }条 </span>
	<span class="tp">共${requestScope.pm.totalPage}页</span>

<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<script type="text/javascript">
	(function($) {
		var currentPage = parseInt($('#currentPage').val());
		var totalPage = parseInt($('#totalPage').val());
		var toHref = $('#goToHref').val();
		$('.firstPage').bind('click', function() {
			goToHref(1);
		});
		$('.nextPage').bind('click', function() {
			if (currentPage >= totalPage)
				goToHref(totalPage);
			else
				goToHref(currentPage + 1);
		});
		$('.beforePage').bind('click', function() {
			if (currentPage <= 1)
				goToHref(1);
			else
				goToHref(currentPage - 1);
		});
		$('.lastPage').bind('click', function() {
			goToHref(totalPage);
		});
		function goToHref(cp) {
			document.location.href = toHref+"?currentPage=" + cp;
		}
	})(jQuery)
</script>


CSS:下面有几张图片需要自己找...
/*点击栏*/
.commonPage{
	width: 16px;
	height: 16px;
	border: none;
	cursor: pointer;
}
.firstPage{
	background: url("../images/page-first.png") no-repeat;
}

.nextPage{
	background: url("../images/page-next.png") no-repeat;
}

.beforePage{
	background: url("../images/page-prev.png") no-repeat;
}

.lastPage{
	background: url("../images/page-last.png") no-repeat;
}

/*显示栏*/
.cp,.tc,.ps,.tp{
	font-size: 14px;
}

在action中调用DAO层的方法,给currentPage和pageSize设置初始值,然后就返回一个list到你分页的页面迭代,以后就直接嵌套在分页页面中就行
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics