java web手寫實現分頁功能
現在很多流行的框架,都可以很快的把分頁效果做出來,但是作為一名程式設計師你必須得知道手寫分頁的流程:
場景效果:
一、分頁的思路
首先我們得知道寫分頁程式碼的思路,保持思路清晰,才能行雲流水的去寫程式碼,其實不管是在寫什麼程式碼,思路,思想特別重要,先想好再動手,就會事半功倍!
先來分析SQL語句實現
Select * from product limit 0,5 Select * from product limit 5,5 Select * from product limit 10,5 Select * from product limit 15,5
#當前頁 起始值 每頁資料大小
1 0 5
3 10 5
4 15 5
結論:
(1)(當前頁-1)*(每頁數量)=起始值
(2)要想實現分頁,向服務端發起請求的時候,必須傳遞當前頁。
二、建立PageBean存放資料
這時候我們需要封裝一個包裝類,來封裝我們的分頁資料
package cn.itcast.store.domain; import java.util.List; /** * 存放分頁相關的資料 * * @author yechengchao */ public class PageModel { //基本屬性 /**當前頁數,由使用者指定 */ private int currentPageNum; /**每頁顯示的條數,可以由使用者指定每頁顯示多少 */ private int pageSize =5; /**總記錄條數,資料庫查出來的 */ private int totalRecords; /**總頁數,計算出來的 */ private int totalPageNum; /**每頁開始記錄的索引,計算出來的 (當前頁-1)*(每頁數量)=起始值 */ private int startIndex; /**上一頁 */ private int prePageNum; /**下一頁 */ private int nextPageNum; /**已經分好頁的結果集,存放我們查出來的結果集*/ private List list; /**擴充套件屬性 一共每頁顯示9個頁碼按鈕*/ /**開始頁碼*/ private int startPage; /**結束頁碼*/ private int endPage; /**完善屬性*/ private String url; /**要想使用我的分頁,必須給我兩個引數。一個是要看哪一頁,另一個是總記錄條數*/ public PageModel(int currentPageNum,int totalRecords,int pageSize){ this.currentPageNum = currentPageNum; this.totalRecords = totalRecords; this.pageSize=pageSize; //計算查詢記錄的開始索引 startIndex = (currentPageNum-1)*pageSize; //計算總頁數 totalPageNum = totalRecords%pageSize==0?(totalRecords/pageSize):(totalRecords/pageSize+1); //5 startPage = currentPageNum - 4; //結束頁碼 endPage = currentPageNum + 4; //看看總頁數夠不夠9頁 if(totalPageNum>9){ //超過了9頁 if(startPage < 1){ startPage = 1; endPage = startPage+8; } if(endPage>totalPageNum){ endPage = totalPageNum; startPage = endPage-8; } }else{ //不夠9頁 startPage = 1; endPage = totalPageNum; } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getStartPage() { return startPage; } public void setStartPage(int startPage) { this.startPage = startPage; } public int getEndPage() { return endPage; } public void setEndPage(int endPage) { this.endPage = endPage; } public int getPrePageNum() { prePageNum = currentPageNum-1; if(prePageNum<1){ prePageNum = 1; } return prePageNum; } public int getNextPageNum() { nextPageNum = currentPageNum+1; if(nextPageNum>totalPageNum){ nextPageNum = totalPageNum; } return nextPageNum; } public int getCurrentPageNum() { return currentPageNum; } public void setCurrentPageNum(int currentPageNum) { this.currentPageNum = currentPageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getTotalPageNum() { return totalPageNum; } public void setTotalPageNum(int totalPageNum) { this.totalPageNum = totalPageNum; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public void setPrePageNum(int prePageNum) { this.prePageNum = prePageNum; } public void setNextPageNum(int nextPageNum) { this.nextPageNum = nextPageNum; } public List getList() { return list; } public void setList(List list) { this.list = list; } }
三、在servlet編寫控制程式碼
首先使用者傳送請求,帶上當前頁數,在這表呼叫業務層的程式碼,把以分頁的形式查詢商品,再把商品查詢出來之後賦值給我們建立的pageModel物件,這時候把這個物件傳到前端頁面,就可以把值取出來,實現分頁。
public String findProductByCidWithPage(HttpServletRequest request,HttpServletResponse response) throws Exception { //獲取cid,num String cid=request.getParameter("cid"); int curNum=Integer.parseInt(request.getParameter("num")); //呼叫業務層的功能:以分頁的形式查詢當前頁類別下商品資訊 //返回PageModel物件(1當前頁商品資訊2分頁3 url) ProductService productService=new ProductServiceImp(); PageModel pm=productService.findProductByCidWithPage(cid,curNum); //將PageModel物件放入request request.setAttribute("page",pm); //轉發到/jsp/product_list.jsp return "/jsp/product_list.jsp"; }
四、業務層service編寫業務邏輯程式碼
當呼叫業務層的業務邏輯的時候,在這邊我們是通過Dao層把我們要查詢的商品查詢出來用一個list接收,再傳給pageModel的屬性list,這時候就把整個pageModel物件傳回去,這邊主要是呼叫Dao層查詢和 關聯集合,關聯URL。
public PageModel findProductByCidWithPage(String cid,int curNum) throws Exception { //1 建立pageModel物件 目的:計算分頁引數 //統計當前分類下商品的個數 select count(*) from product where cid=? int totalRecords=productDao.findtotalRecords(cid); PageModel pageModel=new PageModel(curNum,totalRecords,12); //2.關聯集合 select * form product where cid=? limit ?,? List list=productDao.findProductByCidWithPage(cid,pageModel.getStartIndex(),pageModel.getPageSize()); pageModel.setList(list); //3.關聯url pageModel.setUrl("ProductServlet?method=findProductByCidWithPage&cid="+cid); return pageModel; }
五、Dao層操作資料庫
為什麼我們要在最開始分析sql語句,最根源就是在這邊查詢資料庫,我們需要把起始頁和分頁大小傳進去。
public List findProductByCidWithPage(String cid,int startIndex,int pageSize) throws Exception { String sql="select * from product where cid=? limit ?,?"; QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource()); return qr.query(sql,new BeanListHandler<Product>(Product.class),cid,startIndex,pageSize); }
六、前端頁面,顯示分頁
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%--分頁顯示的開始 --%> <div style="text-align:center"> 共${page.totalPageNum}頁/第${page.currentPageNum}頁 <a href="${pageContext.request.contextPath}/${page.url}&num=1" rel="external nofollow" >首頁</a> <a href="${pageContext.request.contextPath}/${page.url}&num=${page.prePageNum}" rel="external nofollow" >上一頁</a> <%--顯示的頁碼,使用forEach遍歷顯示的頁面 --%> <c:forEach begin="${page.startPage}" end="${page.endPage}" var="pagenum"> <a href="${pageContext.request.contextPath}/${page.url}&num=${pagenum}" rel="external nofollow" >${pagenum}</a> </c:forEach> <a href="${pageContext.request.contextPath}/${page.url}&num=${page.nextPageNum}" rel="external nofollow" >下一頁</a> <a href="${pageContext.request.contextPath}/${page.url}&num=${page.totalPageNum}" rel="external nofollow" >末頁</a> <input type="text" id="pagenum" name="pagenum" size="1"/><input type="button" value="前往" onclick="jump()" /> <script type="text/javascript"> function jump(){ var totalpage = ${page.totalPageNum}; var pagenum = document.getElementById("pagenum").value; //判斷輸入的是一個數字 var reg =/^[1-9][0-9]{0,1}$/; if(!reg.test(pagenum)){ //不是一個有效數字 alert("請輸入符合規定的數字"); return ; } //判斷輸入的數字不能大於總頁數 if(parseInt(pagenum)>parseInt(totalpage)){ //超過了總頁數 alert("不能大於總頁數"); return; } //轉向分頁顯示的Servlet window.location.href="${pageContext.request.contextPath}/${page.url}&num=" rel="external nofollow" +pagenum; } </script> </div> <%--分頁顯示的結束--%>
因為將我們所有需要的資料都封裝在了pageModel中,pageModel物件又在request域中,所以在jsp頁面中,我們只需要拿到我們所需要的資料,進行顯示即可,構造導航圖需要注意的有一點,邏輯要搞清楚,想要顯示什麼不想顯示什麼,全屏自己控制了,只需要記得一點,在請求Servlet時,需要把請求的頁碼交給伺服器。不然伺服器不知道你要獲得第幾頁的資料。
總結
其實分頁也不太難,一個難點就是javaBean的構建,只要弄清楚pageModel裡面需要哪些屬性,各種屬性的作用是什麼,就會很清晰了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。