Seeing that everyone is enthusiastic about struts learning, and there is very little information in this area, I am very tired, so I am very grateful. I will post a good paging method and share it with you! (The original version is in javaworld). I hope you can correct it !!!
A page control class is used to record page information, such as the previous, next, and current pages. In the query action, the control class and query condition are passed to the database access bean, and the two parameters are saved in the user session. In the paging control action, the received paging parameters are used to call the bean for database access.
(1)Paging Control
Package com. Tower. util;
Public class pagecontroller {
Int totalrowsamount; file: // total number of rows
Boolean rowsamountset; file: // whether totalrowsamount has been set
Int pagesize = 2; file: // number of lines per page
Int currentpage = 1; file: // current page number
Int nextpage;
Int previouspage;
Int totalpages; file: // total number of pages
Boolean hasnext; file: // whether the next page exists
Boolean hasprevious; file: // whether the previous page exists
String description;
Int pagestartrow;
Int pageendrow;
Public pagecontroller (INT totalrows ){
Settotalrowsamount (totalrows );
}
Public pagecontroller (){}
Public void settotalrowsamount (int I ){
If (! This. rowsamountset ){
Totalrowsamount = I;
Totalpages = totalrowsamount/pagesize + 1;
Setcurrentpage (1 );
This. rowsamountset = true;
}
}
Public void setcurrentpage (int I ){
Currentpage = I;
Nextpage = currentpage + 1;
Previouspage = currentPage-1;
File: // calculate the start and end rows of the current page.
If (currentpage * pagesize <totalrowsamount ){
Pageendrow = currentpage * pagesize;
Pagestartrow = pageendrow-pagesize + 1;
} Else {
Pageendrow = totalrowsamount;
Pagestartrow = pagesize * (totalPages-1) + 1;
}
If (nextpage> totalpages ){
Hasnext = false;
} Else {
Hasnext = true;
}
If (previouspage = 0 ){
Hasprevious = false;
} Else {
Hasprevious = true;
};
System. Out. println (this. Description ());
}
Public int getcurrentpage (){
Return currentpage;
}
Public Boolean ishasnext (){
Return hasnext;
}
Public Boolean ishasprevious (){
Return hasprevious;
}
Public int getnextpage (){
Return nextpage;
}
Public int getpagesize (){
Return pagesize;
}
Public int getpreviouspage (){
Return previouspage;
}
Public int gettotalpages (){
Return totalpages;
}
Public int gettotalrowsamount (){
Return totalrowsamount;
}
Public void sethasnext (Boolean B ){
Hasnext = B;
}
Public void sethasprevious (Boolean B ){
Hasprevious = B;
}
Public void setnextpage (int I ){
Nextpage = I;
}
Public void setpagesize (int I ){
Pagesize = I;
}
Public void setpreviouspage (int I ){
Previouspage = I;
}
Public void settotalpages (int I ){
Totalpages = I;
}
Public int getpageendrow (){
Return pageendrow;
}
Public int getpagestartrow (){
Return pagestartrow;
}
Public String getdescription (){
String description = "Total:" + this. gettotalrowsamount () +
"Items" + this. gettotalpages () + "pages ";
// This. currentpage + "previous" + this. hasprevious +
// "Next:" + this. hasnext +
// "Start row:" + this. pagestartrow +
// "End row:" + this. pageendrow;
Return description;
}
Public String description (){
String description = "Total:" + this. gettotalrowsamount () +
"Items" + this. gettotalpages () + "pages, current page:" +
This. currentpage + "previous" + this. hasprevious +
"Next:" + this. hasnext +
"Start row:" + this. pagestartrow +
"End row:" + this. pageendrow;
Return description;
}
Public static void main (string ARGs []) {
Pagecontroller Pc = new pagecontroller (3 );
System. Out. println (PC. getdescription ());
// PC. setcurrentpage (2 );
// System. Out. println (PC. Description ());
// PC. setcurrentpage (3 );
// System. Out. println (PC. Description ());
}