如何优雅的写一个分页代码(三)

此分页的步骤完全是按照:见我的博客点击打开链接,这个图的步骤来写分页的

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.大概的分页效果示意图:





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值