【问题标题】:Error the FacesContext is already released错误 FacesContext 已被释放
【发布时间】:2013-03-21 12:55:08
【问题描述】:

以下代码正在使用 request scoped 托管 Bean,并且消息显示在 p:messages 标记中:

FacesContext context = FacesContext.getCurrentInstance();

public void addAction() {
    if(commande.exists()) {
                context.addMessage("Commande", new FacesMessage(
                        FacesMessage.SEVERITY_ERROR,"Error Message : commande exists", null));
    } else {
        commandeService.create(commande);
    }
}

当我尝试在 view scoped Managed Bean 中使用它时,它会在记录器中显示错误消息:

Error the FacesContext is already released!

JSF 页面挂起而不显示面孔消息。

【问题讨论】:

    标签: jsf-2 message facescontext view-scope


    【解决方案1】:

    您应该从不FacesContext 的当前实例或其任何属性指定为托管bean 的属性。当前实例仅在当前 HTTP 请求中有效,在 HTTP 请求结束时销毁,在任何后续 HTTP 请求中不再有效。

    您应该始终在方法本地范围内获取当前实例。

    public void addAction() {
        if (commande.exists()) {
            FacesContext.getCurrentInstance().addMessage("Commande", new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "Error Message : commande exists", null));
        } else {
            commandeService.create(commande);
        }
    }
    

    【讨论】:

    • 谢谢,它有效。使用请求 bean 时,每个调用都会初始化上下文属性,因此隐藏了错误,但使用视图范围的 bean 则不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多