10.按類別查詢分頁
阿新 • • 發佈:2018-02-20
exce ktr ret har public patch jdbcutil 訪問 pub
提交方式:
<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; }
10.按類別查詢分頁