当LazyDataModel load方法加载数据时,我需要显示一条消息。我使用primefaces growl来显示消息,并使用下面的代码从load方法更新它,但它不工作(在UI上看不到任何消息)。请指出我做错了什么,这似乎与load方法的异步行为有关,但我不确定如何修复它。
JSF Bean -
@ManagedBean(name = "cData")
@ViewScoped
public class DataPage implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2193735279937686495L;
@PostConstruct
public void init() {
FacesContext.getCurrentInstance().addMessage("growl-sticky", new
FacesMessage(FacesMessage.SEVERITY_INFO, "Sticky Message",
"Message Content from init"));
loadData();
}
private void loadData() {
FacesContext.getCurrentInstance().addMessage("growl-sticky", new
FacesMessage(FacesMessage.SEVERITY_INFO, "Sticky Message",
"Message loadData before load"));
setMobiles(new LazyDataModel<Mobile>() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public List<Mobile> load(int first, int pageSize,
String sortField, SortOrder sortOrder,
Map<String, Object> filters) {
List<Mobile> data = new DataRepo().getMobileData();
mobiles.setRowCount(data.size());
FacesContext.getCurrentInstance().addMessage("growl-sticky", new
FacesMessage(FacesMessage.SEVERITY_INFO, "Sticky Message",
"Message Content from load"));
return data;
}
@Override
public Mobile getRowData(String rowKey) {
// some code
}
@Override
public Object getRowKey(Mobile object) {
// some code
}
});
}
public LazyDataModel<Mobile> getMobiles() {
return mobiles;
}
public void setMobiles(LazyDataModel<Mobile> mobiles) {
this.mobiles = mobiles;
}
private LazyDataModel<Mobile> mobiles = null;
}
xhtml页面-
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:c = "http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>DataTable tag Example</title>
</h:head>
<h:body>
<h3>Mobile Details</h3>
<h:form>
<c:metadata>
<c:viewAction action="#{cData.showSticky}" />
</c:metadata>
<p:growl id="growl-sticky" showDetail="true" sticky="true" autoUpdate="true"/>
<p:commandButton actionListener="#{cData.showSticky}"
update="growl-sticky" value="Info" style="width: 10rem"
styleClass="ui-button-help" />
<h:dataTable value="#{cData.mobiles}" var="mobile" border="2" paginator="true" rows="10"
lazy="true">
<h:column>
<c:facet name="header">Name</c:facet>
#{mobile.companyname}
</h:column>
<h:column>
<c:facet name="header">Model Number</c:facet>
#{mobile.modelnumber}
</h:column>
<h:column>
<c:facet name="header">Color</c:facet>
#{mobile.color}
</h:column>
<h:column>
<c:facet name="header">Quantity</c:facet>
#{mobile.quantity}
</h:column>
<h:column>
<c:facet name="header">Price</c:facet>
#{mobile.price}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
发布于 2021-09-05 10:26:20
消息不显示的问题是因为调用Load时的阶段。下面的解释和解决方法。
load方法是在RENDER_RESPONSE阶段调用的,这很不方便,因为这里可能会出现错误消息,而且它们通常不会被呈现,因为p:messages在大多数情况下都是作为第一个组件之一呈现的。
问题:https://github.com/primefaces/primefaces/issues/3501
解决方法:https://github.com/primefaces/primefaces/issues/3501#issuecomment-469731529
https://stackoverflow.com/questions/69065056
复制