如何处理原型中的错误懒惰负载? [英] How to handle error in Primefaces lazy load?

查看:152
本文介绍了如何处理原型中的错误懒惰负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题要让用户了解在 PrimeFaces 中发生的异常。 LazyDataModel#load 方法。

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.

我尝试将 FacesMessage 添加到 FacesContext ,但消息未显示在Growl组件上,即使Growl设置为 autoUpdate =true

I tried to add FacesMessage to FacesContext, but message is not shown on the Growl component, even if Growl is set to autoUpdate="true".

使用 PrimeFaces 3.3

推荐答案

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

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>

控制器:

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

希望这有帮助。

这篇关于如何处理原型中的错误懒惰负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆