问题
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.
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.
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.
Because I load my data right in the controller of the view with
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.
回答1:
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);
}
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)
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
.
Don't forget the getter and setter for LazyDataModel
and pageSize
.
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">
来源:https://stackoverflow.com/questions/54749675/jsf-primefaces-pagination-with-backend