ssm中不同的页面使用同一个分页和模糊查询类
在SSM中,可以将分页和模糊查询功能封装为一个类,然后在不同的页面中使用同一个类实例来实现分页和模糊查询的功能。以下是一个示例:
- 创建一个名为PageQuery的类,用于封装分页和模糊查询的相关参数。
public class PageQuery {
private String keyword; // 模糊查询关键字
private int pageNum; // 当前页码
private int pageSize; // 每页显示的记录数
// 省略getter和setter方法
}
- 在控制器中创建一个PageQuery的实例,并在需要分页和模糊查询的方法中使用该实例。
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/userList")
public String userList(PageQuery pageQuery, Model model) {
// 设置每页显示的记录数
pageQuery.setPageSize(10);
// 调用Service层的方法获取分页数据
PageInfo<User> pageInfo = userService.getUserList(pageQuery);
// 将分页数据传递给前端页面
model.addAttribute("pageInfo", pageInfo);
return "userList";
}
}
- 在前端页面中使用分页和模糊查询的功能。
<!-- userList.html -->
<form method="get" action="/userList">
<input type="text" name="keyword" placeholder="请输入关键字">
<button type="submit">查询</button>
</form>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<!-- 遍历分页数据 -->
<c:forEach items="${pageInfo.list}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 显示分页导航 -->
<div class="pagination">
<c:if test="${pageInfo.hasPreviousPage}">
<a href="?pageNum=${pageInfo.prePage}">上一页</a>
</c:if>
<c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="i">
<a href="?pageNum=${i}">${i}</a>
</c:forEach>
<c:if test="${pageInfo.hasNextPage}">
<a href="?pageNum=${pageInfo.nextPage}">下一页</a>
</c:if>
</div>
这样,不同的页面就可以使用同一个分页和模糊查询类来实现分页和模糊查询的功能了。通过设置PageQuery类的属性,可以在不同的页面中传递不同的参数进行分页和模糊查询。
原文地址: https://www.cveoy.top/t/topic/i92U 著作权归作者所有。请勿转载和采集!