We sometimes need to target a FacesMessage
instance to a specific component. For example, suppose we have p:growl
and p:messages
/p:message
tags on the same page and need to display some messages as p:growl
and some as p:message
. PrimeFaces has a grouping feature for messages to associate a notification component to specific command components so that messages created as a result of an action will be displayed in the associated messages or the growl tags.
In this recipe, we will develop samples for targetable messages. Furthermore, we will discuss the severity
attribute. By means of severity
, we can display messages depending on their severities.
Let's use one p:messages
tag and two p:growl
tags, as shown in the following code snippet:
<h:panelGroup id="msg1"> <p:messages for="save" showDetail="true"/> <p:growl for="change" showDetail="true"/> <p:growl globalOnly="true" showDetail="true"/> </h:panelGroup> <p:commandButton value="Save" update="msg1" action="#{targetableMessagesBean.addSaveMessage}"/> <p:commandButton value="Change" update="@form" action="#{targetableMessagesBean.addChangeMessage}"/> <p:commandButton value="Delete" update="@form" action="#{targetableMessagesBean.addDeleteMessage}"/>
Three command buttons create FacesMessage
instances. The first button creates two messages that are displayed only by the p:messages
tag. The second command button creates one message that is displayed only by the first p:growl
tag with the for
attribute set to change
. The message created by the third command button is displayed only by the second p:growl
tag with globalOnly
set to true
. The action methods look as follows:
public String addSaveMessage() { addMessage("save", FacesMessage.SEVERITY_INFO, "Sample info message", "First data was successfully saved"); addMessage("save", FacesMessage.SEVERITY_INFO, "Sample info message", "Second data was successfully saved"); return null; } public String addChangeMessage() { addMessage("change", FacesMessage.SEVERITY_INFO, "Sample info message", "Data was successfully changed"); return null; } public String addDeleteMessage() { addMessage(null, FacesMessage.SEVERITY_INFO, "Sample info message", "Data was successfully deleted"); return null; } private void addMessage(String key, FacesMessage.Severity severity, String message, String detail) { FacesMessage msg = new FacesMessage(severity, message, detail); FacesContext.getCurrentInstance().addMessage(key, msg); }
Let's now use the p:messages
and p:growl
tags without the for
attribute but with a severity
attribute:
<h:panelGroup id="msg2"> <p:messages severity="error" showDetail="true"/> <p:growl severity="info, warn" showDetail="true"/> <p:growl showDetail="true"/> </h:panelGroup> <p:commandButton value="Generate error message" update="msg2" action="#{targetableMessagesBean.addErrorMessage}"/>
The command button should create an error message with the severity
error:
public String addErrorMessage() { addMessage(null, FacesMessage.SEVERITY_ERROR, "Sample error message", "Operation failed"); return null; }
The message created is only displayed by the p:messages
tag with severity="error"
and by the second p:growl
tag without a severity
attribute.
The key of an added FacesMessage
instance should match the for
attribute of the p:growl
, p:messages
, or p:message
components to be displayed. If the for
attribute is missing, all added FacesMessage
instances will be accepted. If a notification component has set the globalOnly
flag (globalOnly="true"
), only the FacesMessage
instances without a defined key (key is null
) will be displayed.
The severity
attribute of a notification component defines exactly which severities can be displayed by this component. It accepts a comma-separated list. The possible values are info
, warn
, error
, and fatal
. They match the Java constants, FacesMessage.SEVERITY_INFO
, FacesMessage.SEVERITY_WARN
, FacesMessage.SEVERITY_ERROR
, and FacesMessage.SEVERITY_FATAL
, respectively. If the severity
attribute is missing, messages with any severity will be displayed.
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter11/targetableMessages.jsf
.