JSP分頁查詢

2021-09-24 12:47:21 字數 1916 閱讀 4488

1)先是在jsp頁面中定義四個按鈕:

首頁

$ / $

尾頁

如圖:

四個按鈕分別觸發不同事件:

//首頁事件

function firstpage()/main";

}function previouspage();

//判斷當前頁減去1後是否小於1,如果是則說明當前頁就是第一頁然後提示使用者並結束方法

if(currentpage < 1));

return;

}//如果當前頁減去1後不小於1則把減去1後的當前頁**到servlet中

window.location.href="$/main?currentpage="+currentpage;

}function nextpage();

//判斷當前頁加1後是否大於總頁數,如果是則說明當前頁就是最後一頁了然後提示使用者並結束方法

if(currentpage > '$'));

return;

}//如果當前頁加1後不大於總頁數則把加1後的當前頁**到servlet中

window.location.href="$/main?currentpage="+currentpage;

}//尾頁

function lastpage()/main?currentpage="+$;

}

然後在servlet中接收這些資料

//開始查詢的位置 = (當前分頁 -1) * 當前查詢的條數

int startindex = (currentpage-1)*pagesize;

//執行查詢操作

iborrowingservice ibs = new borrowingserviceimpl();

listlist = ibs.selectallborrowingandpaging(startindex, pagesize);

//獲取總行數

int count = ibs.selectsumtotal();

//總頁數 = 總行數 除於 當前的查詢條數+1

int totalpage = count/pagesize+1;

//如果總行數 能整除 當前的查詢條數 那麼總頁數就不加1

if(count%pagesize==0)

request.setattribute("currentpage", currentpage);//當前分頁

request.setattribute("count", count);//總行數

request.setattribute("totalpage", totalpage);//總頁數

request.setattribute("list", list);

request.getrequestdispatcher("/jsp/mainjsp.jsp").forward(request, response);

}在servlet中獲取到分頁資料後,接下來就要對資料庫進行操作了

private string selectallborrowingandpagingsql = "select * from borrowing limit ?,?";

使用分頁資料給上面sql語句的佔位符賦值後,就可以執行分頁查詢了,具體步驟就不演示了,最後還需要獲取到資料的總行數。

private string selectsumtotalsql = "select count(*) from borrowing";

jsp 簡單分頁

總記錄數 int count persondao.getcount 每頁顯示5條 int pagesize 5 當前頁 int currentpage 1 int pagecount count pagesize 1 pagesize string currentpgae request.getpa...

jsp頁面分頁

首先要定義四個變數 int pagesize 每頁顯示多少條記錄 int pagenow 希望顯示第幾頁 int pagecount 一共有多少頁 int rowcount 一共有多少條記錄 說明 pagesize是指定的 pagenow是使用者選擇的 rowcount是計算出來的 該計算式為 if...

jsp分頁總結

jsp分頁思路 1.變數的設定 分頁中涉及的變數主要有 總頁數,每頁顯示的記錄數,當前頁數,總記錄數 pagesize 每個頁面所顯示的記錄數 pagecount 一共有多少個頁面 showpage 目前顯示第幾頁 recordcount 總的記錄數 為了方便理解,畫了一張圖 總頁數 總記錄數 每頁...