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">商城价:¥<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;">
<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>