分享一种最简单的JSP+Servlet实现分页的方式!
旧:无分页功能的查询列表功能,仅供参考!
Servlet
try {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;List<Dept> arrayList = null;conn = DBUtil.getConncetion();ps = conn.prepareStatement("select DEPTNO,DNAME,LOC from dept");rs = ps.executeQuery();//遍历结果集while (rs.next()) {String DEPTNO = rs.getString("DEPTNO");String DNAME = rs.getString("DNAME");String LOC = rs.getString("LOC");//将以上零散的数据封装成java对象Dept dept = new Dept();dept.setDEPTNO(DEPTNO);dept.setDNAME(DNAME);dept.setLOC(LOC);//将部门对象放在集合中arrayList.add(dept);}//将集合放在请求域中request.setAttribute("deptList", arrayList);//转发request.getRequestDispatcher("/list.jsp").forward(request, response);} catch (Exception e) {e.printStackTrace();response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "数据库查询失败");}
JSP页面
<table border="1px" align="center" width="50%"><tr><th>序号</th><th>部门编号</th><th>部门名称</th><th>操作</th></tr><c:forEach items="${deptList}" var="dept" varStatus="deptus"><tr><%--deptus属性里有个count用以计数--%><th>${deptus.count}</th><th>${dept.DEPTNO}</th><th>${dept.DNAME}</th><th><a href="javascript:void(0)" onclick="del(${dept.DEPTNO})" class="a-upload"style="font-size: 10px">删除</a><%--f仅起到标记的作用--%><a href="${pageContext.request.contextPath}/dept/detail?f=edit&DEPTNO=${dept.DEPTNO}"class="a-upload" style="font-size: 10px">修改</a><a href="${pageContext.request.contextPath}/dept/detail?f=detail&DEPTNO=${dept.DEPTNO}"class="a-upload" style="font-size: 10px">详情</a></th></tr></c:forEach>
</table>
新:有分页功能的查询列表功能
Servlet
int page = 1; // 默认第一页int pageSize = 5; // 每页显示 5 条记录if (request.getParameter("page") != null) {page = Integer.parseInt(request.getParameter("page"));}try {DeptDao deptDao = new DeptDao();//实际sql:select * from dept limit ? offset ?List<Dept> deptList = deptDao.page(page, pageSize);//实际sql: select count(*) from deptint total = deptDao.getTotal();int totalPages = (int) Math.ceil((double) total / pageSize);//将集合放在请求域中request.setAttribute("deptList", deptList);request.setAttribute("currentPage", page);request.setAttribute("totalPages", totalPages);//转发request.getRequestDispatcher("/list.jsp").forward(request, response);} catch (Exception e) {e.printStackTrace();response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "数据库查询失败");}}
List<Dept> deptList = deptDao.page(page, pageSize);
public List<Dept> page(int page, int pageSize) {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;List<Dept> deptList = new ArrayList<>();try {conn = DBUtil.getConncetion();ps = conn.prepareStatement("select * from dept limit ? offset ?");ps.setInt(1, pageSize);ps.setInt(2, (page - 1) * pageSize);rs = ps.executeQuery();while (rs.next()) {Dept dept = new Dept();dept.setDEPTNO(rs.getString("DEPTNO"));dept.setLOC(rs.getString("LOC"));dept.setPEOPLE(rs.getString("PEOPLE"));dept.setDNAME(rs.getString("DNAME"));deptList.add(dept);}} catch (Exception e) {e.printStackTrace();} finally {DBUtil.close(conn, ps, rs);}return deptList;}
int total = deptDao.getTotal();
public int getTotal() {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = DBUtil.getConncetion();ps = conn.prepareStatement("select count(*) from dept");rs = ps.executeQuery();if (rs.next()) {return rs.getInt(1);}} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(conn, ps, rs);}return 0;}
JSP页面
<%--分页的样式,选加!--%>
<style>.pagination {text-align: center;margin-top: 20px;}.pagination a {margin: 0 5px;text-decoration: none;color: blue;}.pagination a.disabled {color: gray;pointer-events: none;}
</style>
<table border="1px" align="center" width="50%"><tr><th>序号</th><th>部门编号</th><th>部门名称</th><th>操作</th></tr><c:forEach items="${deptList}" var="dept" varStatus="deptus"><tr><%--deptus属性里有个count用以计数--%><th>${deptus.count}</th><th>${dept.DEPTNO}</th><th>${dept.DNAME}</th><th><a href="javascript:void(0)" onclick="del(${dept.DEPTNO})" class="a-upload"style="font-size: 10px">删除</a><%--f仅起到标记的作用--%><a href="${pageContext.request.contextPath}/dept/detail?f=edit&DEPTNO=${dept.DEPTNO}"class="a-upload" style="font-size: 10px">修改</a><a href="${pageContext.request.contextPath}/dept/detail?f=detail&DEPTNO=${dept.DEPTNO}"class="a-upload" style="font-size: 10px">详情</a></th></tr></c:forEach>
</table>
<!-- 分页 -->
<div class="pagination"><%int currentPage = (int) request.getAttribute("currentPage");int totalPages = (int) request.getAttribute("totalPages");if (currentPage > 1) {%><a href="${pageContext.request.contextPath}/dept/list?page=<%= currentPage - 1 %>">上一页</a><%} else {%><a class="disabled">上一页</a><%}if (currentPage < totalPages) {%><a href="${pageContext.request.contextPath}/dept/list?page=<%= currentPage + 1 %>">下一页</a><%} else {%><a class="disabled">下一页</a><%}%><p>当前页:<%= currentPage %> / 共 <%= totalPages %> 页</p>
</div>