带有后端的JSF Primefaces分页 [英] JSF Primefaces Pagination with Backend

查看:62
本文介绍了带有后端的JSF Primefaces分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有点奇怪,但是我不知道如何将PrimeFaces的分页功能传到我的后端.我知道这里有展示柜,这里有多个答案,但是我在质疑它是否真的必须是这样.另外,我的项目没有DAO和事物的结构,这使我陷入了黑暗.

Maybe it's a bit weird, but I don't know how to get the pagination of PrimeFaces to my backend. I know there is the showcase and there are multiple answers here but I'm questioning if it really has to be like this. Additionally my project is not structured with DAO and things, which leaves me in the dark.

我使用Spring Boot配置.整个项目的基础是教程 https://dzone.com/articles/developing-jsf-applications-with-spring-boot

I use a Spring Boot config. The grounding of the whole project is that tutorial https://dzone.com/articles/developing-jsf-applications-with-spring-boot

但是现在它更大了,拥有一个真正的数据库以及类似的东西.我已经实现了搜索,可以使用规范"来查找对象.在那里,我还可以为搜索提供可分页的对象.

But now it's much bigger, has a real database and such things. I already implemented a search where I use Specifications to find objects. There I can also give the search a pageable object.

我以为我可以为正常显示数据表做一个可分页的对象,并以某种方式将其连接到Primefaces的按钮上.

In my head I thought I can do a pageable object for the normal showing datatable and somehow connect it to the buttons of Primefaces.

因为我使用

public void loadData() {
        eintraege = telefonbuchRepository.findAll();
}

所以我在这里不知道如何使用延迟加载过程.如果我错了,也请教我,其他地方已经可以解决这个问题.

So I don't know how to use the lazy loading process in here. Please also teach me if I'm wrong and somewhere else are already answers for this.

推荐答案

我解决了.所以我刚刚实现了LazyDataModel

I solved it. So I just implemented LazyDataModel

public class TelefonbuchListController extends LazyDataModel<Telefonbuch> {

    private LazyDataModel<Telefonbuch> lazyModel;
    private static final long serialVersionUID = 1L;
    private int pageSize = 5;

    public void loadData() {
        lazyModel = new LazyDataModel<Telefonbuch>() {

            private static final long serialVersionUID = 1L;

            @Override
            public List<Telefonbuch> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                    Map<String, Object> filters) {

                List<Telefonbuch> result = new ArrayList<Telefonbuch>();

                if (first == 0) {
                    result = telefonbuchRepository.findAll(PageRequest.of(first, pageSize)).getContent();
                } else {
                    first = first / pageSize;
                    result = telefonbuchRepository.findAll(PageRequest.of(first, pageSize)).getContent();
                }

                return result;
            }
        };

        lazyModel.setRowCount((int) telefonbuchRepository.count());
        lazyModel.setPageSize(pageSize);
    }

您应该选择int pageSize,例如您的rowsPerPageTemplate中的一个.因为我正在使用JpaRepository,所以我想利用Specification和Pageable对象的机会.Pageable对象的形式为 PageRequest.of(int page,int size)

You should pick the int pageSize like one number of your rowsPerPageTemplate. Because I'm working with the JpaRepository I wanted to use the chance of Specifications and Pageable objects. The form of a Pageable object is PageRequest.of(int page, int size)

所以我需要获取页码而不是此页的第一页.只是一个简单的if语句就可以帮助我确定它是否是第一页.我得到 first/pageSize 的页码.

So I needed to get the pagenumber instead of the first of this page. Just a simple if statement helped me to decide if it's the first page or not. I get the pagenumber with first / pageSize.

别忘了 LazyDataModel pageSize 的获取器和设置器.

Don't forget the getter and setter for LazyDataModel and pageSize.

我的.xhtml数据表如下:

My .xhtml datatable looks like this:

<p:dataTable id="table" var="telefonbuch" lazy="true" widgetVar="tableWv" value="#{telefonbuchList.lazyModel}" style="margin-bottom:20px" paginator="true" rows="#{telefonbuchList.pageSize}" dynamic="true" 
                paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink} {Exporters}"
                rowsPerPageTemplate="5,8,10">

这篇关于带有后端的JSF Primefaces分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆