struts+hibernate+pager-taglib 分页

pager-taglib-2.0下载
[URL]http://jsptags.com/tags/navigation/pager/index.jsp[/URL]
 
把pager-taglib.jar放进程序里的/WEB-INF/
 
然后我们的pager-taglib-2.0就算配置成功了
 
现在我们来看一下程序
 
//分页查询所有文章
public List findAll(int pageoffset) {
log.debug("finding all Articles instances");
try {
Integer pagesize=Constant.DEFAULT_MAX_PAGE_SIZE; //一个分页可以显示多少条数据
DetachedCriteria date=DetachedCriteria.forClass(Articles.class); //要查找的实体
List list=getHibernateTemplate().findByCriteria(date,pageoffset,pagesize);
return list;
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
 
以上是Hibernate的查找
=================================================
要在页面上使上分页。先要取得以下数据。为了程序简单一点我写了以下一个init方法
 
//分页初始化 //count为你的数据总量//pageNumber为你当前的页面
public void pageInit(int count,int pageNumber,HttpServletRequest request){
int pageSize=Constant.DEFAULT_MAX_PAGE_SIZE;
int totalPage = count % pageSize == 0 ? count / pageSize : (count / pageSize + 1);
request.setAttribute(Constant.PAGE_COUNT,count);
request.setAttribute(Constant.PAGE_TOTAL,totalPage);
request.setAttribute(Constant.PAGE_SIZE, Constant.DEFAULT_MAX_PAGE_SIZE);
request.setAttribute(Constant.PAGE_NUMBER, pageNumber);
}
//==================================================
action
 
// 显示管理文章页面
public ActionForward showAdminArticles(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
Users loginUser = userService.getLoginUser(request);
if (loginUser == null) {
request.setAttribute(Constant.ERROR_MESSAGE, "用户未登录");
return mapping.findForward(Constant.ERROR);
}
if (!userService.isCreateBloged(loginUser.getId())) {
request.setAttribute(Constant.ERROR_MESSAGE, "用户未开通Blog");
return mapping.findForward(Constant.ERROR);
}
// 显示登录用户的文章分类
request.setAttribute(ConstantHibernate.CATEGORIES, categoryService.showCategories(loginUser.getId()));
// 显示所有用户文章(管理员)
if (userService.isAdmin(request)) {
int pageOffset = 0;
if (request.getParameter(Constant.PAGE_OFFSET) != null) {
pageOffset = Integer.parseInt(request
.getParameter(Constant.PAGE_OFFSET));
}
request.setAttribute("all" + ConstantHibernate.ARTICLES,
articlesService.getAllAritcles(pageOffset));
}
// 显示用户的所有文章
request.setAttribute(ConstantHibernate.ARTICLES, articlesService.getUserArticles(loginUser.getId()));
 
//分页功能实现
int count = articlesService.getAllAritcles().size();
Integer pageNumber = 1;
if (request.getParameter(Constant.PAGE_NUMBER) != null) {
pageNumber = Integer.parseInt(request.getParameter(Constant.PAGE_NUMBER));
}
articlesService.pageInit(count, pageNumber, request);
return mapping.findForward(Constant.SHOW_ADMIN_ARTICLES);
}
 
//==================================================
页面
<!-- 数据显示部分 -->
<c:if test="${!empty allarticles}">
 
 
 
管理所有文章:<br />
 
<hr />
<table border="1">
<thead id="df">
<tr>
<td>ID</td>
<td>文章标题</td>
<td>文章分类</td>
<td width="90px">文章所属用户</td>
<td width="90px">文章修改</td>
<td width="90px">文章删除</td>
</tr>
</thead>
<c:forEach var="articles" items="${allarticles}">
<tr>
<td>
<c:out value="${articles.id}"></c:out>
</td>
<td>
<a href="<%=basePath%>showArticles.do?&id=${articles.id}">${articles.title}</a>
</td>
<td>
<c:out value="${articles.categories.catename}"></c:out>
</td>
<td>
<c:out value="${articles.users.username}"></c:out>
</td>
<td>
<a href="<%=basePath%>showEditArticles.do?&id=${articles.id}">修改</a>
</td>
<td>
<a href="<%=basePath%>delArticles.do?&id=${articles.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
 
<hr>
<pg:pager items="${pageCount}" maxPageItems="${pageSize}" maxIndexPages="${5}" index="center" isOffset="<%= true %>"
export = "pageOffset,currentPageNumber=pageNumber "
scope="request" url="${basePath}showAdminArticles.do">
共<c:out value="${pageTotal}"></c:out>页
<pg:index>
<pg:first>
<a href="${pageUrl}&page=${pageNumber}">首页</a>
</pg:first>
<pg:prev>
<a href="${pageUrl}&page=${pageNumber}">上一页</a>
</pg:prev>
<pg:next>
<a href="${pageUrl}&page=${pageNumber}">下一页</a>
</pg:next>
<pg:last>
<a href="${pageUrl }&page=${pageNumber}">末页</a>
</pg:last>
共${pageCount}条记录
</pg:index>
 
</pg:pager>
</c:if> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值