O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required