<p: growl> and <p: messages> on the same page

I want to use <p:messages> to display an error message, use <p:growl> to display success messages. In the bean database:

 FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage(title, msg)); 

But I found everything that I add the message to the backup bean, <p:messages> and <p:growl> , and display it.

Any suggestion.

+4
source share
6 answers

On the PrimeFaces growl demo page, they mentioned that: "Growl just replaces the h: messages component." I'm afraid you wonโ€™t be able to reach your goal, because a growl will also display all FacesMessage in the view.

However, if you cancel your requirement - display errors with <p:growl> and display successful messages with <p:message> , you can achieve this as follows:

 <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!")); } } } 
+11
source

It looks like with PrimeFaces 3.3 you can do what you want:

http://blog.primefaces.org/?p=1894

Now you can 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")); 
+10
source

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

It just worked with me!

+9
source

You can use p: growl, which is not displayed when there are only validation errors.

 <p:growl id="growl" sticky="true" showDetail="true" rendered="#{not facesContext.validationFailed}"/> 
+1
source

This may be an old question, but perhaps it is still relevant:

I had the same situation, I worked through it with the for statement in each tag

 <p:growl id="someid" for="growl" /> <p:messages id="messageid" for="messages" /> 

and bean support:

 context.addMessage("growl", new FacesMessage("Successful", "to growl")); context.addMessage("messages", new FacesMessage("Successful", "to messages")); 

as a side note, if you add autoupdate="true" then the messages will be reset for both growling and messages (if you add messages to the growling, the messages will be cleared)

0
source

I added a separate message bar for growls and messages and updated this bar when I submit the form as follows:

  <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 tested this with Primefaces 5.0.

Enjoy it!

0
source

Source: https://habr.com/ru/post/1387989/


All Articles