分页显示对于数据在页面中的展示是非常重要的工具,当数据条数较多时,就需要使用分页来显示。实现分页显示的思路包括:(1)确定数据库中一共有多少条数据,即数据总数;(2)设定每页准备显示多少条数据,计算出一共需要分多少页,即分页数目=数据总数/每页数目,由于用到除法,当数据总数能被每页数目整除时,那就正好那么多页;当不能整除时就需要在分页数目上加上一页才行;(3)有了数据总数,每页数据条数,分页数目后,开始使用数据库查询出每页的数据,然后传递给Servlet处理,然后在视图层显示。(4)其中的数据总数,通过数据库查询获得,每页显示数据条数,需要直接给定值,获取每页的数据,使用sql语句的"select * from user order by ID desc limit 每页数目*当前页数,每页数目", 包括两个变量:每页数目,这是可以直接赋值的,当前页数,则需要从前端jsp页面获取才行。因此要实现分页显示,正好将数据库、servlet、jsp页面三个层次都用到。
下面进行一个实例,说明分页显示实现过程:
(1)先保证数据库里的表数据条目较多,比如本案例中学生用户表一共8个用户数据,每页显示3条,因此这里就需要3页显示。
每页数目=3;数据总数则需要查询获取。
(2)新建两个实体:User和Page,User用于学生对象,Page用于分页对象。
package org.student.entity;
public class User {
private int ID;
private String Name;
private String Sex;
public User(int iD) {
this.ID = iD;
}
public User(int iD, String name, String sex) {
this.ID = iD;
this.Name = name;
this.Sex = sex;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
}
package org.student.entity;
public class Page {
private int pageSize;
private int totalPage;
private int Index;
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getIndex() {
return Index;
}
public void setIndex(int index) {
this.Index = index;
}
public Page() {
super();
}
public Page(int Index, int pageSize) {
this.Index=Index;
this.pageSize=pageSize;
}
}
(3)根据需求编写学生用户、分页接口及其实现类,习惯上接口命名为IName,实现类命名为NameImpl
分页接口:
package org.student.dao;
public interface IpageDao {
public int getTotalPage(int pageSize);
}
分页实现类:
package org.student.dao;
import java.sql.SQLException;
public class PageDaoImpl implements IpageDao {
@Override
public int getTotalPage(int pageSize) { //总共多少页数
DButil db=new DButil();
int total=0,totalPage=0;
String sql="select count(ID) from user";
try {
total=db.getTotalCount(sql); //总数
totalPage=total%pageSize>0?total/pageSize+1:total/pageSize; //总页数
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //总数量
return totalPage;
}
}
用户接口类:
package org.student.dao;
import java.util.List;
import org.student.entity.Page;
import org.student.entity.User;
public interface IUser {
public List<User> queryUserByPage(Page page);
}
用户实现类,实现分页查询获得每页的数据,关键参数给定就是当前页数和每页数目:
package org.student.dao;
import java.sql.*;
import java.util.*;
import org.student.entity.Page;
import org.student.entity.User;
public class UserDaoImpl implements IUser {
public Connection con=null;
public PreparedStatement pst=null;
public Statement sm=null;
public ResultSet rs=null;
@Override
public List<User> queryUserByPage(Page page) throws SQLException {
List<User> arr=new ArrayList();
DButil db=new DButil(); //实例化数据库db
try {
con=db.initConnection(); //连接数据库
String sql1="select * from user limit ?,? "; //分页查询
pst=con.prepareStatement(sql1);
pst.setInt(1, page.getIndex()*page.getPageSize()); //当前页*每页数目
pst.setInt(2, page.getPageSize());//每页数目
rs=pst.executeQuery();
while(rs.next()) {
User stu=new User(rs.getInt(1),rs.getString(2),rs.getString(3));//查询数据存到user对象
arr.add(stu);//存成List集合
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.close();//关闭数据库
return arr;
}
}
(4)上述用户分页查询实现类中参数每页数目可以直接赋值,而当前页数则需要前端页面获取,如当前页为1,当前页为2等。在实现的时候在jsp页面使用javabean封装,页面列表下方呈现分页标记:首页 前一页 后一页 末页。每一组文字均为超链接a,首页<a href="?start=0">,使用?传值,start为起始页数变量,index为0表示第一页,末页<a href="?start=TotalPage-1>",TotalPage为总页数,在page实现类中传值过来。如下为前端jsp页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" import="org.student.entity.User" import="org.student.entity.Page"%>
<jsp:useBean id="user" scope="page" class="org.student.dao.UserDaoImpl"/>
<jsp:useBean id="pg" scope="page" class="org.student.dao.PageDaoImpl"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>所有用户列表</h4>
<table border="1" width="60%">
<tr>
<td>用户姓名</td>
<td>用户性别</td>
<td>操作</td>
</tr>
<%
request.setCharacterEncoding("UTF-8");
//获取当前页数,首次进入时start为0,点击超链接时获取start页数
int start=request.getParameter("start")==null?0:Integer.parseInt(request.getParameter("start"));
//通过page实现类获得总页数,这里的3为每页数目
int pageSize=3;
int totalPage=pg.getTotalPage(pageSize);
//前一页页数、后一页变量值
int prePage=start-1>0?start-1:start+1;
int nextPage=start+1<totalPage?start+1:totalPage-1;
//使用request.setAttribute方法便于页面中使用el语法
request.setAttribute("totalPage", totalPage);
request.setAttribute("prePage", prePage);
request.setAttribute("nextPage", nextPage);
//获得当前页的数据
Page pg1=new Page(start,pageSize);
List<User> currentUser=(List<User>)user.queryUserByPage(pg1);
//遍历显示
for(User userlist:currentUser){
%>
<tr>
<td><%=userlist.getName() %></td>
<td><%=userlist.getSex() %></td>
<td><a href="deleteStudent.jsp?ID=<%=userlist.getID() %>">删除</a><br><a href="updateStudent.jsp?ID=<%=userlist.getID() %>">修改</a></td>
</tr>
<% } %>
</table>
<nav>
<ul class="pagination">
<li>
<a href="?start=0">
<span>首页</span>
</a>
</li>
<li>
<a href="?start=${requestScope.prePage }">
<span>前一页</span>
</a>
</li>
<li>
<a href="?start=${requestScope.nextPage }">
<span>后一页</span>
</a>
</li>
<li>
<a href="?start=${requestScope.totalPage-1} ">
<span>尾页</span>
</a>
</li>
</ul>
</nav>
</body>
</html>
上述页面中头部有不少java代码,从MVC角度考虑如果需要将页面不含java代码,可以考虑增加一个servlet服务层,将这些java代码放到servlet服务层中,然后再来进行页面跳转。如下为userPageServlet代码:
@WebServlet("/userPageServlet" )
public class userPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("welcome");
//获取当前页数,首次进入时start为0,点击超链接时获取start页数
int start=request.getParameter("start")==null?0:Integer.parseInt(request.getParameter("start"));
//通过page实现类获得总页数,这里的3为每页数目
int pageSize=3;
PageDaoImpl pg=new PageDaoImpl();
int totalPage=pg.getTotalPage(pageSize);
//前一页页数、后一页变量值
int prePage=start-1>0?start-1:start+1;
int nextPage=start+1<totalPage?start+1:totalPage-1;
//使用request.setAttribute方法便于页面中使用el语法
request.setAttribute("totalPage", totalPage);
request.setAttribute("prePage", prePage);
request.setAttribute("nextPage", nextPage);
//获得当前页的数据
Page pg1=new Page(start,pageSize);
UserDaoImpl user=new UserDaoImpl();
try {
List<User> currentUser=(List<User>)user.queryUserByPage(pg1);
request.setAttribute("userList", currentUser);
request.getRequestDispatcher("userPage.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后在userPage.jsp页面中只需要request.getAttributes获取到数据,即可。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" import="org.student.entity.User" import="org.student.entity.Page"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>所有用户列表</h4>
<table border="1" width="60%">
<tr>
<td>用户姓名</td>
<td>用户性别</td>
<td>操作</td>
</tr>
<%
request.setCharacterEncoding("UTF-8");
List<User> currentUser=(List<User>)request.getAttribute("userList");
//遍历显示
for(User userlist:currentUser){
%>
<tr>
<td><%=userlist.getName() %></td>
<td><%=userlist.getSex() %></td>
<td><a href="deleteStudent.jsp?ID=<%=userlist.getID() %>">删除</a><br><a href="updateStudent.jsp?ID=<%=userlist.getID() %>">修改</a></td>
</tr>
<% } %>
</table>
<nav>
<ul class="pagination">
<li>
<a href="userPageServlet?start=0">
<span>首页</span>
</a>
</li>
<li>
<a href="userPageServlet?start=${requestScope.prePage }">
<span>前一页</span>
</a>
</li>
<li>
<a href="userPageServlet?start=${requestScope.nextPage }">
<span>后一页</span>
</a>
</li>
<li>
<a href="userPageServlet?start=${requestScope.totalPage-1} ">
<span>尾页</span>
</a>
</li>
</ul>
</nav>
</body>
</html>