Many Web sites are articles or other types of content of the paging effect, beginners for pagination development will sometimes only use code, but not in-depth understanding of code before the business logic structure, master this, I think, any effect of pagination can solve their own ...
1. Establish the internal logical structure of JSP pagination:
Now let's take a brief introduction to pagination in the standard MVC mode. First we query a lot of data in the database, listing A for loop (or directly using the tag to iterate the output <logic:interate), maybe the data is a row, in a table tag (perhaps a div nesting), anyway, Take out the data to specify how many lines, more than the paging, we click on the next page, or a page number, the request passed to the servlet (action ...). For business processing, the data that is processed after processing (that is, the page of data clicked to display) is forwarded to that view in the server:
Request.getrequestdispatcher ("xxx.jsp"). Forward (request, response);
The servlet accepts the HTTP address parameters, which are accepted by the request, and the parameters to be passed are mainly the table names of the queries, the number of pages to be obtained (more other parameters should be passed when the query is more detailed), which is used to build the SQL statement to query the contents of the page The following is the Java class that handles querying a page: Here for ease of operation, there are also two business logic methods in the bean, which should be packaged in a specialized Java class when applied in practice:
Package Com.xxx.bean;
Import java.sql.*;
Import Java.util.Vector;
Import Com.xxx.util.DB; * * * * * Sholi * 2012-7-7 * Mainly for the front display list page */public class Lists {private static Connection con=null;//declaration Connection Reference PRI vate static Statement stat=null;//declaration Statement Reference private static ResultSet rs=null;//declare ResultSet reference private static int span = 6; Span each page shows how many rows a private int curpage = 1; Current page private String msgsql;
private int totalpage;
private int total;
public int gettotal () {return total;
public void settotal (int total) {this.total = total;
public int Getcurpage () {//Get user current page return this.curpage;
public void setcurpage (int curpage) {//Log user current page this.curpage = Curpage;
public void SetSQL (String sql) {this.msgsql = SQL;
Public String GetSQL () {return this.msgsql;
public int gettotalpage () {return this.totalpage;
The public void settotalpage (int totalpage) {this.totalpage = Totalpage; public static int Getspan() {return span;
public static void Setspan (int i) {span = i;
//Get the total number of pages to be paged public static int gettotalpages (String sql) {int totalpage=1;
try{Con=db.getcon ();
Stat=con.createstatement ();
Rs=stat.executequery (SQL);
Rs.next ();
int Rows=rs.getint (1);
SYSTEM.OUT.PRINTLN (rows);
Totalpage= Rows/span;
if (rows%span!=0) {totalpage++;
}}catch (Exception e) {e.printstacktrace ();
}finally{try{Db.closecon ();
Con.close ();
}catch (Exception e) {e.printstacktrace ();
} return totalpage; }//Query The result set of the specified page for the result in the specified SQL, public static vector<string[]> getpagecontent (int page,string sql) {Vector<stri
ng[]> Vcon = new vector<string[]> ();
try{Con=db.getcon ();
Stat=con.createstatement ();
The execution statement gets the result set rs = stat.executequery (SQL);
Gets the metadata for the result set ResultSetMetaData RSMT = Rs.getmetadata ();
Get the total number of columns in the result set int count = Rsmt.getcolumncount ();
int start = (page-1) *span;
if (start!=0) {rs.absolute (start);
int temp=0;
while (Rs.next () &&temp<span) {temp++;
string[] str = new String[count];
for (int i=0;i<str.length;i++) {Str[i] = rs.getstring (i+1);
} vcon.add (str);
} catch (Exception e) {e.printstacktrace ();
}finally{try{Db.closecon ();
Rs.close ();
Stat.close ();
Con.close ();
}catch (Exception e) {e.printstacktrace ();
} return Vcon;
}
}
The above Bean holds the total number of pages of the query, the current page number, the amount of the display per page, and so on, and the other two methods are static methods that can be invoked directly when instantiating the object, where the database proxy class db is not written here.
2,the servlet processing customer service end of the query page request and Response
Approximate steps: accept parameters, instantiate list objects, construct SQL statements and query, and forward result sets to foreground display;
Here is the sample code:
String table=request.getparameter ("table");
String ftable=request.getparameter ("ftable");
int Curpage =integer.parseint (request.getparameter ("Curpage"));
Get the number of the columns String sql= "";
Gets the total number of pages of the class String sqlpage = "";
Sql= "SELECT * from" +table+ "where attribute= ' +ftable+" ' ORDER by ID DESC ";
Sqlpage = "SELECT count (*) from" +table+ "where attribute= '" +ftable+ "";
Gets the total number of pages and total bars int totalpage =lists.gettotalpages (sqlpage);
int total=prodb.getcounts (sqlpage); if (curpage==0| | Curpage>totalpage) {curpage=1;//The current page number to 1}//To save the resulting pages and current page number to the bean if the page pages do not meet the requirements Lists list=new Lists ();
Instantiate the paging class List.setcurpage (Curpage);
List.settotalpage (Totalpage);
List.settotal (total);
Get the content of the page vector<string[]> vlist=lists.getpagecontent (curpage,sql);
if (Vlist.size () ==0) {//no article String msg= search for this class of questions "No list item for this class!!!";
Request.setattribute ("msg", MSG);
} request.setattribute ("Vlist", vlist); Request.setattribute ("list", list);
Request.setattribute ("ftable", ftable);
Request.setattribute ("table", table); Request.getrequestdispatcher ("list.jsp"). Forward (request, response);
Here, the result set of the query is forwarded to the list.jsp page display: The following shows the special effects of the page paging icon for the list.
3, about the front page click Effect Display
Most of the paging effects on the site are only the previous, next, last, last, or add a submit text box to write a page number display, that is the following sample code:
<a href= "showservlet?action=listly&curpage=1&jsp=liuyan_list" class= "AAA" title= "Home" > Home </a>
<a href= "showservlet?action=listly&curpage=<%=curpage-1%>&jsp=liuyan_list" class= "AAA" Title= "Previous" > Prev </a>
<a href= "showservlet?action=listly&curpage=<%=curpage+1%>&jsp =liuyan_list " class=" AAA "title=" next Page "> next </a>
<a href=" Showservlet?action=listly&curpage =<%=totalpage%>&jsp=liuyan_list "class=" "AAA" title= "Last" > Last </a>
As for the display of page numbers, and the page numbers change as the page changes, it is necessary to deal with a more complex business logic structure, the following is a number of scripted code in the JSP page to display it:
<% if (totalpage <= 4) {for (int i = 1; I <= totalpage; i++) {if (i = = Curpage) { %> <b><u><%=i%></u>. </b> <%} else {%> <a href= "show Servlet?action=listly&curpage=<%=i%>&jsp=liuyan_list "title=" <%=i%> page "><%=i%></ a>. <%}}%> (<%=curPage%>/<%=totalpage%> page) <%} else {for (int i = Curpage I < Curpage + 4; i++) {if (curpage = = 1) {if (i = = curpage) {%> <b><u>1</u>. </b> &L t;%} else {%> <a href= "Showservlet?action=listly&curpage=<%=i%>&jsp=liuyan_list" titl e= "<%=i%> page" ><%=i%></a>. <%}} else if (Curpage > 1 && curpage < ( totalpage-1)) {if (i-1 = = curpage) {%> <b><u><%=i-1%></u>.&nbsP;</b> <%} else {%> <a href= "Showservlet?action=listly&curpage=<%=i-1%>&js P=liuyan_list "title=" <%=i-1%> page "><%=i-1%></a>. <%}} else If" (Curpage > 1 && Curpage = = totalpage-1) {if (i-2 = = curpage) {%> <b><u><%=i-2%>< ;/u>. </b> <%} else {%> <a href= "showservlet?action=listly&curpage=<%=i-2 %>&jsp=liuyan_list "title=" <%=i-2%> page "><%=i-2%></a>. <%}}" else if (cu RPage = = totalpage) {if (i-3 = = curpage) {%> <b><u><%=i-3%></u>. < /b> <%} else {%> <a href= "Showservlet?action=listly&curpage=<%=i-3%>&jsp=liuy An_list "title=" <%=i-3%> page "><%=i-3%></a>. <%}}}%> (Part <%=CURPAGE%>/<%=totalpage%> page) <%}%> Total <%=total%> strip Records </td> </tr>
Display only four icons, you can change the code to achieve more. The above code in the JSP page, violating the MVC standard pattern development, it is necessary to encapsulate them, not in detail here.
The effect chart is as follows:
Thank you for visiting.