Question

The following code is working with request scoped Managed Beans and message is showing in the p:messages tag:

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

When I tried to use it in a view scoped Managed Bean it gives an error message in the logger:

Error the FacesContext is already released!

and the JSF page hangs without showing faces message.

Was it helpful?

Solution

You should never assign the current instance of the FacesContext or any of its properties as a property of a managed bean. The current instance is only valid in the current HTTP request, it is destroyed by end of the HTTP request and not valid anymore in any subsequent HTTP requests.

You should always get the current instance in the method local scope.

public void addAction() {
    if (commande.exists()) {
        FacesContext.getCurrentInstance().addMessage("Commande", new FacesMessage(
            FacesMessage.SEVERITY_ERROR, "Error Message : commande exists", null));
    } else {
        commandeService.create(commande);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top