Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<p:growl> and <p:messages> in a same page

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.

like image 720
machcm.sz Avatar asked Dec 26 '11 06:12

machcm.sz


2 Answers

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

      }
   }

}
like image 164
Mr.J4mes Avatar answered Jan 04 '23 11:01

Mr.J4mes


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!

like image 28
Zakaria Bouazza Avatar answered Jan 04 '23 11:01

Zakaria Bouazza