2012-06-22 61 views
3

我有問題讓用戶知道在PrimeFacesLazyDataModel#load方法中發生的異常。如何處理Primefaces延遲加載中的錯誤?

我從數據庫加載數據,當發生異常時,我不知道如何通知用戶它。

我試圖將FacesMessage添加到FacesContext,但即使Growl設置爲autoUpdate="true",消息也不會顯示在Growl組件上。

使用PrimeFaces 3.3

+0

我在我的PRO帳戶下報告了它,它在3.5.21和4.0.5版本中被修復。 https://code.google.com/p/primefaces/issues/detail?id=4945 – Melloware

回答

3

它不起作用,因爲load()方法在渲染響應階段被調用(您可以通過打印FacesContext.getCurrentInstance().getCurrentPhaseId()來檢查),當所有消息都已經被處理時。

對我而言,唯一的解決方法是將數據加載到DataTable的事件偵聽器的「頁面」中。

HTML:

<p:dataTable value="#{controller.model}" binding="#{controller.table}"> 
    <p:ajax event="page" listener="#{controller.onPagination}" /> 
</p:dataTable> 

控制器:

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")); 
    } 
} 

希望這有助於。

+0

感謝它的幫助。 – valerian