The front-end implementation with Ligerui implementation of pagination, feel the framework is indeed simple, idle boredom, simulation of the Liger Interface implementation of the page (as long as the function, style what disregard)
Here with the foundation of the three-tier architecture +servlet+jsp implementation, the idea is very simple, all the paging related information to a Pagebean class, the service returned to the Bean class, each page query from the bean to find information. But the details of the problem is more cumbersome, such as border processing (both front and back edge to deal with), Drop-down box jump to display the current page and so on
This is the Ligerui implementation of the paging style (implementation process my last blog has written: http://www.jb51.net/article/92850.htm)
To simulate the implementation process:
Directory structure
Database (MySQL)
model layer, a database corresponding to the model (Blog), there is a Pagebean (blogpage)
import java.sql.Date;
public class Blog {private int id;
private int category_id;
Private String title;
Private String content;
Private Date Created_time; Getter and Setter methods @Override public String toString () {return "Blog [id= + ID +", category_id= "+ category_id +", t
Itle= "+ title +", content= "+ content +", created_time= "+ Created_time +"]; }
}
public class Blogpage {private list<blog> pagerecord;//per page record private int pageno;//current page private int pagenostart;// Start index per page private int pagesize=5;//per page How much data private int totalrecord;//total record number private int totalpage;//total pages public blogpage (in
T pageno,int Totalrecord) {//pageno Totalrecord can be used as an existing information this.totalrecord=totalrecord;
Calculate Total pages totalpage= (totalrecord%pagesize==0) totalrecord/pagesize:totalrecord/pagesize+1;
The PageNo boundary treatment if (pageno<=1) this.pageno=1;
else if (pageno>=totalpage) this.pageno=totalpage;
else This.pageno=pageno;
Calculate the start index per page, the index of the first data per page, for paging queries pagenostart= (this.pageno-1) *pagesize;
public int Getpagenostart () {return pagenostart;
The public void Setpagenostart (int pagenostart) {this.pagenostart = Pagenostart;
Public list<blog> Getpagerecord () {return pagerecord;
public void Setpagerecord (list<blog> pagerecord) {This.pagerecord = Pagerecord;
public int Getpageno () {return pageno; } public VoID setpageno (int pageno) {This.pageno = PageNo;
public int getpagesize () {return pagesize;
The public void setpagesize (int pagesize) {this.pagesize = pagesize;
public int Gettotalrecord () {return totalrecord;
The public void Settotalrecord (int totalrecord) {This.totalrecord = Totalrecord;
public int gettotalpage () {return totalpage;
The public void settotalpage (int totalpage) {this.totalpage = Totalpage;
}
}
DAO layer
Jdbcutil encapsulates the connection and release operations of JDBC
public class Jdbcutil {
private static String URL = "Jdbc:mysql://localhost:3306/blogs_stu";
private static String username = "root";
private static String password = "";
static {
try {
class.forname ("Com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printstacktrace ();
}
}
public static Connection getconnection () {
Connection conn;
try {
conn= drivermanager.getconnection (URL, username, password);
return conn;
} catch (SQLException e) {
e.printstacktrace ();
}
return null;
}
public static void Release (ResultSet rs,preparedstatement ps,connection conn) {
if (rs!=null) {
try {
Rs.close ();
} catch (SQLException e) {
e.printstacktrace ();
}
}
if (ps!=null) {
try {
ps.close ();
} catch (SQLException e) {
e.printstacktrace ();
}
} if (conn!=null) {
try {
conn.close ();
} catch (SQLException e) {
e.printstacktrace ();
}
}
}
}
public class Blogdao {//per page records, incoming per page start index and per page size for paging, that is, limit two parameters (MySQL paging with limit) public list<blog> getpagerecord (int
Pagenostart, int pagesize) {Connection conn = jdbcutil.getconnection ();
PreparedStatement PS = null;
ResultSet rs = null;
String sql = "SELECT * FROM blog limit?,?";
list<blog> list = new arraylist<blog> ();
try {PS = conn.preparestatement (SQL);
Ps.setint (1, Pagenostart);
Ps.setint (2, pagesize);
rs = Ps.executequery ();
while (Rs.next ()) {Blog blog = new Blog ();
Blog.setid (Rs.getint ("id"));
BLOG.SETCATEGORY_ID (Rs.getint ("category_id"));
Blog.settitle (rs.getstring ("title"));
Blog.setcontent (rs.getstring ("content"));
Blog.setcreated_time (Rs.getdate ("Created_time"));
List.add (blog);
} return list;
catch (SQLException e) {e.printstacktrace ();
Finally {jdbcutil.release (RS, PS, conn);
return null; ///Total record number public int gettotal () {Connection conn = jdbcutil.getconnection ();
PreparedStatement PS = null;
ResultSet rs = null;
try {PS = conn.preparestatement ("SELECT count (*) from blog");
rs = Ps.executequery ();
if (Rs.next ()) {return rs.getint (1);
} catch (SQLException e) {e.printstacktrace ();
Finally {jdbcutil.release (RS, PS, conn);
return 0;
}
}
Service Layer
public class Blogservice {
Blogdao Blogdao = new Blogdao ();
Returns Pagebean, all paging required information to Pagebean find public
blogpage findpagerecord (int pageno) {
int totalrecord = Blogdao.gettotal ();
Blogpage blogpage = new Blogpage (PageNo, Totalrecord);
list<blog> list = Blogdao.getpagerecord (Blogpage.getpagenostart (), blogpage.getpagesize ());
Blogpage.setpagerecord (list);
Return Blogpage
}
}
Servlet class
@WebServlet ("/blogsplitservlet") public
class Blogsplitservlet extends HttpServlet {public
void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {
Request.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/html; Charset=utf-8 ");
String pagenostr=request.getparameter ("PageNo");
First access servletpagenostr is NULL, giving an initial value, that is, the default access to the first page
int pageno=1;
if (pagenostr!=null)
pageno=integer.parseint (PAGENOSTR);
Blogservice service=new Blogservice ();
Blogpage Blogpage=service.findpagerecord (pageno);
Request.setattribute ("Blogpage", blogpage);
Request.getrequestdispatcher ("/blogpage.jsp"). Forward (request, response);
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
doget (request, response);
}
So all the paging information is encapsulated in the Pagebean.
JSP implementation just need to take out the information in the Pagebean.
Here's my JSP implementation (mock Ligerui)
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%> <% @page import=" Java.util.*,model. Blog,model. Blogpage "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
This is the last appearance, the style is roughly adjusted, the function is the same as Ligerui default paging
Change the code in the JSP to a label (JSTL, introduce the appropriate jar package) and place the last filler in the JSP behind the servlet
Add in Servlet
Last fill blank row, if not filled, last table tr row number is not consistent with the previous
list<blog> List = Blogpage.getpagerecord ();
if (List.size () < Blogpage.getpagesize ()) {for
(int i = list.size (); I < blogpage.getpagesize (); i++)
list. Add (null);
}
Blogpage.setpagerecord (list);
JSP page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%> <% @page import=" Java.util.*,model. Blog,model. Blogpage "%> <%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix=" C "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >