分页栏

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值