A Sdi A Sdi - 12 days ago 7
Java Question

How to retain the sorting order while paging in Spring

I have a spring application which utilize Spring MVC and Spring data. I am trying to enable paging and sorting in a view by using Pageable and Sort argument in my controller methods. Everything works fine for first page when I sort the results by title or date, but when I navigate to next page results are not ordered anymore my

mvcContext.xml
file is

<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>


and my jsp file is

<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>


and in my controller class I use this method:

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


Please advice.
EDIT: the repository

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

Answer

Thanks to a colleague of min I did find the solution. In order to maintain the sorting order, I have to pass the sort parameter to paging object, so it will be retained in other pages. To do so two steps must be taken. First, in the controller method, a sort parameter must be added to model attribute and it must pass the sort property

@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";
}

and finally in the jsp file, where the paging is performed, the sort parameter must be appended to the 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>