Display JSF Error Messages Using Custom HTML

By default JSF messages are shown using <h:messages/> tag. This includes form validation error messages. This tag shows messages in a simple bulleted list using <ul> and <li>. For example:

<li>Please enter a phone number</li>
<li>Please enter a valid e-mail address.</li>

However, if you wish to show messages using custom HTML layout and additional instructions, <h:messages> will not be useful. The code below shows all error messages (severity level 2) using custom HTML:

<h:panelGroup rendered="#{facesContext.maximumSeverity.ordinal == 2}" 
 <p>Please fix these problems:</p>
   <ui:repeat var="msg" value="#{facesContext.messageList}">

Note, JSF doesn’t say anywhere that the error level messages have an ordinal of 2. This may change from implementation to implementation.

Similarly, to show informational messages, you can do:

<h:panelGroup rendered="#{facesContext.maximumSeverity.ordinal == 0}" 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s