Java Reference
In-Depth Information
This is actually a fake resource bundle (or, to put it more elegantly,
a decorator ), because it intercepts the call to
handleGetObject
( ) and
dynamically delegates to the “real” resource bundle using the class
name contained in the key. That's why
getKeys
( ) returns
null
:
MyRe-
sourceBundle
doesn't have any keys of its own.
If nothing is found when looking for a class name or for a key in
the class name bundle,
MyResourceBundle
looks in the default Stripes
resource bundle. This way, you can use
StripesResources
for text that
is not associated with any specific action bean. You can also put keys
without a class name and use them as a default for the whole appli-
cation, overriding them in action bean bundles if necessary. Using our
earlier example, where you had buttons labeled “Cancel” throughout
the application, but wanted to use “Abort” on the Register page, you
would have
cancel=Cancel
in
StripesResources
and
cancel=Abort
in the
stripesbook/action/RegisterActionBean
bundle.
We've already done most of the work to support our “modular” resource
bundles. We now have to tell Stripes to use
MyResourceBundle
. Although
packaged in
stripesbook.ext
,
MyResourceBundle
is not a Stripes extension,
just a custom
ResourceBundle
. The Stripes extension for changing the
ResourceBundle
implementation is the
LocalizationBundleFactory
interface,
which has a method for the form field bundle and one for the error
message bundle. We'll return
MyResourceBundle
for both:
package
stripesbook.ext;
public class
MyLocalizationBundleFactory
implements
LocalizationBundleFactory
{
public
ResourceBundle getFormFieldBundle(Locale locale) {
return new
MyResourceBundle(locale);
}
public
ResourceBundle getErrorMessageBundle(Locale locale) {
return new
MyResourceBundle(locale);
}
public void
init(Configuration configuration) { }
}
What about the free-form text, which is handled by the JSTL? How
do we tell the JSTL to use
MyResourceBundle
? We could do that (with
<fmt:setBundle>), but then we'd have to make sure to prefix every key
with the action bean class name, since the JSTL does not do that auto-
matically. That's too much work, isn't it? There's an easier way. We can
set the JSTL bundle to the class name of the current action bean with
a one-liner added to the end of
taglibs.jsp
.
Search WWH ::
Custom Search