How to deal with the error in easy loading Primefaces?

I have a problem to inform the user about exceptions that occurred in PrimeFaces LazyDataModel#load .

I load data from a database, and when an exception occurs, I do not know how to report this to the user.

I tried adding FacesMessage to the FacesContext , but the message does not appear in the Growl component, even if Growl is set to autoUpdate="true" .

Using PrimeFaces 3.3 .

+3
source share
1 answer

This does not work, because the load() method is called during the Render response phase (this can be checked by printing FacesContext.getCurrentInstance().getCurrentPhaseId() ) when all messages have already been processed.

The only workaround that worked for me was loading the data into the DataTable's "page" event listener.

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.

+3
source

Source: https://habr.com/ru/post/945298/


All Articles