按型別分頁功能的實現
實現思想
分頁功能 因為是分類查詢所以和一般的分頁功能的sql語句不一樣,需要在查詢的時候新增一個判斷條件where cid=? 因為需要操作的資料多,型別繁雜,所以應該建立一個物件將其封裝起來,並在物件內對其屬性進行操作,這樣儘量做到在後端計算資料,在前端取資料。 一般分頁會有:首頁,上一頁,具體那一頁,下一頁,尾頁,等功能。 當點選首頁時需要傳入,page=1,cid=1 當點選尾頁時需要傳入,page=totalpage,cid=1 當點選上一頁時需要傳入,page=page-1(需要判斷,有沒有上一頁),cid=1 當點選下一頁時需要傳入,page=page+1(需要判斷,有沒有下一頁),cid=1 當點選具體分頁的時候需要傳入,page=當前頁的值,cid=1;(這個需要用到for迴圈後端傳來的totalpage,然後取到值就是前頁的值) 所以經過分析pagemodel需要的引數有:indexpage每頁開始的索引,pagesize每頁顯示的商品數量固定的,cid分類資訊,preProductList每頁查詢到的集合,sumpage共有多少頁,uppage上一頁,headerpage首頁,nextpage下一頁,endpage尾頁。nowpage當前頁數。
nowpage,當前頁數是前端傳過來的資料 indexpage,每頁開始的索引是(當前頁數-1)X每頁的商品數 pagesize,每頁的商品數是個固定值,這次是12 cid,商品分類資訊,是前端傳過來的資料, preProductList,每頁顯示的商品,是一個list集合是通過sql語句查出來的 totalpage,共有多少頁,是通過sql語句查出來的 uppage,上一頁,是用當前頁-1(需要判斷是否有上一頁) headerpage,首頁就是page=1 nextpage,下一頁,是用當前頁+1(需要判斷是否有下一頁) endpage,尾頁就是page=toalpage
pojo程式碼
import java.util.List; public class PageModel { private int nowpage; //當前頁 private int pagesize=12; //每頁總數 private int totalpage; //共多少頁 private int uppage; //上一頁 private int nextpage; //下一頁 private String cid; //分類id private List<Product> preProductList; //分類產品總數 private int beganindex; //每頁起始頁數 public String toString() { return "PageModel [nowpage=" + nowpage + ", pagesize=" + pagesize + ", totalpage=" + totalpage + ", uppage=" + uppage + ", nextpage=" + nextpage + ", cid=" + cid + ", preProductList=" + preProductList + ", beganindex=" + beganindex + "]"; } public PageModel() { super(); // TODO Auto-generated constructor stub } public PageModel(int nowpage, int sumlist, String cid, List<Product> preProductList) { super(); this.nowpage = nowpage; this.cid = cid; this.preProductList = preProductList; this.totalpage = (sumlist%pagesize==0)?(sumlist/pagesize):(sumlist/pagesize+1); this.uppage = (this.nowpage==1)?(1):(this.nowpage-1); this.nextpage = (this.nowpage==totalpage)?(totalpage):(this.nowpage+1); this.preProductList = preProductList; this.beganindex = (this.nowpage-1)*pagesize; } public int getNowpage() { return nowpage; } public void setNowpage(int nowpage) { this.nowpage = nowpage; } public int getPagesize() { return pagesize; } public int getTotalpage() { return totalpage; } public void setTotalpage(int sumlist) { this.totalpage = (sumlist%pagesize==0)?(sumlist/pagesize):(sumlist/pagesize+1); } public int getUppage() { return uppage; } public void setUppage() { this.uppage = (nowpage==1)?(1):(nowpage-1); } public int getNextpage() { return nextpage; } public void setNextpage() { this.nextpage = (nowpage==totalpage)?(totalpage):(nowpage+1); } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public List<Product> getPreProductList() { return preProductList; } public void setPreProductList(List<Product> preProductList) { this.preProductList = preProductList; } public int getBeganindex() { return beganindex; } public void setBeganindex() { this.beganindex = (nowpage-1)*pagesize; } }
dao程式碼
QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource()); List<Product> list = qr.query("select * from product where cid=? limit ? ,12", new BeanListHandler<Product>(Product.class),cid,Integer.parseInt(page)*12); List<Product> totalproduct = qr.query("select * from product where cid=?", new BeanListHandler<Product>(Product.class) ,cid); PageModel pm=new PageModel(Integer.parseInt(page),totalproduct.size(),cid,list); return pm; }``` ## service程式碼 ``` public PageModel findBypage(String page,String cid) throws Exception { PageModel pm=new PageModel(); pm = pdao.findByPage(page,cid); return pm; }``` ## servlet程式碼 ``` public String findByPage(HttpServletRequest request, HttpServletResponse response) throws Exception { String page =request.getParameter("page"); String cid=request.getParameter("cid"); ProductService ps=new ProductServiceImp(); PageModel pm=new PageModel(); pm = ps.findBypage(page,cid); request.setAttribute("pageModel",pm); System.out.println("接收到的兩個引數是page==="+page+"cid======"+cid); System.out.println(pm); return "jsp/product_list.jsp"; }``` ## jsp程式碼 ``` <!--分頁 --> <c:if test="${empty pageModel.preProductList }"><h2>SORRY NOT PRODUCT</h2></c:if> <c:if test="${not empty pageModel.preProductList }"> <div style="width:380px;margin:0 auto;margin-top:50px;"> <ul class="pagination" style="text-align:center; margin-top:10px;"> <li class="disabled"><a href="ProductServlet?metod=findByPage&page=1&cid=${pageModel.cid }" aria-label="Previous"><span aria-hidden="true">«</span></a></li> <c:forEach begin="1" end="${pageModel.totalpage }" var="i"> <li class="active"><a href="ProductServlet?metod=findByPage&page=${i }&cid=${pageModel.cid }">${i }</a></li> </c:forEach> <li class="disabled"><a href="ProductServlet?metod=findByPage&page=${pageModel.totalpage }&cid=${pageModel.cid }" aria-label="Next"><span aria-hidden="true">»</span></a></li> </ul> </div> </c:if>```