我在Spring项目中搜索最常用的分页和排序解决方案。我找到了几个.js库,但是我已经通过将param从jsp传递到存储库并通过以下方式调用它来实现分页表:
users.jsp
<table class="table table-hover">
<thead>
<th><a href="<c:out value="?page=${pageNumber}&sort=name&order=asc" />">Imię</a></th>
<th><a href="<c:out value="?page=${pageNumber}&sort=lastname&order=asc" />">Nazwisko</a></th>
<th><a href="<c:out value="?page=${pageNumber}&sort=login&order=asc" />">Login</a></th>
<th><a href="<c:out value="?page=${pageNumber}&sort=email&order=asc" />">Email</a></th>
<th><a href="<c:out value="?page=${pageNumber}&sort=enabled&order=asc" />">Aktywny</a></th>
<th><a href="<c:out value="?page=${pageNumber}&sort=created&order=asc" />">Data utworzenia</a></th>
<th><a href="<c:out value="?page=${pageNumber}&sort=logged&order=asc" />">Ostatnio zalogowany</a></th>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.login}</td>
<td>${user.email}</td>
<td>${user.enabled}</td>
<td>${user.createDate}</td>
<td>${user.loginDate}</td>
<td><a class="btn btn-default" href="<c:url value="/users/${user.userID}/edit" />">Edytuj</a></td>
<td><a onclick="return confirm('Czy aby napewno chcesz usunąć?')" class="btn btn-danger" href="<c:url value="/users/${user.userID}/delete" />">Usuń</a></td>
</tr>
</tbody>
</c:forEach>
</table>
UserController.java
@RequestMapping
public String list(@RequestParam(value = "page", required = false) Integer pageNumber, @RequestParam(value = "sort", required = false) String sort, @RequestParam(value = "order", required = false) String order, Model model) {
Sort sorting;
pageNumber = (pageNumber == null) ? 1 : pageNumber;
if (sort == null || sort.isEmpty()) {
sorting = new Sort(new Order(Direction.ASC, "firstName"));
model.addAttribute("order", "asc");
} else {
if (order.equals("asc")) {
sorting = new Sort(new Order(Direction.ASC, sort));
model.addAttribute("order", "desc");
} else {
sorting = new Sort(new Order(Direction.DESC, sort));
model.addAttribute("order", "asc");
}
}
Page<User> page = userService.getAllUsers(pageNumber, sorting);
model.addAttribute("users", page.getContent());
return "users";
}
我的UserService.java从JpaRepository扩展:
Sort sort = new Sort(new Order(Direction.ASC, "first_name"));
PageRequest request = new PageRequest(pageNumber - 1, PAGE_SIZE, sort);
此方法有效..但如果我想对此表进行排序,则需要再传递2个参数,如排序和顺序。这一定是更好的方法,有人可以通过Spring Data和.jsp向我展示分页和排序的例子吗?