說到分頁算法
看了網上相當多的分頁算法
首先
分頁大致需要如下屬性
private int currentPage =
private int totalPages =
private int pageRecorders =
private int totalRows =
private int pageStartRow =
private int pageEndRow =
private boolean hasNextPage = false; // 是否有下一頁
private boolean hasPreviousPage = false; // 是否有前一頁
private int nextPage =
private int previousPage =
然後這些屬性之間是有聯系的
有兩種方法
一
public PageBean(int totalRows){
this
this
hasPreviousPage = false;
if ((totalRows % pageRecorders) ==
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders +
}
if (totalRows >= pageRecorders) {
hasNextPage = true;
nextPage =
this
} else {
this
hasNextPage = false;
nextPage =
}
this
previousPage =
}
然後在按下一頁或者上一頁的時候需要如下函數處理
public void nextPage() {
if(hasNextPage == true)
currentPage = currentPage +
if ((currentPage
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currentPage >= totalPages) {
hasNextPage = false;
this
} else {
hasNextPage = true;
nextPage = currentPage+
}
this
if(hasNextPage == true)
this
else{
this
}
previousPage = currentPage
}
public void previousPage() {
if(hasPreviousPage == true)
currentPage = currentPage
if (currentPage ==
currentPage =
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
nextPage = currentPage +
if ((currentPage
hasPreviousPage = true;
previousPage = currentPage
} else {
hasPreviousPage = false;
previousPage = currentPage;
}
this
if(hasNextPage == true)
this
else{
this
}
}
在HTML中按下一頁或者上一頁的時候有如下代碼
<logic:equal name=
<html:link page=
nextPage
</html:link>
</logic:equal>
<logic:equal name=
<html:link page=
PreviousPage
</html:link>
</logic:equal>
然後在Action中作如下處理
String currentPage = request
HttpSession session = request
EmployeeForm employeeForm = (EmployeeForm) form;
String queryString = null;
String queryCon = null;
String action = employeeForm
List list = new ArrayList();
PageBean pb = null;
EmployeeDao employeeDao = new EmployeeDao();
if(action == null || action
int totalRows = employeeDao
pb = new PageBean(totalRows);
session
queryString = employeeForm
queryCon = employeeForm
session
session
list = employeeDao
String
String
}else if(action
queryString = (String)session
queryCon = (String)session
employeeForm
employeeForm
pb = (PageBean)session
pb
list = employeeDao
String
String
}else if(action
queryString = (String)session
queryCon = (String)session
employeeForm
employeeForm
pb = (PageBean)session
pb
list = employeeDao
String
String
}
pb
session
request
request
return mapping
然後在數據庫查詢中有如下代碼
/**
*查詢總記錄數
*/
public int getTotalRows() {
int totalRows =
String sql =
Database db = new Database();
ResultSet rs = db
try {
while (rs
String id = (String) rs
totalRows = Integer
}
} catch (SQLException e) {
e
}
db
return totalRows;
}
/*
*查詢每一頁需要查詢的頁碼
*/
public List getAllEmployee(String queryString
List list = new ArrayList();
String sql = null;
if (queryString == null || queryString
sql =
} else {
sql =
+ queryString +
}
Employee employee = null;
Database db = new Database();
ResultSet rs = db
try {
while (rs
String id = (String) rs
String name = (String) rs
String deptId = (String) rs
String deptName = (String) rs
employee = new Employee();
employee
employee
employee
employee
list
}
} catch (SQLException e) {
e
}
db
return list;
}
這裡我用了hibernate進行數據庫操作
二
這樣的話構造函數應該寫成:
public PageBean(int totalRows
this
this
if(currentPage <
hasPreviousPage = false;
else
hasPreviousPage = true;
if ((totalRows % pageRecorders) ==
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders +
}
if (currentPage < totalPages) {
hasNextPage = true;
nextPage = currentPage +
pageStartRow = (currentPage
this
} else if(currentPage == totalPages){
pageStartRow = (currentPage
this
hasNextPage = false;
nextPage = currentPage;
}
if(currentPage <
previousPage = currentPage;
hasPreviousPage = false;
}else if(currentPage >
previousPage = currentPage
hasPreviousPage = true;
}
}
在action中應該寫成
if(currentPage == null){
pb = new PageBean(totalRows);
session
queryString = employeeForm
queryCon = employeeForm
session
session
list = employeeDao
String
String
}
else{
pb = new PageBean(totalRows
queryString = employeeForm
queryCon = employeeForm
session
session
list = employeeDao
String
String
}
session
request
request
return mapping
在jsp中應該寫成
<logic:equal name=
<a List
nextPage
</a>
</logic:equal>
<logic:equal name=
|
<a /test/List
PreviousPage
</a>
</logic:equal>
數據庫查詢部分依然適用
盡管洋洋灑灑貼了一部分代碼
首先這兩種方法都是取需要顯示的數據顯示
第一種方法是把PageBean存在了一個HttpSession中
第二種方法是考慮了第一種方法的BUG
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28793.html