During the JSF life cycle, any object can create a message and add it to a queue
of messages maintained by the faces context.
At the end of the life cycle, in the
Render Response phase, you
can display those messages in a view.
Typically, messages are associated with a particular component and
indicate either conversion or validation errors.
The tags:
- h:messages: displays all messages that were stored in the
faces context during the course of the JSF life cycle
- h:message: displays a single message for a particular
component, which is designated with h:message’s mandatory
for attribute
Their use is briefly described here.
See table 4-27 on pp. 172-173 for a listing of the available attributes
for these tags.
See
Example Applications for an example application that uses
them.
In the example below,
h:messages is used to display all messages
at the top of the JSF page.
h:message is used to display messages for each input field.
Note the use of the input text field's
required attribute
— if the field is empty on submission an error will be generated.
<h:form>
<h:messages layout="table" errorClass="errors"/>
...
<h:inputText id="name"
value="#{user.name}" required="true" label="#{msgs.namePrompt}"/>
<h:message for="name" errorClass="errors"/>
...
<h:inputText id="age"
value="#{form.age}" required="true" label="#{msgs.agePrompt}"/>
<h:message for="age" errorClass="errors"/>
...
</h:form>
In the scenario shown below, the name is missing and the age is not an
integer, generating automatic
validation errors.
The error messages make use of the input fields' labels ("
Name:"
and "
Age:") provided by their
label attributes.
Also, the error messages are styled using the CSS style
class
errors.