Struts paging one implementation

xiaoxiao2021-04-05  272

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.

(1) Pieces Control class

Public class pageController {/ ***************************************************** / INT TOTALROWSAMOUNT; / / Total number boolean rowsamountset; // Whether to set TotalrowSamount Int Pagesize = 9; // Each page number INT CURRENTPAGE = 1; // Current page int nextPage; // Next Int PreviousPage; // One page INT TOTALPAGES; / / Total page Boolean Hasnext; // Is there Next Boolean Hasprevious; // Is there a previous page String Description; int PageStartRow; / / Specify a line Int pageEndrow; // Specify To the end of the location / ***************************************** /

public PageController () {} public PageController (int totalRows) {setTotalRowsAmount (totalRows);} (! this.rowsAmountSet) public void setTotalRowsAmount (int i) {if {totalRowsAmount = i; if (totalRowsAmount% pageSize == 0) totalPages = totalRowsAmount / pageSize; else totalPages = totalRowsAmount / pageSize 1; setCurrentPage (1); this.rowsAmountSet = true;}} public void setCurrentPage (int i) {// set this page currentPage = i; nextPage = currentPage 1; previousPage = currentPage-1; // calculate this page begins and ending lines if (currentPage * pageSize Totalpages) {HASNEXT = false;} else {hasnext = true;} // Decision Is there a previous page 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"; 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.pagestRow "End Row:" this.pageendrow; return description;}}

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 .forward ("noresult");}} (3), paged the code snippet

public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {file: // page read parameter TurnPageForm turnPageForm = (TurnPageForm) form; file: // PageController extracted from the query information, and bean call interface provides the processing 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); file: // parameter data write request request.removeAttribute ( "queryResult") according to; Request .removeAttribute ( "pageController"); request.setAttribute ( "queryResult", result); request.setAttribute ( "pageController", pc); file: // forward to the display page return mapping.findForward ( "haveResult");} ( 4) Database Access to the segment in Bean

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 (pc.getpagendrow () 1);} return Emps;}

(5) In JSP, the code snippet of the page section " Previous

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


New Post(0)