分頁處理、儲存工具類。
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 儲存分頁處理工具類 在調用此類的方法之前需設置總頁數(即得先從數據庫查詢到相應數據的數據量)
*
* @author ahomeeye
* @version 1.0
*/
public class Pagination implements Serializable {
private static final long serialVersionUID = 1L;
public final static String FIRST_ACTION = "First"; // 執行跳到第一頁操作
public final static String NEXT_ACTION = "Next"; // 執行跳到下一頁操作韓國女裝
public final static String PREVIO_ACTION = "Prev"; // 執行跳到上一頁操作
public final static String LAST_ACTION = "Last"; // 執行跳到最一頁操作
public final static String CURRENT_TAG = "currentPage"; // 當前頁數
public final static String PAGINATION_ACTION_TAG = "paginationAction"; // 緩存操作
public final static String GOTO_PAGE_ACTION = "gotoPage"; // 執行跳到指定的某一頁操作
public final static String PAGES_GOTO = "pageSelect"; // 執行goto操作時,用戶所指定的頁數
private int start; // start表示當前頁開始的記錄數,start=每頁行數*(當前頁數-1)
private int end; // 當前頁結束的記錄行數
private int totalCount; // 總行數
private int rowsPerPage = 15; // 每頁行數,默認15
private int currentPage; // 當前頁數
private int pageListSize = 9;// 頁碼列表大小,默認9
private List<Integer> pageNumList = new ArrayList<Integer>();
public Pagination() {
start = 0;
end = 0;
currentPage = 1;
this.totalCount = 0;
}
public Pagination(int totalCount) {
start = 0;
end = 0;
currentPage = 1;
this.totalCount = totalCount;
}
public Pagination(int totalCount, int numPerPage) {
start = 0;
end = 0;
this.totalCount = totalCount;
currentPage = 1;
if (numPerPage > 0) {
rowsPerPage = numPerPage;
}
}
/**
* 執行翻頁動作
*
* @param currentPage
* 要翻到的目標頁碼
* @return 返回翻頁對象
*/
public Pagination doPagination(int currentPage) {
gotoPage(currentPage);
return this;
}
// 設置起始數
public int getStart() {
start = rowsPerPage * (currentPage - 1);
return start;
}
// 得到起始數
public void setStart(int start) {
if (start < 0) {
this.start = 0;
} else if (start >= this.totalCount) {
this.start = this.totalCount - 1;
} else {
this.start = start;
}
}
// 設置當前頁的最后一行的在總記錄中的順序(從0開始)
public void setEnd(int end) {
this.end = end;
}
// 得到當前頁的最后一行的在總記錄中的順序(從0開始)
public int getEnd() {
if (rowsPerPage * currentPage > this.totalCount) {
end = this.totalCount - 1;
} else {
end = rowsPerPage * currentPage - 1;
}
return end;
}
// 以下4個方法供控制器(struts)調用
// 判斷能否到第一頁;只要能到上一頁,肯定就有第一頁
public boolean firstEnable() {
return previousEnable();
}
// 判斷能否到上一頁
public boolean previousEnable() {
return currentPage > 1;// 只要不是第一頁,就能到上一頁
}
// 判斷能否到下一頁
public boolean nextEnable() {
return currentPage * rowsPerPage < this.totalCount;
}
// 判斷能否到最后一頁;只要有下一頁,就肯定有最后一頁.
public boolean lastEnable() {
return nextEnable();
}
// 跳到第一頁
public void firstPage() {
currentPage = 1;
}
// 跳到上一頁
public void previousPage(int cPage) {
currentPage = (cPage - 1) > 0 ? (cPage - 1) : 1;
}
// 跳到下一頁
public void nextPage(int cPage) {
currentPage = cPage + 1;
if (currentPage * rowsPerPage > this.totalCount) {
lastPage();
}
}
// 跳到最后一頁
public void lastPage() {
if (this.totalCount % rowsPerPage == 0) {
currentPage = this.totalCount / rowsPerPage;
} else {
currentPage = this.totalCount / rowsPerPage + 1;
}
}
// 跳到指定的某一頁
public void gotoPage(int pageNumber) {
if (pageNumber <= 1) {
currentPage = 1;
} else if (getTotalCount() < this.getRowsPerPage()) {
currentPage = 1;
} else if (pageNumber * rowsPerPage >= this.totalCount) {
lastPage();
} else {
currentPage = pageNumber;
}
}
// 設置總行數
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
// 得到總行數
public int getTotalCount() {
return totalCount;
}
// 設置每頁行數
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
// 得到每頁行數
public int getRowsPerPage() {
return rowsPerPage;
}
// 得到總頁數
public int getPages() {
if (this.totalCount % rowsPerPage == 0)
return this.totalCount / rowsPerPage;
else
return this.totalCount / rowsPerPage + 1;
}
// 得到當前頁數
public int getCurrentPage() {
return currentPage;
}
// 設置當前頁數
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageListSize() {
return pageListSize;
}
// 設置頁碼列表大小
public void setPageListSize(int pageListSize) {
this.pageListSize = pageListSize;
}
// 得到頁面列表
public List<Integer> getPageNumList() {
this.pageNumList.removeAll(this.pageNumList);// 設置之前先清空
int totalPage = getPages();
if (totalPage > this.pageListSize) {
int halfSize = this.pageListSize / 2;
int first = 1;
int end = 1;
if (this.currentPage - halfSize < 1) { // 當前頁靠近最小數1
first = 1;
end = this.pageListSize;
} else if (totalPage - this.currentPage < halfSize) { // 當前頁靠近最大數
first = totalPage - this.pageListSize + 1;
end = totalPage;
} else {
first = this.currentPage - halfSize;
end = this.currentPage + halfSize;
}
for (int i = first; i <= end; i++) {
this.pageNumList.add(i);
}
} else {
for (int i = 0; i < totalPage; i++) {
this.pageNumList.add(i + 1);
}
}
return pageNumList;
}
}
import java.util.ArrayList;
import java.util.List;
/**
* 儲存分頁處理工具類 在調用此類的方法之前需設置總頁數(即得先從數據庫查詢到相應數據的數據量)
*
* @author ahomeeye
* @version 1.0
*/
public class Pagination implements Serializable {
private static final long serialVersionUID = 1L;
public final static String FIRST_ACTION = "First"; // 執行跳到第一頁操作
public final static String NEXT_ACTION = "Next"; // 執行跳到下一頁操作韓國女裝
public final static String PREVIO_ACTION = "Prev"; // 執行跳到上一頁操作
public final static String LAST_ACTION = "Last"; // 執行跳到最一頁操作
public final static String CURRENT_TAG = "currentPage"; // 當前頁數
public final static String PAGINATION_ACTION_TAG = "paginationAction"; // 緩存操作
public final static String GOTO_PAGE_ACTION = "gotoPage"; // 執行跳到指定的某一頁操作
public final static String PAGES_GOTO = "pageSelect"; // 執行goto操作時,用戶所指定的頁數
private int start; // start表示當前頁開始的記錄數,start=每頁行數*(當前頁數-1)
private int end; // 當前頁結束的記錄行數
private int totalCount; // 總行數
private int rowsPerPage = 15; // 每頁行數,默認15
private int currentPage; // 當前頁數
private int pageListSize = 9;// 頁碼列表大小,默認9
private List<Integer> pageNumList = new ArrayList<Integer>();
public Pagination() {
start = 0;
end = 0;
currentPage = 1;
this.totalCount = 0;
}
public Pagination(int totalCount) {
start = 0;
end = 0;
currentPage = 1;
this.totalCount = totalCount;
}
public Pagination(int totalCount, int numPerPage) {
start = 0;
end = 0;
this.totalCount = totalCount;
currentPage = 1;
if (numPerPage > 0) {
rowsPerPage = numPerPage;
}
}
/**
* 執行翻頁動作
*
* @param currentPage
* 要翻到的目標頁碼
* @return 返回翻頁對象
*/
public Pagination doPagination(int currentPage) {
gotoPage(currentPage);
return this;
}
// 設置起始數
public int getStart() {
start = rowsPerPage * (currentPage - 1);
return start;
}
// 得到起始數
public void setStart(int start) {
if (start < 0) {
this.start = 0;
} else if (start >= this.totalCount) {
this.start = this.totalCount - 1;
} else {
this.start = start;
}
}
// 設置當前頁的最后一行的在總記錄中的順序(從0開始)
public void setEnd(int end) {
this.end = end;
}
// 得到當前頁的最后一行的在總記錄中的順序(從0開始)
public int getEnd() {
if (rowsPerPage * currentPage > this.totalCount) {
end = this.totalCount - 1;
} else {
end = rowsPerPage * currentPage - 1;
}
return end;
}
// 以下4個方法供控制器(struts)調用
// 判斷能否到第一頁;只要能到上一頁,肯定就有第一頁
public boolean firstEnable() {
return previousEnable();
}
// 判斷能否到上一頁
public boolean previousEnable() {
return currentPage > 1;// 只要不是第一頁,就能到上一頁
}
// 判斷能否到下一頁
public boolean nextEnable() {
return currentPage * rowsPerPage < this.totalCount;
}
// 判斷能否到最后一頁;只要有下一頁,就肯定有最后一頁.
public boolean lastEnable() {
return nextEnable();
}
// 跳到第一頁
public void firstPage() {
currentPage = 1;
}
// 跳到上一頁
public void previousPage(int cPage) {
currentPage = (cPage - 1) > 0 ? (cPage - 1) : 1;
}
// 跳到下一頁
public void nextPage(int cPage) {
currentPage = cPage + 1;
if (currentPage * rowsPerPage > this.totalCount) {
lastPage();
}
}
// 跳到最后一頁
public void lastPage() {
if (this.totalCount % rowsPerPage == 0) {
currentPage = this.totalCount / rowsPerPage;
} else {
currentPage = this.totalCount / rowsPerPage + 1;
}
}
// 跳到指定的某一頁
public void gotoPage(int pageNumber) {
if (pageNumber <= 1) {
currentPage = 1;
} else if (getTotalCount() < this.getRowsPerPage()) {
currentPage = 1;
} else if (pageNumber * rowsPerPage >= this.totalCount) {
lastPage();
} else {
currentPage = pageNumber;
}
}
// 設置總行數
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
// 得到總行數
public int getTotalCount() {
return totalCount;
}
// 設置每頁行數
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
// 得到每頁行數
public int getRowsPerPage() {
return rowsPerPage;
}
// 得到總頁數
public int getPages() {
if (this.totalCount % rowsPerPage == 0)
return this.totalCount / rowsPerPage;
else
return this.totalCount / rowsPerPage + 1;
}
// 得到當前頁數
public int getCurrentPage() {
return currentPage;
}
// 設置當前頁數
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageListSize() {
return pageListSize;
}
// 設置頁碼列表大小
public void setPageListSize(int pageListSize) {
this.pageListSize = pageListSize;
}
// 得到頁面列表
public List<Integer> getPageNumList() {
this.pageNumList.removeAll(this.pageNumList);// 設置之前先清空
int totalPage = getPages();
if (totalPage > this.pageListSize) {
int halfSize = this.pageListSize / 2;
int first = 1;
int end = 1;
if (this.currentPage - halfSize < 1) { // 當前頁靠近最小數1
first = 1;
end = this.pageListSize;
} else if (totalPage - this.currentPage < halfSize) { // 當前頁靠近最大數
first = totalPage - this.pageListSize + 1;
end = totalPage;
} else {
first = this.currentPage - halfSize;
end = this.currentPage + halfSize;
}
for (int i = first; i <= end; i++) {
this.pageNumList.add(i);
}
} else {
for (int i = 0; i < totalPage; i++) {
this.pageNumList.add(i + 1);
}
}
return pageNumList;
}
}