Using rich:messages in a good way

Submitted by Jochus on Sun, 14/02/2010 - 19:47 | Posted in: Java

Imagine you have a form like the picture in the left. The form has been validated, but all error messages are at the top of the form. I prefer to put the error message directly under the textfield on which the error is produced. In this way, the user is better informed about which fields are not perfectly filled in.

So, to do this you need ...


Check the API

  • String id: the id of the component (textfield) on which the error message should be attached
  • javax.faces.application.FacesMessage.Severity severity: in most of the cases, Severity.ERROR should do the trick
  • String key: the key of your i18n message, which you can find in your ResourceBundle
  • Object... params: parameters of your i18n message (look at {0}, {1}, ...)


Directly under your component, you put this:

<h:inputText label="Address" id="address" required="true" value="#{userBean.address}">
    <f:validateLength minimum="10" />
<rich:messages for="address"/>


If you want to throw a "global" message, you need the: FacesMessages.addFromResourceBundle()

Don't forget to specify this is in your XHTML file:

<rich:messages globalOnly="true"/>

... otherwise, you will see the error message in the global box AND the textfield box

Add new comment

The content of this field is kept private and will not be shown publicly.


  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.