5

In my Facelets page I have this:

<p:growl id="msg1" life="1500"/>

and another

<p:messages id="msg2"/>

I need the following message to show up in <p:messages> only.

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("veillez saisir les champs obligatoires (*)", null));

But it also shows up in <p:growl>.

How do I specify where the message should show up?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hayi
  • 6,972
  • 26
  • 80
  • 139

5 Answers5

16

Extracted from primefaces manual. Page 282.

Targetable Messages

There may be times where you need to target one or more messages to a specific message component, for example suppose you have growl and messages on same page and you need to display some messages on growl and some on messages. Use for attribute to associate messages with specific components.

<p:messages for="somekey" />
<p:growl for="anotherkey" />

The Bean

FacesContext context = FacesContext.getCurrentInstance();
context.addMessage("somekey", facesMessage1);
context.addMessage("somekey", facesMessage2);
context.addMessage("anotherkey", facesMessage3);

In sample above, messages will display first and second message and growl will only display the 3rd message.

13

Since p:messages is just extension for h:messages and p:growl is practically the same thing as h:messages you can't. What you can do is to not update p:growl after you add a message to context (probably you do that in some "confirm" commandButton) then it won't display at all, but you can't specify to display only some messages. Better solution is to not mix p:growl with p:messages and use only one.

The feature you are looking for will be available in new Primefaces 3.3 Targetable messages

Fallup
  • 2,417
  • 19
  • 30
  • You clearly have no idea about how it works. `p:messages` is a component to display all generated messages. If you want to generate specific message for a specific action use `p:message` instead. Take a look at Primefaces showcase [example](http://www.primefaces.org/showcase/ui/messages.jsf) and don't confuse `p:message` with `p:messages` (s). Attribute `for` is the key. – Fallup May 16 '12 at 21:12
  • the key for what ? can you give me a example ? because i want add message like that : `FacesContext.getCurrentInstance().addMessage` – Hayi May 16 '12 at 21:59
  • The key for solving your problem. I've already gave you an example. If you need to use `FacesContext.getCurrentInstance().addMessage` then you can specify it like it is described in this post [Messages and FacesContext#addMessage](http://stackoverflow.com/questions/315804/how-to-display-my-applications-errors-in-jsf) – Fallup May 16 '12 at 22:15
  • By the way I answered your question so it would be nice if you vote it up and accept. – Fallup May 17 '12 at 06:38
  • thanks but i don't know how to vote i'm new in this here, i try to vote but they tell me i don"t have enough reputation – Hayi May 17 '12 at 11:28
  • I suggest you to read FAQ section [here](http://stackoverflow.com/faq). If you can't vote, just accept answer. – Fallup May 17 '12 at 11:31
2

Since you have already assigned 2 different IDs for <p:growl> and <p:messages>, I think you can try something like this:

<div id="aDiv">

    ### Use the following button to update only <p:growl id="msg1"> ###
    <p:commandButton actionListener="#{mrBean.doSomething}" update="msg1" />

    ### Use the following button to update only <p:messages id="msg2"> ###
    <p:commandButton actionListener="#{mrBean.doSomethingElse}" update="msg2" />

</div>

The key is that you should only update either msg1 or msg2, not both. In the above example, if your button has the attribute update="aDiv", your messages will be shown on both <p:growl> and <p:messages>.

Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • As I stated, he can choose what to update in order to re-render just one of these components, but his original question is if he can choose which messages will be displayed in which component. And that is not possible (will be in PF 3.3). – Fallup May 17 '12 at 17:38
1

You can use "Severity" Attribute of p:growl to specify which type of Messages you want to Show only in Growl.

<p:growl id="messages" severity="info, warn, error, fatal" showDetail="true" life="5200" autoUpdate="true" />

Now if you want to not to use Growl for info Messages then simply remove "info" perameter and then you can use either p:message or any text of your own choice and style it accordingly <p:outputLabel value="A Validation error occured!" rendered="#{facesContext.validationFailed}" /> So in this way you can use Growl and message according to your choice.

Undertaker
  • 111
  • 2
  • 13
0

it's easy,here an example in JSF + PrimeFaces 5.2

<p:messages for ="Message1" showDetail="true" autoUpdate="true" closable="true" />

<p:messages for ="Message2" showDetail="true" autoUpdate="true" closable="true" />
FacesContext.getCurrentInstance().addMessage("Message1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Hello 1"));
FacesContext.getCurrentInstance().addMessage("Message2", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Hello 2"));
David Hackro
  • 3,652
  • 6
  • 41
  • 61