Reputation: 127
I want use <p:messages>
to display error message, use <p:growl>
to display success messages.
In the backing bean:
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(title, msg));
But I found whatever I add message in the backing bean, <p:messages>
and <p:growl>
both display it.
Any suggestion.
Upvotes: 8
Views: 28593
Reputation: 9266
In the growl's demo page of PrimeFaces, they mentioned that: "Growl simply replaces h:messages component.". I'm afraid that you may not be able to achieve your goal because growl will also display all FacesMessage in the View.
However, if you reverse your requirement - display errors using <p:growl>
& display successful messages using <p:message>
, you can actually achieve that as following:
<p:message id="successMsg" for="successMsg" />
@ManagedBean
@RequestScoped
public class MrBean {
public void doSomething() {
FacesContext context = FacesContext.getCurrentInstance();
if (failed) {
context.addMessage(null, new FacesMessage("Failed", "Sry boss! I have failed."));
} else {
context.addMessage("successMsg", new FacesMessage("Successful", "Hey boss! I did it!"));
}
}
}
Upvotes: 13
Reputation: 723
I added a separate messages panel for growl and messages and update this panel when i submit the form as following:
<p:panel id="messages">
<p:messages for="errorMsg" showDetail="true" />
<p:growl for="infoMsg" showDetail="true" />
<p:growl globalOnly="true" />
</p:panel>
<p:commandButton id="submit" value="Submit" update="messages" actionListener="#{yourManagedBean.submit}" />
In ManagedBean
// display submit info - showDetail="true" in infoMsg will show details message
FacesMessage infoMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Submit Info", "Your details info message." );
FacesContext.getCurrentInstance().addMessage("infoMsg", infoMsg);
// display error message - e is instance of Exception
FacesMessage errMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Occured", e.getMessage());
FacesContext.getCurrentInstance().addMessage("errorMsg", errMsg);
// display some generic message (with no details in growl)
FacesMessage genericMsg = new FacesMessage("Generic Message");
FacesContext.getCurrentInstance().addMessage(null, genericMsg);
I have tested this using Primefaces 5.0.
Enjoy!
Upvotes: 1
Reputation: 461
JSF:
<p:messages for="somekey" />
<p:growl for="anotherkey" />
Bean:
context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces Rocks"));
context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "Always bet on Prime"));a
context.addMessage("anotherkey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces is developed by Chuck Norris"));
This just worked fine with me!
Upvotes: 11
Reputation: 217
This maybe an old question, but maybe this is relevant still:
I had this same situation come up, I worked it out using the for
statement in each tag
<p:growl id="someid" for="growl" />
<p:messages id="messageid" for="messages" />
and the backing bean:
context.addMessage("growl", new FacesMessage("Successful", "to growl"));
context.addMessage("messages", new FacesMessage("Successful", "to messages"));
just as a side note, if you add autoupdate="true"
then the messages will get reset for both growl and messages (if you add messages to growl, messages will get cleared)
Upvotes: 2
Reputation: 1975
It looks like as of PrimeFaces 3.3 you can do what you wanted to do:
http://blog.primefaces.org/?p=1894
You can now do:
<p:messages severity="error" />
<p:growl severity="info, warn" />
with
context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Title", "Error Message"));
context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Success Title", "Success Message"));
Upvotes: 10
Reputation: 6249
You can use a p:growl that doesn't render when there are only validation errors.
<p:growl
id="growl"
sticky="true"
showDetail="true"
rendered="#{not facesContext.validationFailed}"/>
Upvotes: 1