第一次进去得显示第一页
<a href="/Web13_1/PageProductServlet?currPage=1">分页显示</a>
PageProductServlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
int currPage = Integer.parseInt(request.getParameter("currPage"));
ProductService productService = new ProductService();
PageBean pageBean = productService.pageShow(currPage);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/product/product_page.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
ProductService:
/**
* 分页显示
* @param currPage
* @return
* @throws SQLException
*/
public PageBean pageShow(int currPage) throws SQLException {
/**
* 设置PageBean数据
*/
ProductDao productDao = new ProductDao();
PageBean pageBean = new PageBean();
pageBean.setCurrPage(currPage);
int pageSize = 10;
pageBean.setPageSize(10);
int totalCount = productDao.findTotalCount();
pageBean.setTotalCount(totalCount);
int totalPage = 0;
if(totalCount % pageSize == 0) {
totalPage = totalCount/pageSize;
}
if(totalCount % pageSize != 0) {
totalPage = totalCount/pageSize + 1;
}
pageBean.setTotalPage(totalPage);
int begin = (currPage-1)*pageSize;
List<Product> list = productDao.findPageProduct(begin,pageSize);
pageBean.setList(list);
return pageBean;
}
ProductDao:
/**
* 查询总记录数
* @return
* @throws SQLException
*/
public int findTotalCount() throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
String sql = "select count(*) from product";
Long totalCount = queryRunner.query(sql, new ScalarHandler());
return totalCount.intValue();
}
/**
* 查询每页的数据
* @param begin
* @param pageSize
* @return
* @throws SQLException
*/
public List<Product> findPageProduct(int begin, int pageSize) throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
String sql = "select * from product order by pdate desc limit ?,?";
List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class), begin,pageSize);
return list;
}
Product_page.jsp:
<tr>
<td colspan="8" align="center">
第${ pageBean.currPage }/${ pageBean.totalPage }页
共${ pageBean.totalCount }个商品
每页显示${ pageBean.pageSize }个商品
<c:if test="${ pageBean.currPage != 1 }">
<a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=1">首页</a>
<a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ pageBean.currPage - 1 }">上一页</a>
</c:if>
<c:forEach var="page" begin="1" end="${ pageBean.totalPage }">
<c:if test="${ pageBean.currPage == page }">
${ page }
</c:if>
<c:if test="${ pageBean.currPage != page }">
<a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ page }">${ page }</a>
</c:if>
</c:forEach>
<c:if test="${ pageBean.currPage != pageBean.totalPage }">
<a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ pageBean.currPage + 1 }">下一页</a>
<a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ pageBean.totalPage }">尾页</a>
</c:if>
</td>
</tr>