Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Re: Load MessageResource from custom file path

Laurie Harper

2007-02-27

Replies:

The ususal way to expose resource bundles for use with Struts1 is to
include the properties files in the webapp's classpath, configure them
in struts-config.xml and let Struts deal with loading them. Is there a
reason you need to store the files external to the webapp and load them
manually?

If you really do need to do that, you'll have to have your loading code
take care of loading the properties into the appropriate bundle type too.

L.

Andrzej Bengner | e-direct Polska Sp. z o.o. wrote:
> Hello
>
> I can not load MessageResources from my custom file path. I can load for
> <fmt:message/> taglib, but how can I do with <bean:message/>?
>
> For example, here is my Struts action snippet:
>
> URL[] urls = new URL[1];
> try
> {
>  urls[0] = new URL("file:c:/my_dir/messages/");
> }
> catch (MalformedURLException e)
> {
>  log.error("Warning...");
> }
> URLClassLoader loader = new URLClassLoader(urls);
> ResourceBundle bundle = ResourceBundle.getBundle("MessageResources", new
> Locale("en"), loader);
> javax.servlet.jsp.jstl.core.Config.set(request.getSession(),
> javax.servlet.jsp.jstl.core.Config.FMT_LOCALIZATION_CONTEXT, new
> LocalizationContext(bundle ,new Locale("en")));
>
> Of course I have file c:/my_dir/messages/MessageResources_en.properties
> And in my jsp I can do this:
>
> <fmt:message key="my.key"/>
>
> But if I add this to my action:
>
> request.setAttribute(Globals.MESSAGES_KEY, bundle);
> request.getSession().setAttribute(Globals.LOCALE_KEY, new Locale("en"));
>
> And this in jsp:
>
> <bean:message key="my.key"/>
>
> Then I get from jsp:
> java.lang.ClassCastException: java.util.PropertyResourceBundle
>
> Anybody knows, how can I set ResourceBundle for <bean:message/> taglib or
> how can I convert ResourceBundle to MessageResources?
>
> Thanx.
>
> Regards,
> Andy


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.