提交方式:
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=文学">文学</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=生活">生活</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=计算机">计算机</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=外语">外语</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=经营">经管</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=励志">励志</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=社科">社科</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=学术">学术</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=少儿">少儿</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=艺术">艺术</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=原版">原版</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=科技">科技</a>
<a
href="${pageContext.request.contextPath}/product?method=findByPage2&category=考试">考试</a>
三层架构:
ProductServlet
/**
* 根据类别查询并分页
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findByPage2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取分类
String category=request.getParameter("category");
System.out.println(category);
//一般情况下,服务器默认的编码是“iso8859-1”,所以我们需要数据还原,然后再转换成UTF-8的形式
//如果要调用request.setCharacterEncoding进行编码设置,一定要在任何参数被访问之前调用。
//if(category!=null){
// category=new String(category.getBytes("iso8859-1"),"utf-8");
//}
//获取页码
int pageCode=getPageCode(request);
//每页显示的记录条数
int pageSize=4;
//调用业务层
ProductService ps=new ProductService();
//分类分页的查询
PageBean page=ps.findByPage(pageCode,pageSize,category);
request.setAttribute("page", page);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
ProductService
/**
* 根据类别查询并分页
* @param pageCode
* @param pageSize
* @param category
* @return
*/
public PageBean findByPage(int pageCode, int pageSize, String category) {
ProductDao dao=new ProductDao();
return dao.findByPage(pageCode,pageSize,category);
}
ProductDao
/**
* 根据类别查询并分页
* @param pageCode
* @param pageSize
* @param category
* @return
*/
public PageBean findByPage(int pageCode, int pageSize, String category) {
//存储cansh
List<Object> list=new ArrayList();
PageBean<Product> page=new PageBean<Product>();
page.setPageCode(pageCode);
page.setPageSize(pageSize);
QueryRunner runner=new QueryRunner(MyJdbcUtils.getDataSource());
String countSql=null;
String selSql=null;
if(category!=null){
countSql="select count(*) from products where category=?";
selSql="select * from products where category=? limit ?,?";
list.add(category);
}else{
countSql="select count(*) from products ";
selSql="select * from products limit ?,?";
}
try {
//查询总记录的条数
long totalCount=(long) runner.query(countSql, new ScalarHandler(),list.toArray());
//设置条数
page.setTotalCount((int)totalCount);
list.add((pageCode-1)*pageSize);
list.add(pageSize);
//查询每页显示的条数
List<Product> beanList=runner.query(selSql, new BeanListHandler<Product>(Product.class),list.toArray());
page.setBeanList(beanList);
} catch (SQLException e) {
e.printStackTrace();
}
return page;
}