如何查看春季jpa中是否达到了页面大小

时间:2017-02-15 18:43:17

标签: java spring spring-boot spring-data-jpa

我在滚动上调用ajax。当html页面滚动到达窗口结束时,我使用spring jpa请求数据,但问题是在获取所有记录之后,当我向上和向下滚动时,发送ajax请求并再次重新获取数据

我该如何解决这个问题?

滚动时的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();
        }
    });
}

Spring 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()分页方法

更新:检查扩展Page接口的接口Slice只有以下方法:

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()方法。为什么呢?

1 个答案:

答案 0 :(得分:0)

如果最后一页已经加载,你的javascript会调用api直到文档结束,而不是关心。

如果正确配置了spring中的分页,则每个响应都将包含有关当前页面,pagesize,可用页面的信息。

您的客户必须评估这些内容,如果到达最后一页,请停止加载。

Spring还可以添加指向响应下一页的链接,因此您无需在客户端上计算网址。

有关spring paging here

的更多信息