我需要一些帮助才能在我的应用中实现延迟加载。
数据表:
<p:dataTable var="log" value="#{logger.getAll()}"
widgetVar="loggerTable"
emptyMessage="No logs found with given criteria"
rowStyleClass="#{log.status eq 'uspesno' ? 'old' : null}">
<!-- //Main
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="PF('loggerTable').filter()"
style="width:150px" placeholder="Enter keyword" />
</p:outputPanel>
</f:facet> -->
<p:column filterBy="#{log.mdmId}" headerText="Id"
footerText="contains" filterMatchMode="contains">
<h:outputText style="display: block; width:100%; "
value="#{log.mdmId}" />
</p:column>
<p:column filterBy="#{log.pib}" headerText="pib"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{log.pib}" />
</p:column>
<!-- // poruka
<p:column filterBy="#{log.poruka}" headerText="message"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{log.poruka}" />
</p:column> -->
<p:column filterBy="#{log.status}" headerText="status"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{log.status}" />
</p:column>
<p:column filterBy="#{log.mbr}" headerText="mbr"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{log.mbr}" />
</p:column>
<p:column filterBy="#{log.datumUpisa}" headerText="datumupisa"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{log.datumUpisa}" />
</p:column>
<p:column style="width:40px;text-align: center">
<p:commandButton update=":form:logDetail"
oncomplete="PF('logDialog').initPosition();PF('logDialog').show()"
icon="fa fa-search" title="View">
<f:setPropertyActionListener value="#{log}"
target="#{logger.selectedLog}" />
</p:commandButton>
</p:column>
</p:dataTable>
ManagedBean:
private static final long serialVersionUID = 1L;
private Integer mdmId;
private String pib;
private String mbr;
private Date datumUpisa;
private String status;
private String poruka;
private Logger selectedLog;
public Logger getSelectedLog() {
return selectedLog;
}
public static List<Logger> getAll(){
List<Logger> lista = LoggerDAO.getAll();
return lista;
} // ...
DAO:
public static List<Logger> getAll() {
Connection con = null;
PreparedStatement ps = null;
List<Logger> lista = new ArrayList<>();
try {
con = DataConnect.getConnection();
ps = con.prepareStatement("Select * from log");
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
int mdm_id = resultSet.getInt(1);
String pib = resultSet.getString(2);
String mbr = resultSet.getString(3);
Date datumUpisa = resultSet.getDate(4);
String status = resultSet.getString(5);
String poruka = resultSet.getString(6);
Logger log = new Logger();
log.setMbr(mbr);
log.setMdmId(mdm_id);
log.setDatumUpisa(datumUpisa);
log.setPib(pib);
log.setPoruka(poruka);
log.setStatus(status);
lista.add(log);
}
} catch (SQLException ex) {
System.out.println("Login error -->" + ex.getMessage());
return null;
} finally {
DataConnect.close(con);
}
return lista;
}
我想实现延迟加载,但是我不知道是否可能。 primefaces提供的数据库没有示例。我是否需要编写查询来获取几行,还是应该一次返回所有行? 我需要一些帮助和建议,谢谢
答案 0 :(得分:1)
我考虑到您要实现分页。
首先将<p:dataTable>
修改为以下内容:
<p:dataTable lazy="true" rows="8" paginator="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="2,4,6,8"></p:dataTable>
然后在每个filterBy
字段中添加sortBy
,<p:column>
(根据需要)。
然后,在Managed Bean类中,创建私有字段: private LazyDataModel<Logger> lazyDataModel;
在托管bean中为 LazyDataModel<Logger> lazyDataModel
创建合适的吸气剂和吸气剂。
现在使用getAll()
方法,但是将返回类型设为void
,添加以下内容:
public static void getAll(){
lazyDataModel = new LazyLoggerDataModel(LoggerDAO.getAll());
}
创建一个LazyLoggerDataModel
的{{1}}类。看起来像这样:
extends LazyDataModel<Logger>
请参阅Primefaces Official的Lazy-加载示例以获取更多信息:
链接此处: Primefaces Lazy Loading Example
希望这会对您有所帮助:)
答案 1 :(得分:-2)
This文章解决了我的问题:
getAll()
在DAO中:
public static List<Logger> getAll(int from , int to) {
Connection con = null;
PreparedStatement ps = null;
List<Logger> lista = new ArrayList<>();
try {
con = DataConnect.getConnection();
int upper = from + to;
ps = con.prepareStatement("Select * from log limit ?,?");
ps.setInt(1, from);
ps.setInt(2, upper);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
int mdm_id = resultSet.getInt(1);
String pib = resultSet.getString(2);
String mbr = resultSet.getString(3);
Date datumUpisa = resultSet.getDate(4);
String status = resultSet.getString(5);
String poruka = resultSet.getString(6);
Logger log = new Logger();
log.setMbr(mbr);
log.setMdmId(mdm_id);
log.setDatumUpisa(datumUpisa);
log.setPib(pib);
log.setPoruka(poruka);
log.setStatus(status);
lista.add(log);
}
} catch (SQLException ex) {
System.out.println("Login error -->" + ex.getMessage());
return null;
} finally {
DataConnect.close(con);
}
return lista;
}