How to specify whether the message should show up in p:growl or p:messages?

一笑奈何 提交于 2019-12-07 06:58:16

问题


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?


回答1:


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.




回答2:


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




回答3:


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>.




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/10626089/how-to-specify-whether-the-message-should-show-up-in-pgrowl-or-pmessages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!