How to handle error in Primefaces lazy load?
error-handling, growl, lazy-loading, primefaces
Solution
It doesn't work because `load()` method is invoked during Render Response phase (you can check this by printing `FacesContext.getCurrentInstance().getCurrentPhaseId()`), when all messages have already been processed.
The only workaround that worked for me is to load the data within the "page" event listener of the 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"));
}
}
Hope this helps.
Problem
I have problem to let user know about exceptions occurred in PrimeFaces `LazyDataModel#load` method. I am loading there data from database and when an exception is raised, I have no idea how to inform the user about it. I tried to add `FacesMessage` to `FacesContext`, but message is not shown on the Growl component, even if Growl is set to `autoUpdate="true"`. Using PrimeFaces 3.3.