分页步骤分析:
@分页:
1. 后台需要查询出总条数,根据规定的每页条数来确定一共多少页
2. 显示页面在需要拿到数据的list,当前页码,功能有上一页,下一页,首页,尾页,所以还需要总页数
3. 后台根据前端页面传回来的页码和每页条数,来查询对应的数据
因为limit要确定从第几条查到第几条,这两个数据可以根据页码和每页条数来确定
4. 此处需要先拿页码和每页条数,先设默认值,若可以拿到再修改
5. 此时根据这两个值已经可以查出对应页的数据->list
6. 查出所有总条数。(根据目前所拥有的三个数值可以计算其他有用数值)
7. 定义一个页面信息javabean:传入目前已知的list和其他三个数
8. 在其中可以定义并计算出总页数,上一页,下一页等数据,
9. 封装好后,将此javabean对象一同发给前端页面解析。
10.前端页面根据指令带数据再次访问此servlet。
- servlet:
@WebServlet("/showPageEmp")
public class ShowPageEmp extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pageNumStr = req.getParameter("pageNum");
String pageSizeStr = req.getParameter("pageSize");
//当前页
int pageNum = 1;
//每页条数
int pageSize = 5;
try {
pageNum = Integer.parseInt(pageNumStr);
pageSize = Integer.parseInt(pageSizeStr);
} catch (Exception e) {
}
EmpDao dao = new EmpDaoImpl();
//数据总条数
int count = dao.count();
//对应页的数据
List<Emp> list = dao.showPage(pageNum, pageSize);
Page page = new Page(pageNum, list, count, pageSize);
req.setAttribute("page", page);
req.getRequestDispatcher("/pageShow.jsp").forward(req, resp);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- jsp页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SHOW ALL</title>
</head>
<body>
<p>${date}</p>
<table border="1">
<tr>
<td>empno</td>
<td>ename</td>
<td>job</td>
<td>mgr</td>
<td>hiredate</td>
<td>sal</td>
<td>COMM</td>
<td>deptno</td>
<td>dname</td>
<td>loc</td>
<td>操作</td>
</tr>
<c:forEach items="${page.list}" var="emp">
<tr>
<td>${emp.empno}</td>
<td>${emp.ename }</td>
<td>${emp.job}</td>
<td>${emp.mgr }</td>
<td>${emp.hiredate }</td>
<td>${emp.sal }</td>
<td>${emp.COMM }</td>
<td>${emp.deptno }</td>
<td>${emp.dname }</td>
<td>${emp.loc }</td>
<td>
<a href="<%=request.getContextPath() %>/delByEmpno?id=${emp.empno }">删除</a>
<a href="<%=request.getContextPath() %>/showByEmpno?id=${emp.empno }">修改</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="11">
<a href="<%=request.getContextPath() %>/showPageEmp?pageNum=1&pageSize=5">首页</a>
<a href="<%=request.getContextPath() %>/showPageEmp?pageNum=${page.prePage}&pageSize=5">上一页</a>
<a href="<%=request.getContextPath() %>/showPageEmp?pageNum=${page.nextPage}&pageSize=5">下一页</a>
<a href="<%=request.getContextPath() %>/showPageEmp?pageNum=${page.totalpage}&pageSize=5">尾页</a>
</td>
</tr>
<tr>
<td colspan="11">当前第${page.pageNum}页,共${page.totalpage}页</td>
</tr>
</table>
<form action="<%=request.getContextPath() %>/insert" method="post"
accept-charset="UTF-8">
<table>
<tr>
<td><a href="<%=request.getContextPath() %>/showMgrAndDept">新增</a></td>
</tr>
</table>
</form>
</body>
</html>
- javabean:
import java.util.List;
public class Page {
int pageNum;// 当前页
List<?> list;
int prePage;// 上一页
int nextPage;// 下一页
int totalpage;// 总页数
int count;// 总条数
int pageSize;
String pageSizeStr;
String pageNumStr;
public Page(int pageNum, List<?> list, int count, int pageSize) {
this.pageNum = pageNum;
this.list = list;
this.count = count;
this.pageSize = pageSize;
// 总页数
totalpage = (count - 1) / pageSize + 1;
// 上一页
prePage = this.pageNum - 1;
if (this.pageNum == 1) {
prePage = this.pageNum;
}
// 下一页
nextPage = this.pageNum + 1;
if (this.pageNum == totalpage) {
nextPage = totalpage;
}
}
public int getPageNum() {
return pageNum;
}
public List<?> getList() {
return list;
}
public int getPrePage() {
return prePage;
}
public int getNextPage() {
return nextPage;
}
public int getTotalpage() {
return totalpage;
}
public int getCount() {
return count;
}
public int getPageSize() {
return pageSize;
}
}
- 查询方法:
@Override
public List<Emp> showPage(int pageNum, int pageSize) {
String sql = "Select * from emp natural join dept order by empno asc limit ?,?";
Connection coon = JDBCUtils.getCoon();
PreparedStatement ps =null;
ResultSet rs = null;
try {
ps = coon.prepareStatement(sql);
ps.setInt(1, (pageNum-1)*pageSize);
ps.setInt(2, pageSize);
rs = ps.executeQuery();
List<Emp> list = new ArrayList<Emp>();
while (rs.next()) {
Emp emp = new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getInt("mgr"));
emp.setHiredate(rs.getDate("hiredate"));
emp.setSal(rs.getBigDecimal("sal"));
emp.setCOMM(rs.getBigDecimal("COMM"));
emp.setDeptno(rs.getInt("deptno"));
emp.setDname(rs.getString("dname"));
emp.setLoc(rs.getString("loc"));
list.add(emp);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询sql语句执行失败");
}finally {
JDBCUtils.closeCoon(coon, ps, rs);
}
}