如何处理 Primefaces 延迟加载中的错误?

2024-05-08

我无法让用户知道发生的异常PrimeFaces http://primefaces.org/ LazyDataModel#load方法。

我正在从数据库加载数据,当引发异常时,我不知道如何通知用户。

我尝试添加FacesMessage to FacesContext,但消息不会显示在 Growl 组件上,即使 Growl 设置为autoUpdate="true".

Using PrimeFaces 3.3 http://primefaces.org/downloads.html.


它不起作用,因为load()方法在渲染响应阶段被调用(您可以通过打印来检查这一点)FacesContext.getCurrentInstance().getCurrentPhaseId()),当所有消息都已被处理时。

对我有用的唯一解决方法是将数据加载到"page"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"));
    }
}

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何处理 Primefaces 延迟加载中的错误? 的相关文章

随机推荐