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.
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!"));
}
}
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With