Author: Unknown Source: http://www.jspcn.net/
Access times: Join time:
One implementation of paging in struts
Pagination in my projects
1. Ideas
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.
2. Implementation
(1) Paging Control
/* @ Author Nick
* Created on 2004-3-18
* File name: pagecontroller. Java
*
*
*/
Package com. Tower. util;
/**
* @ Author Nick
* 2004-3-18
* Used for page turning control
*
*/
Public class pagecontroller {
Int totalrowsamount; // total number of rows
Boolean rowsamountset; // whether totalrowsamount has been set
Int pagesize = 2; // number of lines per page
Int currentpage = 1; // current page number
Int nextpage;
Int previouspage;
Int totalpages; // the total number of pages.
Boolean hasnext; // whether the next page exists
Boolean hasprevious; // whether the previous page exists
String description;
Int pagestartrow;
Int pageendrow;
Public pagecontroller (INT totalrows ){
Settotalrowsamount (totalrows );
}
Public pagecontroller (){}
/**
* @ Param I
* Set the total number of rows.
*/
Public void settotalrowsamount (int I ){
If (! This. rowsamountset ){
Totalrowsamount = I;
Totalpages = totalrowsamount/pagesize + 1;
Setcurrentpage (1 );
This. rowsamountset = true;
}
}
/**
* @ Param I
*
* Current page
*
*/
Public void setcurrentpage (int I ){
Currentpage = I;
Nextpage = currentpage + 1;
Previouspage = currentPage-1;
// 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;
}
// Whether the front and back pages exist
If (nextpage> totalpages ){
Hasnext = false;
} Else {
Hasnext = true;
}
If (previouspage = 0 ){
Hasprevious = false;
} Else {
Hasprevious = true;
};
System. Out. println (this. Description ());
}
/**
* @ Return
*/
Public int getcurrentpage (){
Return currentpage;
}
/**
* @ Return
*/
Public Boolean ishasnext (){
Return hasnext;
}
/**
* @ Return
*/
Public Boolean ishasprevious (){
Return hasprevious;
}
/**
* @ Return
*/
Public int getnextpage (){
Return nextpage;
}
/**
* @ Return
*/
Public int getpagesize (){
Return pagesize;
}
/**
* @ Return
*/
Public int getpreviouspage (){
Return previouspage;
}
/**
* @ Return
*/
Public int gettotalpages (){
Return totalpages;
}
/**
* @ Return
*/
Public int gettotalrowsamount (){
Return totalrowsamount;
}
/**
* @ Param B
*/
Public void sethasnext (Boolean B ){
Hasnext = B;
}
/**
* @ Param B
*/
Public void sethasprevious (Boolean B ){
Hasprevious = B;
}
/**
* @ Param I
*/
Public void setnextpage (int I ){
Nextpage = I;
}
/**
* @ Param I
*/
Public void setpagesize (int I ){
Pagesize = I;
}
/**
* @ Param I
*/
Public void setpreviouspage (int I ){
Previouspage = I;
}
/**
* @ Param I
*/
Public void settotalpages (int I ){
Totalpages = I;
}
/**
* @ Return
*/
Public int getpageendrow (){
Return pageendrow;
}
/**
* @ Return
*/
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 ());
}
}
(2) query the code snippet of the action
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {
Base queryform = (base) form;
If (! Queryform. getname (). Equals ("")){
Pagecontroller Pc = new pagecontroller ();
Employeebase service = new employeebase ();
Arraylist result = (arraylist) service. Search (queryform, PC );
Httpsession session = request. getsession ();
Session. setattribute ("queryform", queryform );
Session. setattribute ("pagecontroller", Service. getpagecontroller ());
Request. setattribute ("queryresult", result );
Request. setattribute ("pagecontroller", Service. getpagecontroller ());
Return Mapping. findforward ("haveresult ");
} Else {
Return Mapping. findforward ("noresult ");
}
}
(3) flip the code snippet of the action
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {
// Read paging Parameters
Turnpageform = (turnpageform) form;
// Retrieve the query information from pagecontroller and use the call interface provided by bean to process the result
Httpsession session = request. getsession ();
Pagecontroller Pc = (pagecontroller) Session. getattribute ("pagecontroller ");
Base queryform = (base) Session. getattribute ("queryform ");
PC. setcurrentpage (turnpageform. getviewpage ());
Employeebase service = new employeebase ();
Arraylist result = (arraylist) service. Search (queryform, PC );
// Write data to the request according to the parameter
Request. removeattribute ("queryresult ");
Request. removeattribute ("pagecontroller ");
Request. setattribute ("queryresult", result );
Request. setattribute ("pagecontroller", PC );
// Forward to the display page
Return Mapping. findforward ("haveresult ");
}
(4) fragments in the database access Bean
Public Collection Search (base, pagecontroller PC)
Throws sqlexception {
Arraylist EMPs = new arraylist ();
Resultset rs = getsearchresult (base );
Rs. Absolute (-1 );
PC. settotalrowsamount (Rs. getrow ());
Setpagecontroller (PC );
If (Rs. getrow ()> 0 ){
Rs. Absolute (PC. getpagestartrow ());
Do {
System. Out. println ("in loop" + Rs. getrow ());
Base B = new base ();
B. setname (Rs. getstring ("name "));
B. setidcard (Rs. getstring ("idcard "));
System. Out. println ("from DB:" + Rs. getstring ("idcard "));
EMPs. Add (B );
If (! Rs. Next ()){
Break;
}
} While (Rs. getrow () <(PC. getpageendrow () + 1 ));
}
Return EMPs;
}
(5) code snippets in pages in JSP
<Bean: write name = "pagecontroller" property = "Description"/>
<Logic: equal name = "pagecontroller" property = "hasprevious" value = "true">
<A href = "turnpage. do? Viewpage = <Bean: write name = "pagecontroller" property = "previouspage"/> "class =" A02 ">
Previous
</A>
</Logic: equal>
<Logic: equal name = "pagecontroller" property = "hasnext" value = "true">
<A href = "turnpage. do? Viewpage = <Bean: write name = "pagecontroller" property = "nextpage"/> "class =" A02 ">
Next
</A>
</Logic: equal>
In this way, the page flip function can be displayed to the client as you like.