2017-02-15 40 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