JSP servlet分页

public interface ProductDao {
	public long queryCategoryProductsCount(String cid);
	public List<Product> queryProductsByCid(String cid, int pageIndex, int pagesize);
}

ProductDaoImpl implements ProductDao

	//获取种类中商品总条数
	@Override
	public long queryCategoryProductsCount(String cid) {
		String sql = "select count(*) from product where cid = ?";
		try {
			return (long)queryRunner.query(sql, new ScalarHandler(), cid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	//获取种类中当前页码下商品数据
	@Override
	public List<Product> queryProductsByCid(String cid, int pageIndex, int pagesize) {
		String sql = "select pid, pname, market_price, pimage " + 
				 	 "from product " + 
				     "where cid = ? " +
				 	 "order by pdate desc " +
				 	 "limit ? , ?";
		try {
			return queryRunner.query(sql, new BeanListHandler<>(Product.class), cid, pageIndex, pagesize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

public PageBean queryProductsByCid(String cid, String page, String pageSize);

ProductServiceImpl implements ProductService

	@Override
	public PageBean<Product> queryProductsByCid(String cid, String page, String pageSize) {
		PageBean<Product> pageBean = new PageBean<>();
		
		int pagesize = Integer.parseInt(pageSize);		//页面显示数
//		int pageIndex = 0;								//默认页码为1  -  索引为0
//		if(page != null) {								
//			pageIndex = (Integer.parseInt(page) - 1) * pagesize
//		}
		
		//pager-taglib 分页标签  -  开始条数索引
		int pageIndex = 0;
		if(page != null) {
			pageIndex = Integer.parseInt(page);
		}
		
		pageBean.setCurrentPage(pageIndex);
		pageBean.setPageSize(pagesize);
		
		//种类中商品总条数
		long total = productDao.queryCategoryProductsCount(cid);
		pageBean.setTotal(total);
		
		int totalPage = (int)(total % pagesize == 0 ? total / pagesize : total / pagesize + 1);
		pageBean.setTotalPage(totalPage);
		
		List<Product> list = productDao.queryProductsByCid(cid, pageIndex, pagesize);
		pageBean.setList(list);
		
		return pageBean;
	}

	private void queryProsByCid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//查询种类编号
		String cid = request.getParameter("cid");
		
		/*
		 * 查询页面数据  -  分页查询
		 * 	1: 页面显示数   -    全局初始化参数
		 * 	2:查询的当前页码
		 * 		点击页码 - 获取页码 - okay
		 * 		点击种类 - 页码null - 默认为第一页
		 */
		String pageSize = this.getServletContext().getInitParameter("pageSize");
//		String page = request.getParameter("page");					//页码
		
		//pager-taglib  - 分页标签
		String page = request.getParameter("pager.offset");			//开始条数的索引
		
		PageBean<Product> pageBean = productService.queryProductsByCid(cid, page, pageSize);
		request.setAttribute("pageBean", pageBean); 
		request.setAttribute("cid", cid);      //设置head头部active状态
		request.getRequestDispatcher("/WEB-INF/jsp/product_list.jsp").forward(request, response);
	}
<div class="row" style="width: 1210px; margin: 0 auto;">
		<c:choose>
			<c:when test="${not empty requestScope.pageBean.list}">
				<c:forEach var="product" items="${requestScope.pageBean.list}"> 
					<div class="col-md-2 col-sm-3 col-xs-4">
						<a href="?pid=${product.pid}"> <img src="${pageContext.request.contextPath}/${product.pimage}"
							width="170" height="170" style="display: inline-block;">
						</a>
						<p>
							<a href="?pid=${product.pid}" style='color: green' title="${product.pname}">
								${fn:substring(product.pname, 0, 10)}
							</a>
						</p>
						<p>
							<font color="#FF0000">商城价:&yen;<fmt:formatNumber value="${product.market_price}" minFractionDigits="2"/></font>
						</p>
					</div>
				</c:forEach>
				
				<%-- pager-taglib.jar 
						items - 总条数
						maxIndexPages = "5"   -  最大显示页码数
						maxPageItems="${initParam.pageSize}"		  -  页面显示数
						url - ${pageContext.request.contextPath}/product
								<pg:param name="method" value="queryProsByCid"/>
								<pg:param name="cid" value="${requestScope.cid}"/>
				--%>
				<div style="text-align: center;">
					&nbsp;<br>
					<pg:pager items="${pageBean.total}" maxPageItems="${pageBean.pageSize}"
						export="currentPageNumber=pageNumber" maxIndexPages="5"
						isOffset="true" url="${pageContext.request.contextPath}/product">
						<pg:param name="method" value="queryProsByCid"/>
						<pg:param name="cid" value="${requestScope.cid}"/>
						<c:if test="${currentPageNumber != 1}">
							<pg:first>
								<a href="${pageUrl}">首页</a>
							</pg:first>
						</c:if>
						<pg:prev>
							<a href="${pageUrl}">上一页</a>
						</pg:prev>
						<pg:pages>
							<c:choose>
								<c:when test="${currentPageNumber eq pageNumber}">
									<font color="red">${pageNumber}</font>
								</c:when>
								<c:otherwise>
									<a href="${pageUrl}">${pageNumber}</a>
								</c:otherwise>
							</c:choose>
						</pg:pages>
						<pg:next>
							<a href="${pageUrl }">下一页</a>
						</pg:next>
						<c:if test="${currentPageNumber != pageBean.totalPage}">
							<pg:last>
								<a href="${pageUrl }">尾页</a>
							</pg:last>
						</c:if>
					</pg:pager>
				</div>
					</c:when>
			<c:otherwise>
				<div class="col-md-12" style="text-align: center;">
					正努力上架新商品, 静请期待! 可先购买其他已上架商品!<br><br><br><br>
				</div>
			</c:otherwise>
		</c:choose>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单从表现层来说分页不是一个复杂的工作,稍微理一下思路,处于不同competence level的同学应该都能自己搞出来。 以上面的文章列表分页为例,我觉得分页有两点重要的, 一是:分页我们必须首先自己搞清楚,文章总数、每页显示文章数(页大小)、页数 二是:如何做好页脚的分页导航条 实际应用中,文章总数这个值我们从数据库可以得到;每页显示的文章数即分页的页大小可以自己定义;页数我们可以通过下面的个表达式简单得出。 假设: int pageSize = 10; //分页大小 int totalPosts = PagingDAO.entryList.size(); //总文章数 int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数 每页的文章怎么取出来? 知道分页的大小之后,我们生成了页好的选取下拉框,每次选择第几页的时候,都会向Servlet传递当前选择页号的参数,这样Servlet调用后面的DAO相应的方法,取得文章列表信息,再回传到JSP以供显示。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> page Size : ${pageSize} <br /> Total Posts: ${totalPosts} <br /> Total Pages: ${totalPages} <br /> Current Page: ${pageNumber} <hr /> <table> <thead> <tr align="center"> <td width="10%">Article ID</td> <td width="70%">Article Title</td> <td colspan="3">Actions</td> </tr> </thead> <tbody> <c:forEach items="${entryList}" var="entry"> <tr align="center"> <td>${entry.entryID}</td> <td>${entry.title}</td> <td><a href="viewEntry?entryID=${entry.entryID}">View</a></td> <td><a href="editEntry?entryID=${entry.entryID}">Edit</a></td> <td><a href="deleteEntry?entryID=${entry.entryID}">Delete</a></td> </tr> </c:forEach> </tbody> <tfoot> <tr align="center"> <td colspan="5"> <jsp:include page="paging_footer.jsp"></jsp:include> </td> </tr> </tfoot> </table> <hr/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值