Spring Boot和胸腺分页和分类

7vux5j2d  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(235)

我正在研究Spring Boot 和宠物项目,现在我需要做分页和排序。我在html页面中使用thymeleaf,我在pageable和sort字段方面有一些问题。
我有控制器

@GetMapping
    public String showAllClassrooms(Model model, Pageable page) {
        Page<Classroom> classroomPage = classroomService.getAndSort(page);  
        model.addAttribute("classroomPage", classroomPage);
        return "classrooms-page";
    }

服务方式

@Transactional
    public Page<Classroom> getAndSort(Pageable paging) {
        long totalElement = count();
        List<Classroom> classrooms = new ArrayList<>();
        if (paging.getSort().toString().contains("Address")) {
            classrooms = getAndSortByAddress(paging.getPageSize(), (int) paging.getOffset());
        } else {
            classrooms = getAndSortByCapacity(paging.getPageSize(), (int) paging.getOffset());
        }
        return new PageImpl<Classroom>(classrooms, paging, totalElement);
    }

和导航页面

<nav aria-label="..." th:object="${classroomPage}">
                <ul class="pagination  justify-content-center">
                    <li class="page-item" th:classappend="*{(getNumber == 0 ? 'disabled' : '' )}">
                        <a class="page-link" th:href="@{/classrooms/(page=*{getNumber-1}, size = *{size}, sort=*{sort})}">&laquo;</a></li>
                    <li class="page-item"  th:classappend="*{(getNumber == getNumber ? 'active' : '' )}"> 
                        <a class="page-link" th:text="*{getNumber} + 1"  th:href="@{/classrooms/(page=*{getNumber}, sort=*{sort})}" ></a></li>
                    <li class="page-item"  th:classappend="(*{getNumber} == *{getTotalPages}-1 ? 'disabled' : '' )">
                        <a class="page-link" th:href="@{/classrooms/(page=*{getNumber+1} , size = *{size} , sort=*{sort})}">&raquo;</a>
                    </li>
                     <li class="page-item">
                        <form action="#" method="get" th:action="@{/classrooms/}"  
                            >
                            <div class="input-group">
                                 <input class="w-25 p-1" type="text" name="page" th:value="${classroomPage.getNumber+1}">
                                 <input type="hidden" name="size"  th:value="${classroomPage.size}">
                                 <input type="hidden" name="sort"  th:value="${classroomPage.getSort}">
                                 <span class="input-group-text" th:utext="${classroomPage.getNumber + 1}" ></span>

                            </div>
                        </form>
                    </li>       

                </ul>
            </nav>

一切正常,但我在浏览器的地址字段有双向,像这样

http://localhost:8080/classrooms/?page=2&size=5&sort=Capacity:%20ASC:%20ASC:%20ASC:%20ASC:%20ASC:%20ASC:%20ASC:%20ASC

感谢您抽出时间,也为我的英语不好感到抱歉:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题