Primefaces : how to use growl for notifications without user interaction
It is possible. When the message is added on the constructor, @PostConstruct or preRenderView listener of the bean, it is displayed on the page automatically, you don't have to use any "onload" event. The problem with your code is that you are not sending the message correctly. This is how you should do it:
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "test", "test");
FacesContext.getCurrentInstance().addMessage(null, msg);
The first parameter of addMessage is the ID of the component that had an error, or null in case it's a global error.
Also, if in your growl you only want to show global messages, and you want error messages specific to a component to be displayed only using p:message for each component, you should add globalOnly="true"
to the growl.
Qualaelay
Updated on October 10, 2022Comments
-
Qualaelay over 1 year
I'm pretty new to JSF/Primefaces so maybe I'm taking the problem the wrong way and I've been searching forums quite a lot with no luck.
I simplified the problem to the maximum and used that @PostConstruct of Damian
So to resume I would like that upon loading test.xhtml, my growl notification pops up - ie with no user interaction... And it's not showing up, however it does when you click the save button.
It does go through the sayHi() but doesn't seem to know the container at that point.
WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
The aim would be to show a user-friendly notification with growl if a database access has failed.
I must be missing some basic principle I believe :(
test.xhtml
<h:form id="form"> <p:growl id="growl" showDetail="true" showSummary="true" sticky="true" /> <h:outputLabel value="#{testGrowl.message}" /> <p:commandButton value="Save" actionListener="#{testGrowl.save}" update="growl" /> </h:form>
TestGrowl.java
@ManagedBean public class TestGrowl { private String message; public TestGrowl() { //sayHi(); commented to try with @PostConstruct message = "we've been in the TestGrowl constructor"; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void save(ActionEvent actionEvent) { sayHi(); } @PostConstruct private void sayHi(){ FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Successful", "Hello ")); } }
Many thanks
-
Qualaelay almost 12 yearsHi Damian, I also tried with null but it keeps queuing these FacesMessage, even with
globalOnly="true"
. The idea was actually to have a "growl" as general notifications, and a "growlError" withsticky="true"
for more severe errors. However when I add a<p:selectOneMenu>
that has a<p:ajax event="change" update="growl">
then, when I select one item, it shows the growl messages that have been queued, which is why I was wondering if there was a way to show them without user input. -
damian almost 12 years@Qualaelay where are you sending the message when it doesn't work? in @PostConstruct? what is the scope of the bean? what version of primefaces are you using?
-
Qualaelay almost 12 yearsI'm using PF 3.2, and not using @PostConstruct (actually had never heard of it coming from SE, in my bean constructor I was calling my init function directly). I removed the concept of error to simply displaying a growl message when the init method is called, I provided the files above :/
-
damian almost 12 yearstry adding @PostConstruct annotation to fillInFilteredList() method, and remove the call to this method from the constructor (it will be automatically called after bean is constructed).
-
Qualaelay almost 12 yearsI edited the initial post with your @PostConstruct, but is still doesn't work - I have simplified the problem to the max too :(