DAO層的代碼分頁(yè)代碼:
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;
}
分頁(yè)的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頁(yè)面:
<%@ 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="首頁(yè)" title="首頁(yè)"/>
<input type="button" class="beforePage commonPage" alt="上一頁(yè)" title="上一頁(yè)"/>
<input type="button" class="nextPage commonPage" alt="下一頁(yè)" title="下一頁(yè)"/>
<input type="button" class="lastPage commonPage" alt="尾頁(yè)" title="尾頁(yè)" />
<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">當(dāng)前第${requestScope.pm.currentPage }頁(yè)</span>
<span class="tc"> 相關(guān)資訊:${requestScope.pm.totalCount }條</span>
<span class="ps">每頁(yè)${requestScope.pm.pageSize }條 </span>
<span class="tp">共${requestScope.pm.totalPage}頁(yè)</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:下面有幾張圖片需要自己找...
/*點(diǎn)擊欄*/
.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中調(diào)用DAO層的方法,給currentPage和pageSize設(shè)置初始值,然后就返回一個(gè)list到你分頁(yè)的頁(yè)面迭代,以后就直接嵌套在分頁(yè)頁(yè)面中就行