JavaWeb学习-分页功能
1. 获取指定页码上的库存列表信息,每页显示5条
(1)FruitDAO和FruitDAOImpl
//获取指定页码上的库存列表信息,每页显示5条
List<Fruit> getFruitListbypageNo(int pageNo);
public List<Fruit> getFruitListbypageNo(int pageNo) {
List<Fruit> list = new ArrayList<>();
getConn();
String sql = "select * from fruit LIMIT ?,5";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,(pageNo-1)*5);
rs = ps.executeQuery();
while (rs.next()){
Fruit fruit = new Fruit();
fruit.setFid(rs.getInt(1));
fruit.setFname(rs.getString(2));
fruit.setPrice(rs.getInt(3));
fruit.setFcount(rs.getInt(4));
fruit.setRemark(rs.getString(5));
list.add(fruit);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
注:实体类必须写在while(rs.next())里面,否则最后一条数据会覆盖之前已保存的数据。
(2)IndexServlet
FruitDAO fruitDAO=new FruitDAOImpl();
//List<Fruit> fruitList = fruitDAO.getFruitList();
List<Fruit> fruitList = fruitDAO.getFruitListbypageNo(1); //获取第一页的内容
session.setAttribute("fruitList", fruitList);
2.实现分页功能
(1)index.html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<script language="JavaScript" src="js/index.js"></script>
</head>
<body>
<div >
<div style="align-content: center;margin-left: 30%">
<div>
<a th:href="@{/add.html}">添加信息</a>
</div>
<table border="1" >
<tr>
<th >名称</th>
<th >单价</th>
<th >库存</th>
<th >备注</th>
<th >操作</th>
</tr>
<tr th:if="${#lists.isEmpty(session.fruitList)}">
<td colspan="4">对不起,货存为空!</td>
</tr>
<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}">
<!-- <td ><a th:text="${fruit.fname}" href="@{'/edit.do?fid='+${fruit.fid}}">苹果</a></td>
( th:text="" 会把后面的值覆盖,添加超链接时候,为防止超链接被覆盖,将“th:text=”放入<a>标签中
@{}表示thymeleaf中的绝对路径 ${}表示需要thymeleaf来解析 而“/edit.do?fid=”是字符串不需要thymeleaf去解析,添加单引号) -->
<!--
给url地址后面附加请求参数:
@{/order/process(exexId=${exexId},exexType="FAST")} (键值对)
-->
<td><a th:text="${fruit.fname}" th:href="@{'/edit.do?fid='+${fruit.fid}}">苹果 </a></td>
<td th:text="${fruit.price}">5</td>
<td th:text="${fruit.fcount}">20</td>
<td th:text="${fruit.remark}"> </td>
<!-- <td> <img src="img/del.png" th:onclick="'delFruit('+${fruit.fid}+')"></td> -->
<td> <img src="img/del.png" style="height: 50px;width: 50px" th:onclick="|delFruit(${fruit.fid})|"></td>
</tr>
</table>
<div style="margin-left: 1%">
<input type="button" value="首 页" th:onclick="|page(1)|" th:disabled="${session.pageNo == 1}"/>
<input type="button" value="上一页" th:onclick="|page(${session.pageNo - 1})|" th:disabled="${session.pageNo == 1}"/>
<input type="button" value="下一页" th:onclick="|page(${session.pageNo + 1})|" th:disabled="${session.pageNo == session.pageCount}"/>
<input type="button" value="尾 页" th:onclick="|page(${session.pageCount})|" th:disabled="${session.pageNo == session.pageCount}"/>
</div>
</div>
</div>
</body>
</html>
//th:disabled="${session.pageNo == 1}"表示在在。。。条件下该按钮不能使用
(2)index.js
function page(pageNo) {
window.location.href="index?pageNo="+pageNo;
}
(3)FruitDAO和FruitDAOImpl
//获取库存总页数
int getFruitCount();
public int getFruitCount() {
int count = 0;
String sql = "select * from fruit ";
getConn();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()){
count++; //获取rs中的条数
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!