Java Reference
In-Depth Information
Using FacesMessage to Create a Message
Instead of registering messages in the application configuration resource file, you can ac-
cess the java.util.ResourceBundle directly from managed bean code. The code
snippet below locates an email error message:
Click here to view code image
String message = "";
...
message = ExampleBean.loadErrorMessage(context,
ExampleBean.EX_RESOURCE_BUNDLE_NAME,
"EMailError");
context.addMessage(toValidate.getClientId(context),
new FacesMessage(message));
These lines call the bean's loadErrorMessage method to get the message from the
ResourceBundle . Here is the loadErrorMessage method:
Click here to view code image
public static String loadErrorMessage(FacesContext context,
String basename, String key) {
if ( bundle == null ) {
try {
bundle = ResourceBundle.getBundle(basename,
context.getViewRoot().getLocale());
} catch (Exception e) {
return null;
}
}
return bundle.getString(key);
}
Referencing Error Messages
A JavaServer Faces page uses the message or messages tags to access error messages,
as explained in “Displaying Error Messages with the h:message and h:messages Tags” in
The Java EE 6 Tutorial: Basic Concepts .
The error messages these tags access include:
• The standard error messages that accompany the standard converters and validators
that ship with the API. See Section 2.5.2.4 of the JavaServer Faces specification
for a complete list of standard error messages.
Search WWH ::




Custom Search