How to implement pagination display in the Struts database application
[Related Links] "JavaWebStudio Series Development Tools Visaul Struts Report" http://dev.9cbs.net/develop/Article/28/28871.shtm "How to achieve the simplest struts program" http: //dev.9cbs. Net / development / article / 28 / 28946.SHTM "Struts drop-down box" http://dev.9cbs.net/develop/Article/28/28956.SHTM "http: // dev" http: // dev .9cbs.net / development / article / 29 / 29042.shtm "Struts check box" http://dev.9cbs.net/develop/Article/29/29043 The relationship between the SHTM "Struts program (1) "http://dev.9cbs.net/develop/article/29/29131.shtm" The relationship between the Struts program each file (2) "http://dev.9cbs.net/develop/Article/ 29 / 29133.SHTM "How to implement the struts database application (1)" http://dev.9cbs.net/develop/Article/29/29135.shtm "How to implement the Struts database application (2)" http: // Dev.9cbs.net/develop/Article/29/29138.shtm "How to implement pagination display in the Struts database application (1)" http://dev.9cbs.net/develop/Article/29/29139.shtm " How to implement paging display (2) "" How to implement record in the Struts database application "in the Struts database application" "How to add a record in the Struts database application" "How to build a landing Page Struts Database Application "How to create a Struts database application with landing page and role"
The above example development environment: Java Web Studio Series Development Environment Visual Strutst version. The latest JavaWebStudio download address: ftp: //210.36.64.79/kui The above example source program download address: ftp: //210.36.64.79/kui
Key Tip: The setting of the JSPOUT property page is that this example is different from the steps in "How to Implement Struts Database Application", which is the core content of this example. We have to do just to select the "Auto Pagination" selection and enter the number of records per page in the settings of the JSPOUT property page.
DBListDao.java file:
Here, a DAO class is defined to implement functions such as data plus, delete, update, add, and generate zone strings. The getPagestr (int ipage) function is used to generate a pages string, and the int ipage parameter is used to transfer the current page number. The FindSQL (STRING SQL, INT iPAGE) function is used to find database data, and the String SQL parameter is used to deliver the SQL query statement string, and the int ipage parameter is used to transfer the current page number.
. . . . . . . . . . (Omit)
Public class dblistdao {
PRIVATE CONNECTION CON;
PRIVATE INT ROWCOUNT;
PRIVATE INT pageCOUNT;
PRIVATE INT Length;
PRIVATE STRING PAGESTR;
Public int getLength () {return (this.Length);
Public void setlength (int length) {this.Length = length;} public string getpageStr (int ipage)
{
String strpage = ""
IF (getLength ()> 0)
{
STRPAGE = "Total";
StrPage = String.Valueof (Rowcount);
STRPAGE = "Strip record, total";
StrPage = String.Valueof (pagecount);
STRPAGE = "page, currently the first";
StrPage = String.Valueof (ipage);
STRPAGE = "page,";
INT iStart, IEND;
iStart = ipage-5;
IF (iStart <0) {iStart = 0;}
IEND = ISTART 10;
Ie (IEND> PageCount) {IEND = PageCount;}
iStart = IEND-10;
IF (iStart <0) {iStart = 0;}
For (INT i = iStart; i { STRPAGE = ""; STRPAGE = String.Valueof (i 1); STRPAGE = ""; STRPAGE = "" } } this.pageStr = STRPAGE; Return strpage; } The intermediate code is independent of the paging, and it is omitted. Public Collection Findsql (String SQL, INT iPAG) { PreparedStatement PS = NULL; ResultSet RS = NULL; ArrayList List = New ArrayList (); Try { IF (con?isclosed ()) { Throw New IllegalStateException ("Error.unexpected"); } PS = con.preparestatement (SQL, ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata); RS = ps.executeQuery (); Rs.absolute (-1); Rowcount = rs.getrow (); INT offset = 1; INT PAGESIZE = GetLength (); IF (getLength () <1) { PageSize = rowcount; PageCount = 1; } Else { PageCount = rowcount / getLength () ((RowCount% getLength ())> 0? 1: 0); OFFSET = (ipage-1) * getLength () 1; IF (Offset <1) OFFSET = 1; if (Offset> Rowcount) OFFSET = RowCount; } rs.absolute (offset); For (int i = 0; i DBLIST M_DBLIST = New DBLIST (); // m_dblist.set serial number (Rs.Getint (1)); m_dblist.set name (rs.getstring (2)); M_dblist.set Remarks (rs.getstring (3)); // find> rs.next (); List.add (m_dblist); } Return List; } catch (sqlexception e) { //e.printstacktrace (); // throw new runtimeException ("Error.unexpected"); Return List; } finally { Try { IF (PS! = NULL) ps.close (); IF (rs! = null) Rs.close (); } catch (sqlexception e) { E.PrintStackTrace (); Throw new runtimeException ("error.unexpected"); } } } } DBListAction.java file: The code between . . . . . . . . . . (Omit) Public Final Class DBListAction Extends Action { // Variable definitions: Private log log = logfactory.getlog ("org.apache.struts.Webapp.example"); PRIVATE CONNECTIONPOOL POOL; Public dblistression () { Pool = connectionPool.getInstance (); } // Function definition: Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, Httpservletresponse response Throws Exception { // Extract Attributes and Parameters WE WILL NEED Locale locale = getLocale (request); MessageResources Messages = GetResources (Request); HttpSession session = request.getations (); DBListForm m_dblistform = (dblistform) form; Connection con = NULL; Try { (Omit) // m_dblistdao.setlength (10); // Set the number of records per page INT ipage; // current page Try { String Page = Request.getParameter ("Page"); // Pedestrian Parameters, read the current page of the request ipage = java.lang.integer.parseint (page, 10); } Catch (Exception E) { ipage = m_dblistform.getpage (); } IPAGE = 1; Collection col = m_dblistdao.findsql (SQL, IPAGE); // Query data via DAO object Request.setattribute ("dblist", col); // Save data String PageStr = m_dblistdao.getpagestr (ipage); // Paging string, paging content String s_find, str; IF (! "Search" .Equals (search) // lookup and all display two different operations, which are different, which is handled here. If all it is displayed, remove "Search = Search &" { s_find = "Search = SEARCH &"; While (PageStr.Indexof (s_find)! = - 1) { Str = pageStr.Substring (0, PageStr.Indexof (s_find)); Str = pageStr.substring (PageStr.Indexof (s_find) s_find.length (), pageStr.Length ()); PageStr = STR; } } m_dblistform.setpageStr (PageStr); m_dblistform.setAction; Request.setattribute ("dblistform", m_dblistform); // page> Return mapping.findforward ("Success"); } Catch (SQLException E) { (Omit) } } }