Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据-最常见的分页和排序表解决方案_Spring_Sorting_Paging - Fatal编程技术网

Spring数据-最常见的分页和排序表解决方案

Spring数据-最常见的分页和排序表解决方案,spring,sorting,paging,Spring,Sorting,Paging,我正在寻找Spring项目中最常用的分页和排序解决方案。我找到了两个.js库,但通过将param从jsp传递到存储库并通过以下方式调用它,我实现了分页表: users.jsp <table class="table table-hover"> <thead> <th><a href="<c:out value="?page=${pageNumber}&sort=name&order

我正在寻找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>

这个方法有效。。但如果我想对这个表进行排序,则需要再传递两个参数,如sort和order。这一定是更好的方法,有人可以向我展示通过Spring数据和.jsp进行分页和排序的示例?

查看
PagingAndSortingRepository
和示例4如果您希望对某些内容进行分页和排序,我不知道如果不传递参数,告诉我使用哪种排序标准,哪个方向,你要哪一页。有什么不好或奇怪的地方吗?@JB:不好,但你能告诉我一种优雅的方法吗。我以前的做法是:从.jsp传递两个参数,在控制器中获取(必要时切换顺序),将它们传递到服务方法,其中包含在PageRequest中,但我认为这远远不是好的编程实践,因为当我尝试实现搜索方法时,它在其他控制器方法中生成了许多重复的代码。请访问codereview.stackexchange.com,在那里发布您的代码,并要求改进。这个问题中没有足够的代码来制定关于SO或CR的答案
   @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";
    }
Sort sort = new Sort(new Order(Direction.ASC, "first_name"));
PageRequest request = new PageRequest(pageNumber - 1, PAGE_SIZE, sort);