struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Heikki Hyyrö <heikki.hy...@uta.fi>
Subject Re: Problem reading a resource file (migrating 2.3.29 --> 2.5.10)
Date Sat, 11 Mar 2017 23:30:04 GMT
11.03.2017, 09:20, Lukasz Lenart kirjoitti:
> This is do the changes in ActionSupport's dependencies, you must
> inject them first to have access to resource bundles.
>
> Instead your approach with singleton you can use something like this
>
> public class DbConnectionManager {
>
>    public DbConnectionManager(@Inject TextProvider textProvider) {
>      String driverNames = textProvider.getText("database.drivers");
>      ...
>    }
> }
>
> and then
>
> public class MyAction extends ActionSupport {
>
>    public String execute() {
>      DbConnectionManager mgr = container.inject(DbConnectionManager.class)
>      ....
>    }
> }
>
> but perhaps the best option would be to disconnect your DAO layer from
> Web layer, you have coupled two different layers which is a "bad
> design" ™
Thanks, I will have a look at this.

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?

-Heikki

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


Mime
View raw message