我正在尝试在我的primefaces数据表设置中实现延迟加载。目前,这没有延迟加载实现,但在实现延迟加载后,我在数据表中没有数据。但是,我可以在我的LoadData方法中打印我的列表以验证数据是否已加载到我的列表中,但是一旦我的LazyModel被返回并且尝试了数据表加载,似乎就会出现问题。这可能只是我忽略的一些简单。非常感谢任何帮助!
这是我的ScreenshotListProducer类代码:
@ManagedBean
@RequestScoped
public class ScreenshotListProducer implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private EntityManager em;
private List<Screenshot> screenshots;
private LazyDataModel<Screenshot> lazyModel = null;
private int pageSize = 5;
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
@Produces
@Named
public List<Screenshot> getScreenshots() {
System.out.println("************* getting screenshots list **************");
return screenshots;
}
@PostConstruct
public void LoadData() {
lazyModel = new LazyDataModel<Screenshot>() {
private static final long serialVersionUID = 1L;
@Override
public List<Screenshot> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
int start = first;
int end = first + pageSize;
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Screenshot> criteria = cb.createQuery(Screenshot.class);
Root<Screenshot> screenshot = criteria.from(Screenshot.class);
criteria.select(screenshot).orderBy(cb.asc(screenshot.get("time")));
TypedQuery<Screenshot> s = em.createQuery(criteria);
s.setMaxResults(end - start);
s.setFirstResult(start);
screenshots = s.getResultList();
System.out.println("************* lazy screenshot list created **************");
for (Screenshot lazylist : screenshots) {
System.out.println("id numbers in lazy list: " + lazylist.getId());
}
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return screenshots;
}
};
}
public LazyDataModel<Screenshot> getLazyModel() {
return lazyModel;
}
这是我的截图类:
@Entity
@XmlRootElement
@Table(name="Test2012", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class Screenshot implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id", columnDefinition="INT")
private Long id;
private Timestamp time;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
}
这是我的xhtml代码:
<p:dataTable id="table1" var="scrshot" rowKey="#{scrshot.id}" value="#{screenshotListProducer.lazyModel}" paginator="true" rows="7" paginatorPosition="bottom"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="7,20,50,100" widgetVar="dataTable" currentPageReportTemplate="(Number of Records: {totalRecords})"
emptyMessage="No screenshot data found with given criteria" lazy="true" scrollable="true" draggableColumns="true" scrollHeight="217" style="width: 100%;">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields: "/>
<p:inputText id="globalFilter" onkeyup="dataTable.filter()"/>
</p:outputPanel>
</f:facet>
<p:column selectionMode="multiple"/>
<p:column id="time" headerText="Time" sortBy="#{scrshot.time}" filterBy="#{scrshot.time}" filterMatchMode="startsWith">
<h:outputText value="#{scrshot.time}"/>
</p:column>
<p:column id="id" headerText="ID" sortBy="#{scrshot.id}" filterBy="#{scrshot.id}" filterOptions="#{scrshot.id}" filterMatchMode="exact">
<h:outputText value="#{scrshot.id}"/>
</p:column>
</p:dataTable>
答案 0 :(得分:10)
在延迟加载程序的情况下,你必须设置你的记录总数,如“this.setRowCount(total_records_that_are_there);”在你的“加载”方法。延迟加载器必须确定将有多少页面,以便可以确定数据表上的下一个和前一个按钮,即(启用/禁用)。像[0是开始]和[total / rowsize]是你使用load方法获取的页面总数(first和pagezie =&gt; skip和limit);