利用servlet实现分页
1.创建工程和导入包
创建web工程,在WEB-INF中创建lib文件夹,和js文件夹
lib中导入:
javax.servlet.jar,
jstl.jar,
mysql-connector-java-5.1.7-bin.jar,
servlet-api.jar,
standard.jar
js中导入:
js的包
2.分包:
工程中创建包:entity,util,dao,test,service,servlet,jsp
(entity,util,dao,test,service,servlet,jsp)
3.工具类
创建BaseDao作为通用对数据库操作的方法封装工具类,再创建一个工具类(分页用)PageBean
package limit.util;
//分页的工具类
public class PageBean {
private int pageNo;//当前页
private int pageTotal;//总页数
private int countTotal;//总记录数
private int pageSize;//每页记录数
private int startRow;//起始位置
public PageBean() {
}
public PageBean(int pageNo, int countTotal, int pageSize) {
//利用三个参数来得到其他的参数
this.pageNo = pageNo;
this.pageTotal = countTotal % pageSize == 0 ? countTotal / pageSize : countTotal / pageSize + 1;
this.countTotal = countTotal;
this.pageSize = pageSize;
this.startRow = (pageNo - 1) * pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageTotal() {
return pageTotal;
}
public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}
public int getCountTotal() {
return countTotal;
}
public void setCountTotal(int countTotal) {
this.countTotal = countTotal;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
}
4.在Dao接口中定义方法,查询方法和查询记录数并实现
定义两个方法,一个用来查找总的记录数,一个用来按照分页查询得到数据
@Override
public List<User> findAll(PageBean pageBean) {
String sql = "select * from t_user limit ?,?";
Object[] params = new Object[]{pageBean.getStartRow(), pageBean.getPageSize()};
ResultSet rs = super.executeQuery(sql, params);
ArrayList<User> list = new ArrayList<>();
User user = null;
try {
while (rs.next()) {
user = new User(rs.getInt("id"), rs.getString("name"), rs.getString("pwd"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return list;
}
@Override
public int countTotal() {
String sql = "select count(*) as count from t_user";
ResultSet rs = super.executeQuery(sql, null);
int a = 0;
try {
while (rs.next()) {
a = rs.getInt("count");
}
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
5.在Servlet中添加控制
package limit.servlet;
import limit.service.UserServiceImpl;
import limit.untity.User;
import limit.util.PageBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class UserServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符集
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
//获取jsp页面的当前页数
String pageNoStr = request.getParameter("pageNo");
//设置初始值,添加判断,如果没获取到,或初次访问,就默认为1
int pageNo = 0;
if (pageNoStr != null){
pageNo = Integer.parseInt(pageNoStr);
} else {
pageNo = 1;
}
//实例化service
UserServiceImpl userService = new UserServiceImpl();
//获取总的记录数
int countTotal = userService.countTotal();
//实例化PageBean工具类
PageBean pageBean = new PageBean(pageNo, countTotal,5);
//查找分页数据
List<User> list = userService.findAll(pageBean);
//绑定值并转发
request.setAttribute("list",list);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("list.jsp").forward(request,response);
}
}
6.在list.jsp文件中添加函数逻辑
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>展示页面</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/list.js">
$(function () {
});
function firstPage() {
//给当前页设置为1
$("[name=pageNo]").val(1);
//提交
$("#userForm").submit();
}
function prevPage() {
//获取当前页数的值
var $pageNo = $("[name=pageNo]").val();
var pageNo = parseInt($pageNo);
//判断是否是第一页并提示
if (pageNo == 1){
alert("当前已经是第一页");
} else {
//不是第一页的话当前页减去1并赋值
$("[name=pageNo]").val(pageNo-1);
$("#userForm").submit();
}
}
function nextPage() {
var $pageNo = $("[name=pageNo]").val();
var $pageTotal = $("[name=pageTotal]").val();
if ($pageNo == $pageTotal){
alert("当前已经是最后一页");
} else {
$("[name=pageNo]").val($pageNo*10/10+1);
$("#userForm").submit();
}
}
function lastPage() {
var $pageTotal = $("[name=pageTotal]").val();
$("[name=pageNo]").val($pageTotal);
$("#userForm").submit();
}
</script>
</head>
<body>
<div id="base_top">
<form action="UserServlet" method="post" id="userForm">
<table border="1px">
<tr>
<td>用户名查询</td>
<td><input type="text" name="nameMsg" value="${nameMsg}"></td>
<td><input type="submit" value="提交"></td>
</tr>
<tr>
<td>用户ID</td>
<td>用户名</td>
<td>用户密码</td>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.pwd}</td>
</tr>
</c:forEach>
</table>
<div>
<input type="hidden" name="pageNo" value="${pageBean.pageNo}">
<input type="hidden" name="pageTotal" value="${pageBean.pageTotal}">
<a href="#" onclick="firstPage()">首页</a>
<a href="#" onclick="prevPage()">上一页</a>
<a href="#" onclick="nextPage()">下一页</a>
<a href="#" onclick="lastPage()">尾页</a>
</div>
</form>
</div>
</body>
</html>
最后的实现: