java web 中分页技术


        java web中的分页技术,就是显示用户查询的数据的时候,规定每页显示多少条数据,

      即如下所示:

        这样的话,用户可以自己选择要查看哪一页,更加的人性化,一次只查询一页,不用查询所有页,

     <1>首先分页得知道当前页码(pagecode),由用户选定,然后提交给后台,默认为第一页,

     <2>其次得知道总页数(totalpages),总页数可以通过总记录数/每页的记录数得到,

     <3>总记录数(totalrecord)可以通过执行sql语句(select count(*) from customer)得到,

     <4>每页记录数由用户决定,然后就可以计算出总页数,

      <5>还得知道当前页数据(beanutils),通过执行sql语句(select * from customer limit x,y);

     把这些属性封装到一个pagebean中,首先可以得到首页 上一页 下一页 尾页

    在jsp中显示为:

 

 <c:forEach items="${pb.beanList}" var="cstm">
	<tr>
		<td>${cstm.cname}</td>
		<td>${cstm.gender}</td>
		<td>${cstm.birthday}</td>
		<td>${cstm.cellphone}</td>
		<td>${cstm.email}</td>
		<td>${cstm.description}</td>
		<td>
			<a href="<c:url value='/servlet/CustomerServlet?method=preEdit&cid=${cstm.cid}'/>">编辑</a>
			<a href="<c:url value='/msg.jsp'/>">删除</a>
		</td>
	</tr>
	</c:forEach>

 

 <center>
第${pb.pc}页/共${pb.tp}页
<a href="<c:url value='/servlet/CustomerServlet?method=findAll&pc=1'/>">首页</a>
<c:if test="${pb.pc>1}">
<a href="<c:url value='/servlet/CustomerServlet?method=findAll&pc=${pb.pc-1}'/>">上一页</a>
</c:if>
<c:if test="${pb.pc<pb.tp}">
<a href="<c:url value='/servlet/CustomerServlet?method=findAll&pc=${pb.pc+1}'/>">下一页</a>
</c:if>
<a href="<c:url value='/servlet/CustomerServlet?method=findAll&pc=${pb.tp}'/>">尾页</a>
</center>
在servlet中如下

public String findAll(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
	   //得到pc(当前页码)
		int pc = getPc(request);
		//设定ps
		int ps = 10;//设定每页记录为10
		//调用service,得到pageBean
		PageBean<Customer> pb = customerservice.findAll(pc,ps);
		//保存到request域中
		request.setAttribute("pb", pb);
		//转发到list.jsp
		return "f:/list.jsp";
	}

在dao中如下:

public PageBean<Customer> findAll(int pc,int ps){
		try{
		//得到PageBean对象pb
		PageBean<Customer> pb = new PageBean<Customer>();
		//把pc,ps设置给pb
		pb.setPc(pc);
		pb.setPs(ps);
		//得到tr,设置给pb
		String sql = "select count(*) from t_customer";
		Number num = (Number)qr.query(sql, new ScalarHandler());
		int tr = num.intValue();
		pb.setTr(tr);
		
		sql = "select * from t_customer order by cname limit ?,?";
		List<Customer> beanList = qr.query(sql, new BeanListHandler<Customer>(Customer.class),(pc-1)*ps,ps);
		pb.setBeanList(beanList);
		return pb;
		}catch(SQLException e){
			throw new RuntimeException(e);
		}
		
	}

其次为了显示页码列表,规定最多显示10个页码,当前页在页码中的位置为6,则begin=当前页-5,end=当前页+4;

其中特殊情况为总页数小于10,则begin=1,end=总页数

头溢出:当begin<1时,让begin=1;

尾溢出:当end>总页数时,end=总页数

代码如下:

<c:choose>

<c:when test="${pb.tp<10}">
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="${pb.tp}"></c:set>
</c:when>

<c:otherwise>
<c:set var="begin" value="${pb.pc-5}"></c:set>
<c:set var="end" value="${pb.pc+4}"></c:set>
<!-- 头溢出 -->
<c:if test="${begin<1}">
<c:set var="begin" value="1"></c:set>
<c:set var="end" value="10"></c:set>
</c:if>
<!-- 尾溢出 -->
<c:if test="${end>pb.tp}">
<c:set var="begin" value="${pb.tp-9}"></c:set>
<c:set var="end" value="${pb.tp}"></c:set>
</c:if>
</c:otherwise>
</c:choose>

<!-- 循环显示页码列表 -->
<c:forEach var="i" begin="${begin}" end="${end}">
<c:choose>
<c:when test="${i eq pb.pc}">
[${i}]
</c:when>
<c:otherwise>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值