当前位置: 首页 > news >正文

JSP+Servlet实现列表分页功能

分享一种最简单的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>

http://www.lryc.cn/news/494119.html

相关文章:

  • 操作系统存储器相关习题
  • QUICK 调试camera-xml解析
  • 【linux】shell脚本编写基础
  • STM32 外设简介
  • Django-Vue3-Admin - 现代化的前后端分离权限管理系统
  • Cesium K-means自动聚合点的原理
  • Vue 项目中如何解决组件之间的循环依赖
  • 交通流量预测:基于交通流量数据建立模型
  • Hot100 - 搜索二维矩阵II
  • uart_pl011.c驱动API的zephyr测试
  • RPA:电商订单处理自动化
  • 小程序 - 个人简历
  • MySQL自启动失败(MySQL不能开机自启)解决方案_MySQL开机自启疑难杂症解决,适用Win11/Win10
  • 储存水..
  • Cmake 常用操作总结
  • Kylin Server V10 下 RocketMQ 主备自动切换模式部署
  • DevOps工程技术价值流:GitLab源码管理与提交流水线实践
  • Vue 3 中实现页面特定功能控制
  • VLC 播放的音视频数据处理流水线搭建
  • 何时在 SQL 中使用 CHAR、VARCHAR 和 VARCHAR(MAX)
  • 学习笔记043——HashMap源码学习1
  • 单点登录原理
  • 【随笔】AI大模型对软件开发的影响
  • JAVA中接口类和抽象类的区别
  • 【AI系统】昇腾 AI 架构介绍
  • uniapp input只输入一个字符就自动失去焦点
  • 定时/延时任务-ScheduledThreadPoolExecutor的使用
  • 自编码器(一)
  • Spring Cloud(Kilburn 2022.0.2版本)系列教程(五) 服务网关(SpringCloud Gateway)
  • 40分钟学 Go 语言高并发:Go程序性能优化方法论