/**
創(chuàng)新互聯(lián)從2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元金牛做網(wǎng)站,已為上家服務(wù),為金牛各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792
*?分頁(yè)代碼
*?
*?@author?Star
*?配頃蔽@version?1.0?2008/07/08
*/
public?class?CutPage?培州implements?Serializable{
private?static?Log?log?=?LogFactory.getLog(CutPage.class);
private?int?curPageNo?=?0;?//?當(dāng)前頁(yè)數(shù),從0開(kāi)始
private?int?size?=?0;?//?所有數(shù)據(jù)條數(shù)
private?String?url;?//?頁(yè)面跳轉(zhuǎn)的路徑
private?List?showList;?//?當(dāng)前頁(yè)面需要顯示的數(shù)據(jù)列表
private?int?pageSize?=?20;//?每頁(yè)顯示的數(shù)據(jù)條數(shù)
private?int?groupSize?=?1;//?多少頁(yè)為一組
private?String?pageNavigation;//?導(dǎo)航條
/**
?*?每次通過(guò)sql語(yǔ)句從數(shù)據(jù)庫(kù)里面分組取出需要顯示的數(shù)據(jù)
?*?
?*?@param?request
?*????????????javax.servlet.http.HttpServletRequest對(duì)象
?*?@param?sql
?*????????????String?查詢(xún)數(shù)據(jù)庫(kù)的sql語(yǔ)句
?*?@param?pageSize
?*????????????int?每頁(yè)顯示的條數(shù)
?*?@param?groupSize
?*????????????int?分成多少組
?*?@param?url
?*????????????String?頁(yè)面跳轉(zhuǎn)的路徑乎敗,若沒(méi)有特殊的參數(shù)傳遞,可以傳入null或"",
?*????????????如是在aciton里面調(diào)用,并且action是繼承自DispatherAction的話(huà)最好傳入完整的路徑
?*/
public?void?init(HttpServletRequest?request,?String?sql,?int?pageSize,
int?groupSize,?int?pageNo,?String?url)?{
//?上一頁(yè)、下一頁(yè)跳轉(zhuǎn)路徑
if?(url?!=?null)?{
this.url?=?url;
}?else?{
this.url?=?request.getRequestURL()?+?"";
}
if?(pageSize??0)
this.pageSize?=?pageSize;//?每頁(yè)多少條記錄
if?(groupSize??0)
this.groupSize?=?groupSize;
//?當(dāng)前第幾頁(yè)
if?(pageNo??0)?{
this.curPageNo?=?0;
}?else?{
this.curPageNo?=?pageNo;
}
int?curGroup?=?this.curPageNo?/?this.groupSize?+?1;
//?是否是新的一組數(shù)據(jù),如果是則到數(shù)據(jù)庫(kù)取數(shù)據(jù)
this.size?=?parseInt(request.getSession().getAttribute("page_all_size")
+?"",?0);
if?(this.curPageNo?%?this.groupSize?==?0
||?(request.getSession().getAttribute("cur_group")?!=?null??parseInt(
""?+?request.getSession().getAttribute("cur_group"),?1)?!=?curGroup)
||?this.size?==?0?||?request.getParameter("reload")?!=?null)?{
request.getSession().setAttribute("cur_group",?curGroup);
if?(pageNo??0
?request.getSession().getAttribute("page_sql")?!=?null)?{
sql?=?request.getSession().getAttribute("page_sql")?+?"";
}?else?{
request.getSession().setAttribute("page_sql",?sql);
}
this.size?=?getTotalCount(sql);
List?list?=?getPageData(sql,?(this.curPageNo?/?this.groupSize)
*?this.pageSize?*?this.groupSize,?this.pageSize
*?this.groupSize);
request.getSession().setAttribute("page_all_size",?this.size);
request.getSession().setAttribute("page_cur_list",?list);
this.setShowList(list);//?設(shè)置頁(yè)面上的顯示數(shù)據(jù)
}?else?{
this.setShowList((List)?request.getSession().getAttribute(
"page_cur_list"));//?設(shè)置頁(yè)面上的顯示數(shù)據(jù)
}
}
/**
?*?每次通過(guò)sql語(yǔ)句從數(shù)據(jù)庫(kù)里面分組取出需要顯示的數(shù)據(jù)
?*?
?*?@param?request
?*????????????javax.servlet.http.HttpServletRequest對(duì)象
?*?@param?sql
?*????????????String?查詢(xún)數(shù)據(jù)庫(kù)的sql語(yǔ)句
?*?@param?pageSize
?*????????????int?每頁(yè)顯示的條數(shù)
?*?@param?groupSize
?*????????????int?分成多少組
?*?@param?url
?*????????????String?頁(yè)面跳轉(zhuǎn)的路徑,若沒(méi)有特殊的參數(shù)傳遞,可以傳入null或"",
?*????????????如是在aciton里面調(diào)用,并且action是繼承自DispatherAction的話(huà)最好傳入完整的路徑
?*/
public?void?init(HttpServletRequest?request,?String?sql,?int?pageSize,
int?groupSize,?String?url)?{
//?當(dāng)前第幾頁(yè)
String?curPage?=?request.getParameter("pageNo");
init(request,?sql,?pageSize,?groupSize,?parseInt(curPage,?-1),?url);
}
/**
?*?每次通過(guò)sql語(yǔ)句從數(shù)據(jù)庫(kù)里面分組取出需要顯示的數(shù)據(jù)
?*?
?*?@param?request
?*????????????javax.servlet.http.HttpServletRequest對(duì)象
?*?@param?sql
?*????????????String?查詢(xún)數(shù)據(jù)庫(kù)的sql語(yǔ)句
?*?@param?pageSize
?*????????????int?每頁(yè)顯示的條數(shù)
?*?@param?groupSize
?*????????????int?分成多少組
?*?@param?url
?*????????????String?頁(yè)面跳轉(zhuǎn)的路徑,若沒(méi)有特殊的參數(shù)傳遞,可以傳入null或"",
?*????????????如是在aciton里面調(diào)用,并且action是繼承自DispatherAction的話(huà)最好傳入完整的路徑
?*/
public?void?init(HttpServletRequest?request,?String?sql,?int?pageSize,
int?groupSize,?int?pageNo)?{
init(request,?sql,?pageSize,?groupSize,?pageNo,?"");
}
太多了,貼不下,見(jiàn)附件
實(shí)現(xiàn)原理很簡(jiǎn)單,就是建立一個(gè)Page類(lèi),里面放當(dāng)前訪(fǎng)問(wèn)的頁(yè)數(shù)(這個(gè)是從客戶(hù)瀏覽器傳到后臺(tái)的數(shù)據(jù),所以你的分頁(yè)需要用它來(lái)定位記錄的條目)和每一搏猜頁(yè)顯示的記錄行數(shù)。然后通過(guò)分頁(yè)計(jì)算就可以得出下列數(shù)據(jù)。
(假定你的頁(yè)數(shù)從1開(kāi)始)
1、總頁(yè)數(shù) = 總記錄數(shù)/每頁(yè)大小,如果0!=總記錄數(shù)%每頁(yè)大小,那么總頁(yè)數(shù)再+1
2、當(dāng)前頁(yè)數(shù)(從瀏覽器傳遞的參數(shù)中獲得)
3、表記錄的起始位置=(當(dāng)前頁(yè)數(shù)-1)*每頁(yè)大小
4、總旁銀念記錄數(shù)(select count(*) from [表名] [where [條件]],從數(shù)據(jù)庫(kù)中查詢(xún)得到)
5、每頁(yè)大小,可以固定,也可以從頁(yè)面?zhèn)鬟^(guò)來(lái)
有了這幾個(gè)參數(shù)之后,就用sql語(yǔ)句查出對(duì)應(yīng)的記錄就可以了。
mysql數(shù)據(jù)庫(kù)用limit 表記錄的起始位置,每頁(yè)大小 語(yǔ)句添加到你的查詢(xún)語(yǔ)句最后面
sqlserver數(shù)據(jù)庫(kù)運(yùn)困用top語(yǔ)句和not in 來(lái)做
oracle數(shù)據(jù)庫(kù)用rownum來(lái)做
再給你一段分頁(yè)對(duì)象代碼,你自己先讀一下
pre t="code" l="java"public class Page {
private long totalCount = 0;// 總記錄數(shù)
private int pageNumber = 1;// 當(dāng)前頁(yè)號(hào),默認(rèn)顯示第一頁(yè)
private int pageSize = 20; // 每頁(yè)大小,默認(rèn)每頁(yè)20條
private int totalPage = 0;// 總頁(yè)數(shù),默認(rèn)為0
private int startRow = 0;// 起始記錄行號(hào),默認(rèn)為從表頭開(kāi)始
/**
* 分頁(yè)計(jì)算方法,由setTotalCount調(diào)用
*/
public void pagination() {
// 計(jì)算總頁(yè)數(shù)
if (this.totalCount % pageSize == 0)
this.totalPage = new Long(this.totalCount / pageSize).intValue();
else
this.totalPage = new Long(this.totalCount / pageSize).intValue() + 1;
// 排除錯(cuò)誤頁(yè)號(hào)
if (this.pageNumber 1)
this.pageNumber = 1;
if (this.pageNumber this.totalPage)
this.pageNumber = this.totalPage;
// 計(jì)算起始行號(hào)
this.startRow = (this.pageNumber - 1) * this.pageSize;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
this.pagination();
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
}
這個(gè)很簡(jiǎn)單啊,如果總記錄數(shù)豎饑除以每頁(yè)的記錄數(shù)正好除盡的話(huà)碼罩,那么最大頁(yè)數(shù)就是總記錄數(shù)除以每頁(yè)的記錄數(shù),否則就是總記錄數(shù)除以每頁(yè)的記錄數(shù)+1咯,例如總記錄數(shù)為100條,每頁(yè)是20條,那么分為5頁(yè)(100/20),如果總記錄數(shù)為101條,那么應(yīng)該分為(101/20+1)也就是6頁(yè),有余模返問(wèn)題嗎