Search code examples
jsf-2primefaces

How can I use a shared error message Dialog in PrimeFaces


In PrimeFaces, when you want to show an error Message you can:

  1. create in View a command Component that calls an action
  2. in 'update' attribute, you set the id of the p:message or p:growl component that will display the message
  3. in The backing Bean, in the action function you throw a message

As a result, the error message will be displayed in the redirect page, in the message component with the matching id

But what if :

  1. You want to display the message in another page, that doesn't contain the command component that called the action.
  2. The action can redirect to lots of different pages, depending on some Backing bean Logic.
  3. The action is not called from a command Component, at least not directly

I've thought of putting a p:message component with a specific id, and include it in every xhtml page. But this would not necessarily cover the 3rd scenario.

For example, there could be a function that checks the connection to another Web Service. A Connection error could be thrown from lots of different Actions.

  • Or a session expiration
  • Or a denial of permission

How would you manage this kind of generic error messages ?


Solution

  • You could put the common <p:dialog> or <h:message> in a template file which is being used for all pages and give it a unique id. That way, it will be rendered for all the pages using that template.

    This, is assuming that you're using templates that is.

    UPDATE: If you wish to programmatically update the component, you can do so using RequestContext#update()

    For e.g.

    if (someErrorCondition) {
        RequestContext.getCurrentInstance().update("errorDialogId");
    }
    

    where errorDialogId is the ID of the common error dialog.

    If you intend to use this approach, you need to remove the update atribute from your command component.

    See Also