任务
单击导航栏上不同的类型,显示该类型下所有的图书,当单击“全部商品”时,查询的是所有的图书
一、编写页面功能代码
<div id="divmenu">
<a href="${pageContext.request.contextPath}/showProductByPage?category=文学">文学</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=生活">生活</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=计算机">计算机</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=外语">外语</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=经营">经管</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=励志">励志</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=社科">社科</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=学术">学术</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=少儿">少儿</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=艺术">艺术</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=原版">原版</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=科技">科技</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=考试">考试</a>
<a href="${pageContext.request.contextPath}/showProductByPage?category=生活百科">生活百科</a>
<a href="${pageContext.request.contextPath}/showProductByPage" style="color:#b4d76d">全部商品</a>
</div>
二、创建ShowProductByPageServlet
//分页显示数据
public class ShowProductByPageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.定义当前页码,默认为1
int currentPage = 1;
String _currentPage = request.getParameter("currentPage");
if (_currentPage != null) {
currentPage = Integer.parseInt(_currentPage);
}
// 2.定义每页显示条数,默认为4
int currentCount = 4;
String _currentCount = request.getParameter("currentCount");
if (_currentCount != null) {
currentCount = Integer.parseInt(_currentCount);
}
// 3.获取查找的分类
String category = "全部商品";
String _category = request.getParameter("category");
if (_category != null) {
category = _category;
}
// 4.调用service,完成获取当前页分页Bean数据.
ProductService service = new ProductService();
PageBean bean = service.findProductByPage(currentPage, currentCount,
category);
// 将数据存储到request范围,跳转到product_list.jsp页面展示
request.setAttribute("bean", bean);
request.getRequestDispatcher("/client/product_list.jsp").forward(request, response);
return;
}
}
三、ProductService添加findProductByPage
// 分页操作
public PageBean findProductByPage(int currentPage, int currentCount,
String category) {
PageBean bean = new PageBean();
// 封装每页显示数据条数
bean.setCurrentCount(currentCount);
// 封装当前页码
bean.setCurrentPage(currentPage);
// 封装当前查找类别
bean.setCategory(category);
try {
// 获取总条数
int totalCount = dao.findAllCount(category);
bean.setTotalCount(totalCount);
// 获取总页数
int totalPage = (int) Math.ceil(totalCount * 1.0 / currentCount);
bean.setTotalPage(totalPage);
// 获取当前页数据
List<Product> ps = dao.findByPage(currentPage, currentCount,
category);
bean.setPs(ps);
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
四、ProductDao添加findByPage方法
// 获取当前页数据
public List<Product> findByPage(int currentPage, int currentCount,
String category) throws SQLException {
// 要执行的sql语句
String sql = null;
// 参数
Object[] obj = null;
// 如果category不为null,代表是按分类查找
if (!"全部商品".equals(category)) {
sql = "select * from products where category=? limit ?,?";
obj = new Object[] { category, (currentPage - 1) * currentCount,
currentCount, };
} else {
sql = "select * from products limit ?,?";
obj = new Object[] { (currentPage - 1) * currentCount,
currentCount, };
}
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new BeanListHandler<Product>(Product.class),
obj);
}