此分页的步骤完全是按照:见我的博客点击打开链接,这个图的步骤来写分页的
1.写PageBean类,见我的博客点击打开链接
2.写Action类,将jsp中需要的类放到指定的作用域中,这里我放到了值栈中,因为这样就可以直接使用属性了。
/*显示单个列表*/
public String show(){
//准备数据forum
Forum forum=forumService.getById(model.getId());
ActionContext.getContext().put("forum", forum);
//准备数据topicList
// List<Topic> topicList=topicService.findByForum(forum);
// ActionContext.getContext().put("topicList", topicList);
//准备分页的数据version-1
PageBean pageBean=topicService.getPageBeanByForum(pageNum,forum);
//放到值栈中
ActionContext.getContext().getValueStack().push(pageBean);
return "show";
}
3.写相应的service层.这里主要限制性查询语句的书写。这里的hql语句与mysql的limit(1,10)语句类似
@Deprecated
public PageBean getPageBeanByForum(int pageNum, Forum forum) {
int pageSize = Configuration.getPageSize();
//查询一页的数据列表
List list=getSession().createQuery("from Topic t where t.forum=? order by (case t.type when 2 then 2 else 0 end) desc,t.lastUpdateTime desc")
.setParameter(0, forum)
.setFirstResult((pageNum-1)*pageSize)//limit 0,10 //这里是当前页的第一个所在的记录数
.setMaxResults(pageSize)//显示的记录条数
.list();
Long count=(Long) getSession().createQuery("select count(*) from Topic t where t.forum=?").setParameter(0, forum).uniqueResult();
return new PageBean(pageNum, pageSize, count.intValue(), list);
}
4.分页信息的公共代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<div id=PageSelectorBar>
<div id=PageSelectorMemo>
页次:${currentPage}/${pageCount}页
每页显示:${pageSize}条
总记录数:${recordCount}条;
</div>
<div id=PageSelectorSelectorArea>
<!--
<IMG SRC="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage2.png"/>
-->
<a href="javascript:gotoPage(1)" title="首页" style="cursor: hand;">
<img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage.png"/>首页</a>
<s:iterator begin="%{beginPageIndex}" end="%{endPageIndex}" var="num">
<span class="PageSelectorNum" style="cursor: hand;" onClick="gotoPage(${num});">${num}</span>
</s:iterator>
<!--
<IMG SRC="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage2.png"/>
-->
<a href="javascript:gotoPage(${pageCount })" title="尾页" style="cursor: hand;">
<img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage.png"/>尾页</a>
转到:
<select id="pn" οnchange="gotoPage(this.value)">
<s:iterator begin="1" end="%{pageCount}" var="num">
<option value="${num}">${num}</option>
</s:iterator>
</select>
<script type="text/javascript">
$("#pn").val("${currentPage}");
</script>
<script type="text/javascript">
function gotoPage(pageNum){
//window.location.href="forum_show.do?id=${id}&pageNum="+pageNum;
alert("请写gotoPage函数!");
}
</script>
</div>
</div>
5.在jsp页面中include指定的文件并写gotoPage(pageNum)函数
<!--分页信息-->
<!-- 导入公共的分页信息代码 -->
<%@ include file="/WEB-INF/jsp/public/pageView.jspf" %>
<script type="text/javascript">
function gotoPage(pageNum){
window.location.href="forum_show.do?id=${id}&pageNum="+pageNum;
}
</script>
6.大概的分页效果示意图: