标签:att get 步骤 count 遍历数组 amp void runner new
按类别 分页展示 步骤分析: 在菜单上 点击一个分类 head.jsp <a href="/store/product?method=findByPage&cid=${}&currPage=1"></a> findByPage操作: 1.接受 cid currPage 设定一个每页显示的条数 pageSize 2.调用productSerivce 返回一个PageBean pageBean中 list currPage pageSize totalPage totalCount 3.将pagebean放入request域中,请求转发 在productSerivce需要封装成pagebean 在product_list.jsp展示数据
package com.louis.domain; import java.util.List; public class PageBean<E> { private List<E> list; private Integer currPage; private Integer pageSize; private Integer totalPage; private Integer totalCount; public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public Integer getCurrPage() { return currPage; } public void setCurrPage(Integer currPage) { this.currPage = currPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalPage() { return (int)Math.ceil(totalCount*1.0/pageSize); } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } public PageBean() { } public PageBean(List<E> list, Integer currPage, Integer pageSize, Integer totalCount) { super(); this.list = list; this.currPage = currPage; this.pageSize = pageSize; this.totalCount = totalCount; } }
<script> $(function() { //发送ajax请求 $.get("${pageContext.request.contextPath}/category?method=findAll",function(data){ //获取元素 var $ul = $("#menuId"); //遍历数组 $(data).each(function(){ $ul.append($("<li><a href=‘${pageContext.request.contextPath}/product?method=findByPage&cid="+this.cid+"&currPage=1‘>"+this.cname+"</a></li>")); }); },"json"); }); </script>
/* * 分页查询数据 * */ public String findByPage(HttpServletRequest request, HttpServletResponse response) throws Exception { //1、获取类别,当前页,设置pageSize String cid = request.getParameter("cid"); int currPage = Integer.parseInt(request.getParameter("currPage")); int pageSize = 12; //2、调用service返回值pageBean ProductService productService = new ProductServiceImpl(); PageBean<Product> pageBean = productService.getByPage(currPage,pageSize,cid); //3、将结果放入request中请求转发 request.setAttribute("pb", pageBean); return "/jsp/product_info.jsp"; }
//按类别查询商品 @Override public PageBean<Product> getByPage(int currPage, int pageSize, String cid) throws Exception { ProductDao productDao = new ProductDaoImpl(); //当前页数据 List<Product> list = productDao.findByPage(currPage,pageSize,cid); //总条数 int totalCoun = productDao.getTotalCount(cid); return new PageBean<>(list,currPage,pageSize,totalCoun); }
/** * 查询当前也需要展示的数据 */ @Override public List<Product> findByPage(int currPage, int pageSize, String cid) throws Exception { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from product where cid = ? limit ?,?"; return qr.query(sql, new BeanListHandler<>(Product.class), cid,(currPage-1)*pageSize,pageSize); } /** * 查询当前类别的总条数 */ @Override public int getTotalCount(String cid) throws Exception { QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); String sql="select count(*) from product where cid = ?"; return ((Long)qr.query(sql, new ScalarHandler(), cid)).intValue(); }
return ((Long)qr.query(sql, new ScalarHandler(), cid)).intValue();
标签:att get 步骤 count 遍历数组 amp void runner new
原文地址:http://www.cnblogs.com/Michael2397/p/7654487.html