1. 程式人生 > >JSP servlet分頁

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>