One implementation of paging in Struts

xiaoxiao2021-04-08  409

Paging feature in my project

1, ideas

Use a page control class, which record page information, such as page, next page, current page, etc. In the action of the query, this control class and query conditions are passed to the database access bean and then save the two parameters in the user session. In paging control Action, the BEAN access to the database is called using the received paging parameters.

2, realize

(1) Pieces Control class

/ * @Author Nick

* CREATED ON 2004-3-18

* File Name: PageController.java

*

*

* /

Package com.tower.util;

/ **

* @Author Nick

* 2004-3-18

* Used to turn page control

*

* /

Public class pageController {

INT TOTALROWSAMOUNT; / / total number

Boolean rowsamountset; // Whether to set TOTALROWSAMOUNT

INT PageSize = 2; // Variety per page

INT CurrentPage = 1; // Current page number

Int nextpage;

INT Previouspage;

INT TOTALPAGES; / / total page

Boolean Hasnext; // Is there a next page

Boolean Hasprevious; // Is there a previous page

String description;

INT PagestartRow;

Int pagendrow;

Public pageController (int Totalrow) {

SetTotAlrowsAmount (TotalRows);

}

Public pageController () {}

/ **

* @Param I

* Set the total number of lines

* /

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 current page to start rows and end lines

CurrentPage * Pagesize

PageEndrow = CURRENTPAGE * PAGESIZE

PagestRow = PageEndrow-Pagesize 1;

} else {

PageEndrow = TotalRowsamount;

PageStartrow = PageSize * (Totalpages-1) 1;

}

/ / Is there a previous page and

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 ishasis () {

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 getpagendrow () {

Return PageEndRow;

}

/ **

* @Return

* /

Public int getpagestRow () {

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.pagestRow

// "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.pagestRrow

"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 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.getations ();

Session.setttribute ("Queryform", queryform);

Session.SetaTRibute ("PageController", Service.getPageController ());

Request.setattribute ("QueryResult", Result;

Request.setttribute ("PageController", Service.getPageController ());

Return mapping.findforward ("haveresult");

} else {

Return mapping.findforward ("noresult");

}

}

(3), page the code snippet

Public ActionForward Execute

ActionMapping mapping,

Actionform Form,

HTTPSERVLETREQUEST REQUEST,

Httpservletresponse response

Throws exception {

// Read forward page parameters

TurNPageForm TurNPageForm = (TurNPageForm) Form; / / Take query information from pageController and use the call interface processing result provided by Bean

HttpSession session = request.getations ();

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 request according to parameters

Request.RemoveAttribute ("queryResult");

Request.RemoveAttribute ("PageController");

Request.setattribute ("QueryResult", Result;

Request.setttribute ("PageController", PC);

// forward to the display page

Return mapping.findforward ("haveresult");

}

(4) Database Access to Pieces in Beans

Public Collection Search (Base 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" qGetrow ());

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 pieces in JSP

Previous

NEXT

In this way, the functionality of the page can be expressed to the client in the way you like.

转载请注明原文地址:https://www.9cbs.com/read-132793.html

New Post(0)