Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 742
yannicuLar Avatar asked Dec 20 '25 16:12

yannicuLar


1 Answers

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

  • Calling Primefaces dialog box from Managed Bean function
like image 87
Vrushank Avatar answered Dec 22 '25 13:12

Vrushank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!