【问题标题】:How to keep the sorting order while paging in SpringSpring分页时如何保持排序顺序
【发布时间】:2016-11-22 13:45:46
【问题描述】:

我有一个使用 Spring MVC 和 Spring 数据的 Spring 应用程序。我正在尝试通过在我的控制器方法中使用 Pageable 和 Sort 参数在视图中启用分页和排序。当我按标题或日期对结果进行排序时,第一页一切正常,但是当我导航到下一页结果不再排序时,我的 mvcContext.xml 文件是

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                <property name="maxPageSize" value="3"/>
            </bean>
            <bean class="org.springframework.data.web.SortHandlerMethodArgumentResolver"/>
        </mvc:argument-resolvers>
    </mvc:annotation-driven>
    <context:component-scan base-package="com.its.stud"/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

我的jsp文件是

 <c:forEach items="${page.content}" var="topic">
    <div class="topic-box">
        <div class="from-topicbov"  >
            <div class="form-topic-title">
                <h1<a href="<spring:url value="/logged?page=0&sort=title"/>">Title</a></h1>
                <p><a href="<spring:url value="/logged?page=0&sort=date"/>">Date</a></p>
            </div>
            <div class="form-row">
                <label>
                    <p >${topic.content}</p>
                    <div class="date">
                        <p><a href="<spring:url value="/logged/${topic.id}"/>">${topic.title}</a></p>
                        <p>${topic.date}</p>
                        <p>${topic.author}</p>
                    </div>
                </label>
            </div>
            <div class="form-row">
                <button type="submit" >comment</button>
            </div>
        </div>
    </div>
    </c:forEach>

在我的控制器类中我使用这个方法:

  @RequestMapping("/logged")
    public String welcome(Model model, Pageable page, Sort sort) {
        model.addAttribute("page",topicRepository.findAll(page));
        return "topics";
    }

请指教。 编辑:存储库

public interface TopicRepository extends JpaRepository<Topic,Long> {
}

【问题讨论】:

  • 您或许还应该将排序对象传递到存储库中。
  • 我确实阅读了 spring-data/data-commons/docs 和存储库,但没有找到任何指向我的问题的内容,请您提供一个示例
  • 你的topicRepository,这是一个什么样的存储库,你不能把你的分页信息从那里传递给查询吗?

标签: java spring spring-mvc spring-data


【解决方案1】:

感谢 min 的一位同事,我确实找到了解决方案。为了保持排序顺序,我必须将排序参数传递给分页对象,所以它会保留在其他页面中。为此,必须采取两个步骤。首先,在控制器方法中,必须在模型属性中添加排序参数,并且必须传递排序属性

@RequestMapping("/logged")
public String welcome(Model model, Pageable page, Sort sort) {
    model.addAttribute("page",topicRepository.findAll(page));
    model.addAttribute("sort",(sort !=null)?sort.iterator().next().getProperty():"");
    return "welcome";
}

最后在执行分页的jsp文件中,排序参数必须附加到url

<c:forEach items="${page.content}" var="topic">
            <div class="form-topic-title">
                <h1><a href="<spring:url value="/logged?page=0&sort=title"/>">Title</a></h1>
                <p><a href="<spring:url value="/logged?page=0&sort=date"/>">Date</a></p>
            </div>
            <div class="form-row">
                <label>
                    <p >${topic.content}</p>
                    <div class="date">
                        <p><a href="<spring:url value="/logged/${topic.id}"/>">${topic.title}</a></p>
                        <p>${topic.date}</p>
                        <p>${topic.author}</p>
                    </div>
                </label>
            </div>
            <div class="form-row">
                <button type="submit" >comment</button>
            </div>
    </c:forEach>
    <a href="<spring:url value="/logged?page=${page.number - 1}&sort=${sort}"/>">Previous</a>
    <a href="<spring:url value="/logged?page=${page.number + 1}&sort=${sort}"/>">Next</a>

【讨论】:

    【解决方案2】:

    您甚至可以删除 Sort 参数,因为 Pageable 接口已经有了它。 所以控制器可能是:

    @RequestMapping("/logged")
    public String welcome(Model model, Pageable page) {
        model.addAttribute("page",topicRepository.findAll(page));
        Sort sort = page.getSort();
        model.addAttribute("sort",(sort !=null)?sort.iterator().next().getProperty():"");
        return "welcome";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2023-01-07
      • 2018-06-04
      • 2016-11-01
      • 2011-11-19
      • 2016-05-13
      • 1970-01-01
      相关资源
      最近更新 更多