使用带规范的分页
Using Pagination with Specifications
我对分页完全陌生,什么都不知道。从未使用过它。有人告诉我使用 Specifications 来使用分页之类的东西既简单又好。现在我成功地实现了我的规范,但我不知道分页是如何工作的,也不知道我将如何把它送到我的前端。
通常我会得到一个显示数据库中所有内容的列表。但是当我使用分页对象时,我需要一个页面,所以我将列表更改为页面。
到目前为止我尝试了什么:
搜索控制器:
public class SucheController {
@Autowired
private TelefonbuchRepository telefonbuchRepository;
private Page<Telefonbuch> eintraege;
private Telefonbuch telefonbuch = new Telefonbuch();
public void search(String vorname, String nachname, String telefonnummer, String handynummer) {
if (!vorname.isEmpty()) {
eintraege = telefonbuchRepository.findAll(TelefonbuchSpecifications.hasVorname(vorname), PageRequest.of(0, 5));
}
我没有找到任何教程,所以我只是试图传递我的规范和一个分页元素。当我搜索错误是:
javax.el.PropertyNotFoundException: Property [id] not found on type [org.springframework.data.domain.PageImpl]
如何正确操作?你知道这方面的任何教程或网站吗?我什至不知道我是否可以在前端显示页面对象。我将 JSF 与 xhtml 文件一起使用。如果您需要更多信息,请告诉我。
编辑:
suche.xhtml 根据要求:
<p:dataTable id="table" var="telefonbuch" value="#{sucheController.eintraege}" stickyHeader="true" resizableColumns="true" liveResize="true" style="margin-bottom:20px" paginator="true" rows="10" emptyMessage="Keine Telefonbucheinträge vorhanden" selection="#{telefonbuchList.selectedEntry}" selectionMode="single" rowKey="#{telefonbuch.id}"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="10,20,30">
我现在在第一行末尾看到 telefonbuch.id。
您通过确保存储库扩展 JpaSpecificationExecutor
并调用 findAll(Specification, Pageable)
方法
正确实施了规范
但是,此方法 returns 一个 Page<TelefonBuch>
对象,您已将其分配给名为 eintraege
的字段。从您发布的异常来看,您似乎正试图在 eintraege
之上直接获取一个名为 id
的 属性,所以我假设您有类似 ${eintraege.id}
的内容在你的 JSF 代码中,那是行不通的,因为它没有 id
属性.
您可能想要做的是遍历 eintraege
中的所有对象,并显示每个对象的 ID。这意味着你可能需要做这样的事情:
<c:forEach var="telefonbuch" items="#{eintraege.content}">
<h:outputText value="#{telefonbuch.id}" />
</c:forEach>
这里发生的是我们遍历 eintraege.getContent()
,returns 一个可以迭代的集合。然后你应该将每个对象分配给一个变量(例如telefonbuch
),然后你可以调用像#{telefonbuch.id}
.
这样的属性
我对分页完全陌生,什么都不知道。从未使用过它。有人告诉我使用 Specifications 来使用分页之类的东西既简单又好。现在我成功地实现了我的规范,但我不知道分页是如何工作的,也不知道我将如何把它送到我的前端。
通常我会得到一个显示数据库中所有内容的列表。但是当我使用分页对象时,我需要一个页面,所以我将列表更改为页面。
到目前为止我尝试了什么:
搜索控制器:
public class SucheController {
@Autowired
private TelefonbuchRepository telefonbuchRepository;
private Page<Telefonbuch> eintraege;
private Telefonbuch telefonbuch = new Telefonbuch();
public void search(String vorname, String nachname, String telefonnummer, String handynummer) {
if (!vorname.isEmpty()) {
eintraege = telefonbuchRepository.findAll(TelefonbuchSpecifications.hasVorname(vorname), PageRequest.of(0, 5));
}
我没有找到任何教程,所以我只是试图传递我的规范和一个分页元素。当我搜索错误是:
javax.el.PropertyNotFoundException: Property [id] not found on type [org.springframework.data.domain.PageImpl]
如何正确操作?你知道这方面的任何教程或网站吗?我什至不知道我是否可以在前端显示页面对象。我将 JSF 与 xhtml 文件一起使用。如果您需要更多信息,请告诉我。
编辑: suche.xhtml 根据要求:
<p:dataTable id="table" var="telefonbuch" value="#{sucheController.eintraege}" stickyHeader="true" resizableColumns="true" liveResize="true" style="margin-bottom:20px" paginator="true" rows="10" emptyMessage="Keine Telefonbucheinträge vorhanden" selection="#{telefonbuchList.selectedEntry}" selectionMode="single" rowKey="#{telefonbuch.id}"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="10,20,30">
我现在在第一行末尾看到 telefonbuch.id。
您通过确保存储库扩展 JpaSpecificationExecutor
并调用 findAll(Specification, Pageable)
方法
但是,此方法 returns 一个 Page<TelefonBuch>
对象,您已将其分配给名为 eintraege
的字段。从您发布的异常来看,您似乎正试图在 eintraege
之上直接获取一个名为 id
的 属性,所以我假设您有类似 ${eintraege.id}
的内容在你的 JSF 代码中,那是行不通的,因为它没有 id
属性.
您可能想要做的是遍历 eintraege
中的所有对象,并显示每个对象的 ID。这意味着你可能需要做这样的事情:
<c:forEach var="telefonbuch" items="#{eintraege.content}">
<h:outputText value="#{telefonbuch.id}" />
</c:forEach>
这里发生的是我们遍历 eintraege.getContent()
,returns 一个可以迭代的集合。然后你应该将每个对象分配给一个变量(例如telefonbuch
),然后你可以调用像#{telefonbuch.id}
.