struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukasz Lenart <lukaszlen...@apache.org>
Subject Re: Problem reading a resource file (migrating 2.3.29 --> 2.5.10)
Date Sun, 12 Mar 2017 17:15:34 GMT
2017-03-12 0:30 GMT+01:00 Heikki Hyyrö <heikki.hyyro@uta.fi>:
> I now that the particular example was a bit of a hack. DBConnectionManager
> inherited ActionSupport only for the sole purpose of getting easy access to
> the resource files (just rely on the framework to find it; no need to hard
> code any path or filename). Changing this is not such a big deal; something
> like DbConnectionManager.class.getResourceAsStream("package.properties")
> should work.
>
> A related question is how to access resources from a static context. For
> example if I have an Enum whose constants are based on strings defined in
> the resource files, what would be a proper way to access them? I am thinking
> of something like:
>
> public Enum Event
> {
>     SOME_EVENT(0, "header.some_event"),
>     ANOTHER_EVENT(1, "header.another_event"),
>     ...
>
>     private final int eventId;
>     private final String eventString;
>
>     private LogEvent(int id, String description)
>     {
>         eventId = id;
>         // How to achieve the following conversion of a generic
>         // header into how its defined in the resource files?
>         eventString = getText(string);
>     }
> }
>
> Is there a nice way to handle this with getText, or should I again use
> getResourceAsStream or some other non-Struts facility?

In theory you can use LocalizedTextUtil and its findText() methods but
right now I'm working on converting this static util class into a bean
(because of many reasons) so you will have the same problem as with
DbConnectionManager at some point.

Another option is to use ActionContext but that can be only used in a
Enum's method and when that method will be called from within a Struts
action.

ActionContext.getContext().getContainer().getInstance(TextProvider.class).getText(...)


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message