==SQL语句==
1
| select * from table order by id desc limit (curPage-1)*pageSize,pageSize
|
curPage : 当前页
pageSize : 每页显示的条数
==Dao层设计==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public List<Goods> getGoodsPaging(int curPage,int pageSize){ String sql = "select * from goods order by gid desc limit ?,?"; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List<Goods> gList = new ArrayList<Goods>(); try { conn = db.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, (curPage-1)*pageSize); ps.setInt(2, pageSize); log.info(ps.toString()); rs = ps.executeQuery(); while(rs.next()){ Goods goods = new Goods(); goods.setGid(rs.getInt("gid")); goods.setGname(rs.getString("gname")); goods.setGpicName(rs.getString("gpicName")); goods.setGprice(rs.getFloat("gprice")); gList.add(goods); } } catch (SQLException e) { e.printStackTrace(); } finally { db.release(rs, ps, conn); } return gList; }
|
返回从数据库查到的list集合,并且还要得到数据库总条数,用于求得总共多少页(没copy上来)
==业务层设计==
++得到总页数++
1 2 3 4 5
| public int getTotalPage(){ return this.getTotal() % this.offset ==0 ? this.getTotal() / this.offset:this.getTotal() / this.offset+1; }
|
每一页得到的数据
1 2 3 4
| public List<Goods> getGoodsPaging(int curPage,int limit){ return goodsDao.getGoodsPaging(curPage,limit); }
|
Servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext application = this.getServletContext(); String pageIndex = (String) req.getParameter("curPage"); int curPage = pageIndex == null?1:Integer.valueOf(pageIndex); PageUtils pu = new PageUtils(curPage);
req.setAttribute("curPage", curPage); req.setAttribute("totalPage", pu.getTotalPage()); req.setAttribute("goodsList",pu.getDataByPage());
req.getRequestDispatcher("/WEB-INF/index_show.jsp").forward(req, resp); }
|
==View层设计==
循环展示当前页的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <tbody> <c:if test="${requestScope.curPage != null }"> <c:set var="curPage" value="${requestScope.curPage }"></c:set> <c:set var="totalPage" value="${requestScope.totalPage }"></c:set> </c:if> <c:forEach items="${requestScope.goodsList }" var="g"> <tr> <td><img class=" img-responsive" src="image/${g.gpicName}.jpg" style="height: 40px;width: 70px;" /> </td> <td>${g.gname }</td> <td><font color="red">¥${g.gprice }</font></td> <td><a href="okCart/addCart?gid=${g.gid }">添加到购物车</a></td> </tr> </c:forEach> </tbody>
|
显示分页条
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <c:if test="${requestScope.curPage != null }"> <c:set var="curPage" value="${requestScope.curPage }"></c:set> <c:set var="totalPage" value="${requestScope.totalPage }"></c:set> <nav aria-label="Page navigation"> <ul class="pagination"> <li><a href="/okCart/"> 首页</a></li> <c:forEach begin="1" end="${totalPage }" var="i"> <c:if test="${curPage==i}"> <li class="active"><span>${i }</span></li> </c:if> <c:if test="${curPage!=i}"> <li><a href="/okCart/index?curPage=${i }">${i }</a></li> </c:if> </c:forEach> <li><a href="/okCart/index?curPage=${totalPage }">尾页</a></li> </ul> </nav> </c:if>
|
最终显示效果图

附上PageUtils.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
public class PageUtils { private GoodsService goodsSer; private int curPage; private int offset = 5; public PageUtils(int curPage){ this.goodsSer = new GoodsService(); this.curPage = curPage; }
public int getTotal(){ return goodsSer.getGoodsTotal(); }
public int getTotalPage(){ return this.getTotal() % this.offset ==0 ?this.getTotal() / this.offset:this.getTotal() / this.offset+1; }
public List<Goods> getDataByPage(){ return goodsSer.getGoodsPaging(curPage, offset); } }
|