10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Performing Localization<br />

390<br />

<strong>The</strong> Object instance typically contains the substitution parameters that are embedded in the<br />

message. For example, the custom validator described in “Implementing the Validator<br />

Interface” on page 397 will substitute the format pattern for the {0} in this error message:<br />

Input must match one of the following patterns {0}<br />

“Implementing the Validator Interface” on page 397 gives an example of accessing messages.<br />

Using FacesMessage to Create a Message<br />

Instead of registering messages in the application configuration resource file, you can access the<br />

ResourceBundle directly from the code. <strong>The</strong> validateEmail method from the Coffee Break<br />

example does this:<br />

...<br />

String message = "";<br />

...<br />

message = CoffeeBreakBean.loadErrorMessage(context,<br />

CoffeeBreakBean.CB_RESOURCE_BUNDLE_NAME,<br />

"EMailError");<br />

context.addMessage(toValidate.getClientId(context),<br />

new FacesMessage(message));<br />

...<br />

<strong>The</strong>se lines also call the loadErrorMessage to get the message from the ResourceBundle. Here<br />

is the loadErrorMessage method from CoffeeBreakBean:<br />

public static String loadErrorMessage(FacesContext context,<br />

String basename, String key) {<br />

if ( bundle == null ) {<br />

try {<br />

bundle = ResourceBundle.getBundle(basename,<br />

context.getViewRoot().getLocale());<br />

} catch (Exception e) {<br />

return null;<br />

}<br />

}<br />

return bundle.getString(key);<br />

}<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!