Struts paging control

xiaoxiao2021-03-06  58

Seeing that everyone's enthusiasm for Struts is very enthusiastic, and this information is very small. Looking at everyone is very tired, so it is kind, seeing a good paging method, and put it out to share together! (Originally in javaworld), there is an imperfect place, I hope everyone will correct! ! !

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

package com.tower.util; public class PageController {int totalRowsAmount; // Number of rows boolean rowsAmountSet; // is set too totalRowsAmount int pageSize = 2; Number of lines per page // int currentPage = 1; // current page int nextPage; int previousPage; int totalPages; // total number of pages boolean hasNext; // if a next boolean hasPrevious; // if there is a front 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; // calculate this page begins and ending lines if (currentPage * pageSize 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 ()} 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.gettotallrowsamount () " items " this.gettotalpages () " pages "; // this.currentpage " previous " this.hasprevious //" Next: " TH is.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.hasprevious " NEXT: " THIS. HASNEXT " START ROW: " this.pagestRow " 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 Action code snippet

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) Code pieces of page action

public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {// read page parameters TurnPageForm turnPageForm = (TurnPageForm) form; // PageController removed from the query information and uses bean call interface provided 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); // write data to request request.removeAttribute ( "queryResult") based on the parameter; request.removeAttribute ( "pageController" ); Request.setttribute ("QueryResult", Request.setttribute ("PageController", PC); // forward to the display page Return mapping.findforward ("ha VERESULT ");} (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) {r.absolute (pc.getpagestartrow ()); do {system.out.Println ("in loop rs.getrow ()); base b = new base (); B.setname (RS.GetString ("Name")); B.SetidCard ("IDCARD"); System.out.Println ("from DB:" rs.getstring ("iDCARD" ))); EMPS.ADD (b); if (! Rs.next ()) {Break;}} while (pc.getpageendrow () 1)));} Return EMPS;} (5 ) In JSP, the code snippet of the page section

"class =" a02 "> previous 转载请注明原文地址:https://www.9cbs.com/read-118564.html


New Post(0)