2016-11-22 24 views
3

Spring MVCとSpringデータを利用するSpringアプリケーションがあります。私は、コントローラメソッドでPageableとSort引数を使用して、ビュー内でページングとソートを有効にしようとしています。すべては私がタイトルや日付によって結果をソートする場合、最初のページのために正常に動作しますが、私は、次のページの結果に移動したときにはもう注文した私のmvcContext.xmlファイルがSpringでページング中にソート順を保持する方法

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

してくださいアドバイス:

は、私のコントローラクラスで、私は、このメソッドを使用します。 EDIT:

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

また、ソートオブジェクトをリポジトリに渡すべきでしょう。 – Vaelyr

+0

私はspring-data/data-commons/docsとリポジトリを読み込み、問題を指摘していませんでした。例を挙げてください。 –

+0

あなたの 'topicRepository'は、どのようなリポジトリですか?そこからあなたのページング情報をクエリに渡しますか? – Vaelyr

答えて

1

おかげで、私は解決策を見つけた分の同僚にリポジトリ。並べ替え順を維持するためには、sortパラメータをページングオブジェクトに渡す必要があるため、他のページにも保持されます。これを行うには、2つの手順を実行する必要があります。まず、制御方法では、ソート・パラメータが属性をモデルに追加する必要があり、それは

@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> 
関連する問題