1.效果图
2.核心代码(12个一页)
<!--搜索--><formid="searchForm"method="post"action="/product/list.do"><divclass="search"><inputtype="hidden"name="page"value="1"><inputtype="text"name="title"class="search_input"value="${title}"><inputtype="submit"class="search_submit"value=""></div></form><!--分页--><div><%--<ahref="/product/list.do?page=1">首页</a>--%><%--<ahref="/product/list.do?page=${prePage}">上一页</a>--%><%--<ahref="/product/list.do?page=${nextPage}">下一页</a>--%><%--<ahref="/product/list.do?page=${totalPage}">尾页</a>--%><formmethod="post"action="/product/list.do"style="display: inline"><inputtype="hidden"name="page"value="1"><inputtype="hidden"name="title"value="${title}"><inputtype="submit"value="首页"class="btn"></form><formmethod="post"action="/product/list.do"style="display: inline"><inputtype="hidden"name="page"value="${prePage}"><inputtype="hidden"name="title"value="${title}"><inputtype="submit"value="上一页"class="btn"></form><formmethod="post"action="/product/list.do"style="display: inline"><inputtype="hidden"name="page"value="${nextPage}"><inputtype="hidden"name="title"value="${title}"><inputtype="submit"value="下一页"class="btn"></form><formmethod="post"action="/product/list.do"style="display: inline"><inputtype="hidden"name="page"value="${totalPage}"><inputtype="hidden"name="title"value="${title}"><inputtype="submit"value="尾页"class="btn"></form>第${curPage}页/共${totalPage}页</div><!--响应内容省略-->
packagecom.imooc.cart.servlet; importcom.imooc.cart.data.LocalCache; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjava.io.IOException; /*** 商品(课程)控制器* @version 1.0*/publicclassProductServletextendsHttpServlet { @Overrideprotectedvoidservice(HttpServletRequestreq, HttpServletResponseresp) throwsServletException, IOException { Stringname=req.getParameter("title"); StringpageStr=req.getParameter("page"); intpage=1; if (null!=pageStr&&!"".equals(pageStr)) { page=Integer.parseInt(pageStr); } inttotalProducts=LocalCache.getProductsCount(name); inttotalPage=totalProducts%12>0?totalProducts/12+1 : totalProducts/12; req.setAttribute("curPage", page); req.setAttribute("prePage", page>1?page-1 : 1); req.setAttribute("nextPage", totalPage>page?page+1 : totalPage); req.setAttribute("totalPage", totalPage); req.setAttribute("title", name); req.setAttribute("products", LocalCache.getProducts(page, 12, name)); req.getRequestDispatcher("/WEB-INF/views/biz/list.jsp").forward(req, resp); } }
packagecom.imooc.cart.data; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; /*** 本地缓存* @version 1.0*/publicfinalclassLocalCache { privatestaticMap<Long, Product>productMap=newHashMap<>(); // 省略...publicstaticList<Product>getProducts(intpage, intsize, Stringname) { List<Product>products=newArrayList<>(); if (null!=name&&!"".equals(name)) { productMap.values().forEach(product-> { if (product.getName().contains(name)) { products.add(product); } }); } else { products.addAll(productMap.values()); } intstart= (page-1) *size; intend=products.size() >=page*size?page*size : products.size(); returnproducts.subList(start, end); } publicstaticintgetProductsCount(Stringname) { List<Product>products=newArrayList<>(); if (null!=name&&!"".equals(name)) { productMap.values().forEach(product-> { if (product.getName().contains(name)) { products.add(product); } }); } else { products.addAll(productMap.values()); } returnproducts.size(); } }
3.待更新...