【发布时间】: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