The components <h:message>
and <h:messages>
are dedicated to display messages to users (generally error message).
For example, when you have a validation on a field that failed (for example the user didn't fill a required field, or inputed a string in a number-only field), then a FacesMessage
is added to the FacesContext
object. The <h:message>
and <h:messages>
are then used to display the message in the page.
The component <h:messages>
will display all the messages contained in the FacesContext
, while the <h:message>
is dedicated to a specific clientId (a specific field). The latter is usefull when you want to place the message close to a field for example.
Note that you can add any kind of message that will displayed to the user:
FacesContext.getInstance().addMessage(null, new FacesMessage("The message to display"));
In this example, the first parameter is the ID field of the field that is concerned by this message (usefull when the message is a validation message for a specific field). null
means that the message is a general information (i.e. not linked to any particular field).
You can see an example of this component here. Note that this example uses the rich:messages
that is an extension (provided by RichFaces) of the "basic" <h:message/>
, but the principle is the same.