The Java EE 5 Tutorial

Creating a Message with a Message Factory

    To use a message factory to create a message, follow these steps:

  1. Register the ResourceBundle instance with the application. This is explained in Registering Custom Error Messages.

  2. Create a message factory implementation. You can simply copy the MessageFactory class included with the Duke’s Bookstore application to your application.

  3. Access a message from your application by calling the getMessage(FacesContext, String, Object) method of the MessageFactory class. The MessageFactory class uses the FacesContext to access the Application instance on which the messages are registered. The String argument is the key that corresponds to the message in the ResourceBundle. The Object instance typically contains the substitution parameters that are embedded in the message. For example, the custom validator described in Implementing the Validator Interface will substitute the format pattern for the {0} in this error message:

    Input must match one of the following patterns {0}

Implementing the Validator Interface gives an example of accessing messages.