旅游线路分页展示
类别id传递
header.html:中拿到cid
//遍历数组 for (var i = 0; i <data.length; i++) { var li = '<li><a href="route_list.html?cid='+data[i].cid+'">'+data[i].cname+'</a></li>'; lis+=li; }
CategoryServiceImpl:进行修改缓存中的cid
route_list.html:即可获取到cid
<script> $(function () { var search = location.search; //切割字符串,拿到第二个值 var cid = search.split("=")[1]; alert(cid); }); </script>
根据id查询不同类别的旅游线路数据
分析:
route_list.html:
加上:
在domain包中创建:PageBean对象
public class PageBean<T> { private int totalCount;//总记录数 private int totalPage;//总页数 private int currentPage;//当前页码 private int pageSize;//每页显示条数 private List<T> list;//每页显示的数据集合 //get,set }
代码实现:
RouteDao:编写两个方法
public interface RouteDao { /** * 根据id查询总记录数 * @param cid * @return */ public int findTotalCount(int cid); /** * 根据cid,start,pageSize查询当前页的数据集合 * @param cid * @param start * @param pageSize * @return */ public List<Route> findByPage(int cid,int start,int pageSize); }
RouteDaoImpl:实现
public class RouteDaoImpl implements RouteDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public int findTotalCount(int cid) { String sql = "select count(*) from tab_route where cid = ?"; return template.queryForObject(sql,Integer.class,cid); } @Override public List<Route> findByPage(int cid, int start, int pageSize) { String sql = "select * from tab_route where cid = ? limit ? , ?"; return template.query(sql,new BeanPropertyRowMapper<Route>(Route.class),cid,start,pageSize); } }
RouteService:
/** * 线路Service */ public interface RouteService { /** * 根据类别进行分页查询 * @param cid * @param currentPage * @param pageSize * @return */ public PageBean<Route> pageQuery(int cid,int currentPage,int pageSize); }
RouteServiceImpl:
public class RouteServiceImpl implements RouteService { private RouteDao routeDao = new RouteDaoImpl(); @Override public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize) { //封装PageBean PageBean<Route> pb = new PageBean<Route>(); //设置当前页码 pb.setCurrentPage(currentPage); //设置每页显示条数 pb.setPageSize(pageSize); //设置总记录数 int totalCount = routeDao.findTotalCount(cid); pb.setTotalCount(totalCount); //设置当前页显示的数据集合 int start = (currentPage - 1) * pageSize;//开始的记录数 List<Route> list = routeDao.findByPage(cid,start,pageSize); pb.setList(list); //设置总页数 = 总记录数/每页显示条数 int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize)+1; pb.setTotalPage(totalPage); return pb; } }
RouteServlet:
@WebServlet("/route/*") public class RouteServlet extends BaseServlet { private RouteService routeService = new RouteServiceImpl(); public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收参数 String currentPageStr = request.getParameter("currentPage"); String pageSizeStr = request.getParameter("pageSize"); String cidStr = request.getParameter("cid"); //处理参数 int cid = 0;//类别id if (cidStr!=null && cidStr.length()>0){ cid = Integer.parseInt(cidStr); } int currentPage = 0;//当前页码,如果不传递,则默认第一页 if (currentPageStr!=null && currentPageStr.length()>0){ currentPage = Integer.parseInt(currentPageStr); }else { currentPage = 1; } int pageSize = 0;//每页显示条数,如果不传递,默认每页显示5条记录 if (pageSizeStr!=null && pageSizeStr.length()>0){ pageSize = Integer.parseInt(pageSizeStr); }else { pageSize = 5; } //调用Service查询PageBean对象 PageBean<Route> pb = routeService.pageQuery(cid, currentPage, pageSize); //将PageBean对象序列化为json,返回 writeValue(pb,response); } }
访问一下:出现数据代表正确
搜索
复制
标签:pageSize,cid,int,PageBean,public,旅游网,后台,currentPage,黑马 From: https://www.cnblogs.com/pengtianyang/p/16623661.html