2017-02-15 86 views
0

我在滾動調用ajax。當html頁面滾動到達窗口結束時,我使用spring jpa請求數據,但問題是在獲取所有記錄後,當我上下滾動時,發送ajax請求並再次重新提取數據。如何找出頁面大小是否已達到春季jpa

我該如何解決這個問題?

上滾動

Ajax請求:

window.onload = function(){ 
    var win = $(window); 
    // Each time the user scrolls 
    win.scroll(function() { 
     console.log('scroll reached the end. Loading new data'); 

     if (DataMixin.data.processing) 
     return false; 
     // End of the document reached? 
     if ($(document).height() - win.height() == win.scrollTop()) { 
//   $('#loading').show(); 
      DataMixin.data.processing = true; 
      DataMixin.data.size++; 
      DataMixin.get_data_page_load(); 
     } 
    }); 
} 

春天JPA:

@RequestMapping(path = "/get_data_on_page_load", method = RequestMethod.GET) 
public ResponseEntity get_data_on_page_load(@RequestParam(value="page") int page, @RequestParam(value="size") int size) throws Exception { 
    String role = "ROLE_USER"; 
    Page<TopicBean> topics = topicService.findAllByPage(new PageRequest(page, size)); 
    List<CommentLikes> likes = commentLikesService.findAll(); 
    return new ResponseEntity(new LoginDataBean(topics, role, "", likes), HttpStatus.OK); 
} 

POM:

<dependencies> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-jdbc</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.4-1203-jdbc42</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-log4j2</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-rest</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.mobile</groupId> 
     <artifactId>spring-mobile-device</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>io.jsonwebtoken</groupId> 
     <artifactId>jjwt</artifactId> 
     <version>0.7.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>javax.validation</groupId> 
     <artifactId>validation-api</artifactId> 
     <version>1.1.0.Final</version> 
    </dependency> 
</dependencies> 

無法找到分頁hasNextPage()方法

更新:檢查延伸Slice接口只有這些方法的接口Page

package org.springframework.data.domain; 

public interface Pageable { 

    public int getPageNumber(); 

    public int getPageSize(); 

    public int getOffset(); 

    public Sort getSort(); 

    public Pageable next(); 

    public Pageable previousOrFirst(); 

    public Pageable first(); 

    public boolean hasPrevious(); 
} 

我沒有看到hasNextPage()方法。爲什麼?

+0

http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/api/org/springframework/data/domain/Page.html# isLastPage()(注意:可能不是當前版本的javadoc) – 2017-02-15 18:49:31

+0

@RC。爲什麼在當前版本中沒有。它是一個有用的功能。他們肯定有理由棄用它? – kittu

+0

它仍然存在於當前版本中:http://docs.spring.io/spring-data/data-commons/docs/current/api/org/springframework/data/domain/Slice.html#isLast--(Page extends切片) – 2017-02-15 19:03:27

回答

0

如果最後一頁已經加載,您的javascript調用API直到文檔結束,不關心。

如果在春季的頁面配置正確,每個響應將包含有關當前頁面,頁面大小,可用頁面的信息。

你的客戶必須評估這些,如果到達最後一頁,請停止加載。

Spring也可以添加鏈接到響應的下一頁,所以你不需要計算客戶端的url。

更多spring paging here