我有问题让用户知道 PrimeFaces 中发生的异常LazyDataModel#load
方法。
我正在从数据库加载数据,当出现异常时,我不知道如何通知用户。
我尝试添加 FacesMessage
至 FacesContext
,但即使 Growl 设置为 autoUpdate="true"
,消息也不会显示在 Growl 组件上.
使用 PrimeFaces 3.3 .
最佳答案
它不起作用,因为 load()
方法在渲染响应阶段调用(您可以通过打印 FacesContext.getCurrentInstance().getCurrentPhaseId()
来检查),此时所有消息都已被处理。
对我有用的唯一解决方法是在 DataTable 的“页面”事件监听器中加载数据。
html:
<p:dataTable value="#{controller.model}" binding="#{controller.table}">
<p:ajax event="page" listener="#{controller.onPagination}" />
</p:dataTable>
Controller :
private List<DTO> listDTO;
private int rowCount;
private DataTable table;
private LazyDataModel<DTO> model = new LazyDataModel<DTO>() {
@Override
public List<DTO> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {
setRowCount(rowCount);
return listDTO;
}
};
public void onPagination(PageEvent event) {
FacesContext ctx = FacesContext.getCurrentInstance();
Map<String, String> params = ctx.getExternalContext()
.getRequestParameterMap();
// You cannot use DataTable.getRows() and DataTable.getFirst() here,
// it seems that these fields are set during Render Response phase
// and not during Update Model phase as one can expect.
String clientId = table.getClientId();
int first = Integer.parseInt(params.get(clientId + "_first"));
int pageSize = Integer.parseInt(params.get(clientId + "_rows"));
try {
listDTO = DAO.query(first, pageSize);
rowCount = DAO.getRowCount();
} catch (SQLException e) {
ctx.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"SQL error",
"SQL error"));
}
}
希望这可以帮助。
关于error-handling - 如何处理 Primefaces 延迟加载中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11152840/