分页详解

在做web的时候经常会用到的一种技术,下面解释如何实现它。本文示例为查询产品信息。
首先我们可以先设计一个配置页TurnPage类,

public class TurnPage {
	public int page = 1;            //页号
	public int rows = 10;           //每页有几行默认10
	public int allPages;			//总页数
	public int allRows;				//总行数
}

jsp页面发送请求给servlet,
示例jsp代码:

<%@ include file="../common/IncludeTop.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div id="Content">

	<div id="BackLink">
		<a href="<%=basePath%>/jsp/catalog/Main.jsp">Return
			to Main Menu</a>
	</div>

	<div id="Catalog">
		<table>
			<tr>
				<th>Product ID</th>
				<th>Name</th>
			</tr>
		<c:forEach items="${products}" var="p">
			<tr>
				<td><a href="<%=basePath%>ProductDetailSvl?productid=${p.productid}"</a></td>
				<td>${p.name}</td>
			</tr>
		</c:forEach>

		</table>

	</div>

</div>

<%@ include file="../common/IncludeBottom.jsp"%>



servlet接受请求时,将页号和每页显示几条传入servlet,
实例代码:

String page = request.getParameter("page");
		int iPage = 1;//默认页号
		try {
            iPage = Integer.parseInt(page);	//jsp页面传入的页号
            if(iPage<1){	//设置其不能够跳转到负页
            	iPage = 1;
            }
		} catch (Exception e) {
		}
		
		String productid=request.getParameter("productid");//从jsp页面获取产品id
		List<ProductDetail> listpd=new ArrayList<>();
		ProductDetailBiz biz=new ProductDetailBiz();
		try {
			TurnPage tp = new TurnPage();
			tp.rows = 8;                          //首页设置每页显示8行
			tp.page = iPage;						//页号
			listpd=biz.getProductDetail(productid,tp);//将产品id和封装好的页面信息传入逻辑层,返回的值为此产品id下的所有产品(集合)
			request.setAttribute("productid", productid);
			request.setAttribute("page", iPage);
			request.setAttribute("allRows", tp.allRows);
			request.setAttribute("allPage", tp.allPages);
			request.setAttribute("listpd", listpd);
			request.getRequestDispatcher("/jsp/catalog/Product.jsp").forward(request, response);
		} catch (Exception e) {
		    request.setAttribute("msg", "网络连接错误,请和管理员联系");
		    request.getRequestDispatcher("/err/error.jsp").forward(request, response);
	   }
	}

servlet再传递给逻辑层,
逻辑层代码示例:

public List<ProductDetail> getProductDetail(String productid,TurnPage tp)throws Exception{
		List<ProductDetail> listpd=new ArrayList<>();
		ProductDetailDao dao=new ProductDetailDao();
	 try {
		 listpd= dao.getProductDetail(productid,tp);
	} finally {
		dao.closeConnection();
	}
		return listpd;
	 
	}

逻辑层传递给数据库层,此时要注意数据库语句的写法。
数据库层带密码示例:

public List<ProductDetail> getProductDetail(String productid,TurnPage tp) throws Exception{

		String sql="select  i.itemid,i.productid,i.listprice,p.descn from  product p,item i  where i.productid=?";
		List<ProductDetail> listpd=new ArrayList<>();
		sql += " limit " + ((tp.page-1)  * (tp.rows))+ "," + tp.rows;//mysql的索引从0开始,limit(起始条,结束条)
		this.openConnection();
		PreparedStatement ps= this.conn.prepareStatement(sql);
		ps.setString(1, productid);
		ResultSet rs=ps.executeQuery();
		while(rs.next()){
			ProductDetail pDetail=new ProductDetail();
			pDetail.setItemid(rs.getString("itemid"));
			pDetail.setProductid(rs.getString("productid"));
			pDetail.setListprice(rs.getDouble("listprice"));
			pDetail.setDescn(rs.getString("descn"));
			listpd.add(pDetail);
		}
		return listpd;	 
		}

显示的jsp页面代码护理如下:

<%@ include file="../common/IncludeTop.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 



<div id="Content">
	<div id="BackLink">
		<a href="<%=request.getContextPath()%>/jsp/catalog/Category.jsp">Return
			to FISH</a>
	</div>

	<div id="Catalog">

		<h2>Koi</h2>

		<table>
			<tr>
				<th>Item ID</th>
				<th>Product ID</th>
				<th>Description</th>
				<th>List Price</th>
				<th>&nbsp;</th>
			</tr>
			<c:forEach items="${listpd}" var="pd">
				<tr>
					<td><a href="<%=request.getContextPath()%>/ItemSvl?itemid=${pd.itemid}">${pd.itemid}</a></td>
					<td>${pd.productid}</td>
					<td>${pd.descn}</td>
					<td>${pd.listprice}</td>
					<td><a href="<%=basePath%>CartAddSvl?itemid=${pd.itemid}" class="Button">Add to Cart</a></td>
				</tr>
			</c:forEach>
			
			<tr >
				<td colspan=8>
				<table id="tblTurnPage" cellSpacing="0" cellPadding="1" width="100%" border="0" style="font-family:arial;color:red;font-size:12px;">	    		
	    			<tr>
	    				<td>总记录数:${allRows}</td> 
	    				<td>总页数:${allPage}</td>
	    				<td>当前页:${page}</td>
	    				<td><a href="<%=basePath%>ProductDetailSvl?page=1&productid=${productid}">首页|</a>
	    				    <a href="<%=basePath%>ProductDetailSvl?page=${page-1}&productid=${productid}">《前页|</a>
	    				    <a href="<%=basePath%>ProductDetailSvl?page=${page+1}&productid=${productid}">后页》|</a>
	    				    <a href="<%=basePath%>ProductDetailSvl?page=${allPage}&productid=${productid}">末页|</a></td>
	    				<td >跳转到:第<input type="text" size="3" >页<input type="button" value="go"></td>
	    			</tr>	    		
	    		</table>
				</td>
			</tr>
		</table>

	</div>

</div>

<%@ include file="../common/IncludeBottom.jsp"%>

到此为止,数据库的分页就结束了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值