JSP之分页显示数据

本文将在JavaEE三层框架的基础上,完成JSP分页技术的实现

1.封装分页实体类

将一些散装数据,封装到PageBean中(采用泛型,以适应其他类)

散装数据:当前页数currentPage、当前页面显示条数currentCount、总页数totalPage、总条数totalCount、每页显示的数据products

public class PageBean<T> {
	
	// 当前页数
	private int currentPage;
	
	// 当前页面显示条数
	private int currentCount;
	
	// 总页数
	private int totalPage;
	
	// 总条数
	private int totalCount;
	
	// 每页显示的数据
	private List<T> products = new ArrayList<T>();
	
}

2.web层

向Service层传递分页查询请求,并获取一个PageBean值,存入Request域中

public class ShowProductListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取用户点击的页
		String page = request.getParameter("page");
		if(page == null)
			page = "1";
 		int currentPage = Integer.parseInt(page);
		ProductService service  = new ProductService();
		PageBean<Products> pageBean = null;
		try {
			pageBean = service.findPageBean(currentPage);
		} catch (SQLException e) {
			throw new RuntimeException(e + "商品数据调入失败!");
		}
		
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("product_list.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

3.Service层

处理相相应的PageBean对象的值,向dao层传递请求

public class ProductService {
	private static ProductDao dao = new ProductDao();
	
	//Service层处理PageBean对象
	public PageBean<Products> findPageBean(int currentPage) throws SQLException {
		
		PageBean<Products> pageBean = new PageBean<Products>();
		//1.当前页数		---- 前台用户点击的页数
		pageBean.setCurrentPage(currentPage);
		
		//2.当前页面显示条数	---- 默认设定
		int currentCount = 12;
		pageBean.setCurrentCount(currentCount);
		
		//3.总条数			---- 向dao层传递请求,从数据库中获取
		int totalCount = dao.getTotalCount();
		pageBean.setTotalCount(totalCount);
		
		//4.总页数			---- 向上取整,double(总条数/当前页面显示条数).ceil
		/*
		 * 		总条数   	当前页面显示条数		总页数
		 * 		 10			4			 3
		 *  	 11			4			 3
		 *   	 12			4			 3
		 *   	 13			4			 4		
		 */
		int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
		pageBean.setTotalPage(totalPage);
		
		//5.每页显示的数据		---- 起始索引==(当前页面-1)* 每页显示条数;
	
		/*
		 * 	      当前页数   		起始索引		 每页显示条数
		 * 		1		  0				4           --- 0,1,2,3
		 * 		2		  4				4			--- 4,5,6,7
		 * 		3		  8				4			--- 8,9,10,11
		 * 		4		  12 			4			--- 12,13,14,15
		 */
		int startIndex = (currentPage-1)*currentCount;
		List<Products> products = dao.findProductPageBean(startIndex,currentCount);
		pageBean.setProducts(products);
		return pageBean;
	}

}

4.dao层

从数据中获取商品数据

public class ProductDao {
	private static QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
	


	//1.获取全部商品总条数
	public int getTotalCount() throws SQLException {
		String sql = "select count(*) from product";
		Long query = (Long)runner.query(sql, new ScalarHandler());
		int totalCount = query.intValue();
		return totalCount;
	}


	//2.获得分页的商品信息
	public List<Products> findProductPageBean(int startIndex, int currentCount) throws SQLException {
		//第一个?,起始索引  第二个?,每页显示条数
		String sql = "select * from product limit ?,?";
		List<Products> products = runner.query(sql, new BeanListHandler<Products>(Products.class),startIndex, currentCount);
		return products;
	}

}

5.前端页面

        <!--分页 -->
	<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
		<ul class="pagination" style="text-align: center; margin-top: 10px;">
			
			 			<!-- 当前页面为第一页时,不允许向前点击 -->
			<c:if test="${pageBean.currentPage==1 }">
				<li class="disabled">
					<a href="javascript:void(0)" aria-label="Previous">
						 <span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			</c:if>
						<!-- 当前页面为第一页时,向前点击时,当前页面连接减1 -->
			<c:if test="${pageBean.currentPage!=1 }">
				<li>
					<a href="${pageContext.request.contextPath }/showProductList?page=${pageBean.currentPage-1}" aria-label="Previous">
						 <span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			</c:if>

			<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
					<!-- 判断当前页 -->
				<c:if test="${pageBean.currentPage==page }">
					<!-- 选择当前页时,不允许再次点击按钮 -->
				<li class="active"><a href="javascript:void(0)">${page }</a></li>
				</c:if>
				<c:if test="${pageBean.currentPage!=page }">
				<li><a href="${pageContext.request.contextPath }/showProductList?page=${page}">${page }</a></li>
				</c:if>
			</c:forEach>
			
						 <!-- 当前页面为最后一页时,不允许向后点击 -->
			<c:if test="${pageBean.totalPage==pageBean.currentPage }">
				<li class="disabled">
					<a href="javascript:void(0)" aria-label="Next"> <span aria-hidden="true">&raquo;</span></a>
				</li>
			</c:if>
			<c:if test="${pageBean.totalPage!=pageBean.currentPage }">
				<li>
					<a href="${pageContext.request.contextPath }/showProductList?page=${pageBean.currentPage+1}" aria-label="Next"> <span aria-hidden="true">&raquo;</span></a>
				</li>
			</c:if>
		</ul>
	</div>
	<!-- 分页结束 -->

猜你喜欢

转载自blog.csdn.net/mmake1994/article/details/81120417