1. First defined a Pageutil class, looked at the online many tutorials, seems to have a such class, oneself also defined a. Discovering this class during code writing is not really that much of a function, but it doesn't define the class, it feels like it's missing something. (Ask the master to explain the meaning of the existence of this class.) )
1 PackageCom.util;2 3 Public classPageutil {4 Private intTotalrows;//total number of records5 Private intCurpage;//Current page number6 Private intPageSize;//number of records per page7 Private intTotalPages;8 PublicPageutil (intTotalrows,intCurpage,intpageSize) {9 Super();Ten This. totalrows =totalrows; One This. Curpage =Curpage; A This. pageSize =pageSize; - } - PublicPageutil (intTotalrows,intpageSize) { the This. totalrows=totalrows; - This. pagesize=pageSize; - } - + //Total Pages - Public intgettotalpages () { + returnTotalrows%pagesize==0?totalrows/pagesize:totalrows/pagesize+1; A } at Public intgettotalrows () { - returntotalrows; - } - Public voidSettotalrows (inttotalrows) { - This. totalrows =totalrows; - } in Public intGetcurpage () { - returnCurpage; to } + Public voidSetcurpage (intcurpage) { - This. Curpage =Curpage; the } * Public intgetpagesize () { $ returnpageSize;Panax Notoginseng } - Public voidSetPageSize (intpageSize) { the This. pageSize =pageSize; + } A Public voidSettotalpages (inttotalpages) { the This. TotalPages =TotalPages; + } - $}
2. It provides a querylist action class based on the required functionality.
1 package com.action; 2 3 Import java.util.List; 4 5 Import Javax.servlet.http.HttpSession; 6 7 Import Org.apache.struts2.ServletActionContext; 8 9 Import com.bean.student;10 import com.daoimpl.daoimpl;11 import com.opensymphony.xwork2.actionsupport;12 Import COM.UTIL.PAGEUTIL;13 public class Queryall extends Actionsupport {private final static int pagesize=8;16 pri Vate Daoimpl daoimpl=new Daoimpl (); + private pageutil pageutil;18 private int rows;19 private int curp age;21 public void Setcurpage (int. curpage) {this.curpage = curpage;23}24 public String exec Ute () throws Exception {rows=daoimpl.gettotalrows (); pageutil=new pageutil (rows, pageSize); 28 if (Curpage>pageutil.gettotalpages ()) {Pageutil.setcurpage (Pageutil.gettotalpages ()); 30}31 else if (curpage>0) {pageutil.setcurpage (curpage);}else {Pageutil.setcurpage (1);}36 Notoginseng//test38 List <Student>studetnsinfo=daoimpl.querylist (Pageutil.getpagesize (), Pageutil.getcurpage ()), System.out.println (studetns Info.size ()); HttpSession session=servletactioncontext.getrequest (). getsession (); Sessio N.setattribute ("list", Studetnsinfo), Session.setattribute ("Curpage", Pageutil.getcurpage ()); 45 46 return success;47}48
The information needed at the front desk is placed in the session and is provided to the front desk for use. The control logic is automatically positioned to the first and last page when you visit the pages before and after the homepage.
Attach the code for the Querylist () method.
1 PublicList<student> Querylist (intPageSize,intcurpage) {2List<student> list=NewArraylist<>();3 Student Stu;4String sql= "SELECT * from ClassA limit" +pagesize* (curPage-1) + "," +pageSize;5 Try {6con=jdbc_connection.getconnection ();7Statement=con.createstatement ();8 9res=statement.executequery (SQL);Ten while(Res.next ()) { Onestu=NewStudent (res.getstring (1), res.getstring (2), res.getstring (3), res.getdouble (4), res.getdouble (5)); A List.add (stu); - } -}Catch(SQLException e) { the e.printstacktrace (); - } - Try { - destory (); +}Catch(Exception e) { - e.printstacktrace (); + } A returnlist; at}
3. Front page itself is not too understand, casually write the display JSP page.
1<body>2<%3 intCurpage=0;4 5 if(Session.getattribute ("Curpage")! =NULL)6Curpage= (int) Session.getattribute ("Curpage");7%>8<form action= "Queryall" method= "POST" >9<table width= "98%" align= "center" cellpadding= "1" cellspacing= "1" border= "1" >Ten<TR align= "center" > One<TD width= "5%" > Study No. </td> A<TD width= "5%" > Name </td> -<td> Academic Contributions </td> -<TD width= "5%" >scoreA</td> the<TD width= "5%" >socreB</td> -</tr> -<s:iterator var= "Stu" value= "#session. List" > -<tr> +<td>${stu.stuNo}</td> -<td>${stu.name}</td> +<td>${stu.detialInfo}</td> A<td>${stu.scoreA}</td> at<td>${stu.socreB}</td> -</tr> -</s:iterator> - -</table> - in<div align= "Right" style= "color:red;" > -<a href= "queryall?curpage=<%=curpage-1%>" > Prev </a>/ to<a href= "Queryall?curpage=<%=curpage+1%>" > Next </a> +</div> - the *</form> $ Panax Notoginseng</body>
The Java implementation of the paging function code is very coarse, all according to their own needs to define the logic function.