Defining Custom Error Messages

As you can see in Figure 7-1, the default error messages that the JSF reference implementation produces are generic. Also, they might differ between JSF implementations, so you’ll probably want to define your own messages.

All standard messages are defined as resources of a Java resource bundle, and you can create your own bundle and selectively override the standard messages with your own versions. Your own bundle must be declared in the faces-config.xml file, like this:

<faces-config>
  <application>
    <message-bundle>custMessages</message-bundle>
  </application>
  ...
</faces-config>

The value of the <message-bundle> element is the so-called bundle base name. If you’re unfamiliar with resource bundles, don’t worry. Even though there’s more than one way to make a bundle (as well as to skin a cat), the easiest way is to create a text file that contains all the resources as text strings identified by keys. The name of the file is the base name followed by a .properties extension, and the file must be stored somewhere in the application’s classpath. I recommend putting it in the application’s WEB-INF/classes directory, because it’s always included in the classpath. Here’s the sample application’s custMessage.properties file with the replacement text for the message generated by the DoubleRangeValidator when the value is below the specified minimum:

javax.faces.validator.DoubleRangeValidator.MINIMUM=Please enter a value greater \
than {0}.

Each message is declared ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.