You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Lukas Bradley <lu...@somnia.com> on 2005/04/22 18:34:06 UTC

Static Access to MessageResources or Bundles?

Is there any way to statically access fields from resources defined in 
Struts?

In other words, I would to be able to access terms stored in properties 
files without needing a ServletRequest.  My application exists within a 
servlet container, and utilizes Struts.  However, I have processes 
running that are independant of incoming requests.

Any ideas?


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


Re: Static Access to MessageResources or Bundles?

Posted by Joe Germuska <Jo...@Germuska.com>.
At 12:34 PM -0400 4/22/05, Lukas Bradley wrote:
>Is there any way to statically access fields from resources defined in Struts?
>
>In other words, I would to be able to access terms stored in 
>properties files without needing a ServletRequest.  My application 
>exists within a servlet container, and utilizes Struts.  However, I 
>have processes running that are independant of incoming requests.

The commons-resources project is intended to provide a generalized 
framework for handling messages like this; it was factored out of 
Struts, and one day, perhaps Struts will depend upon it in favor of 
the original implementations.

I don't think commons-resources has seen a full 1.0 release yet, but 
it's usable.

The Spring Framework also provides a generalized message resource 
handling mechanism which is well suited to use independently from the 
Servlet API.  Spring, of course, has seen a full release, and is 
closing in on its 1.2 release.  Of course, it carries with it a lot 
more than just message handling, but you can selectively install 
sub-jars if you don't want to carry around the whole thing (or risk 
your team depending on some part of it that you aren't ready to 
depend upon just yet.)

Either of these would require some more set-up to make the resources 
available, but either should be fairly straightforward to implement. 
Spring makes it trivial to provide your business layer classes with 
implementations of the resource loader, as long as you let it manage 
those classes.

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

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


Re: Static Access to MessageResources or Bundles?

Posted by Joe Germuska <Jo...@Germuska.com>.
At 12:51 PM -0400 4/22/05, Lukas Bradley wrote:
>>Is there any way to statically access fields from resources defined 
>>in Struts?
>
>Well, I have a solution.
>
>String bundle = "common" ;
>String key = "date" ;
>MessageResources.getMessageResources(bundle).getMessage(Locale.EN, key);
>
>This is very straightforward.
>
>So why does Struts prefer to store and access the resources through 
>the ServletContext?  Is this faster?

Because at least in theory, you have no guarantee that the Message 
Resources are coming from a properties file.  Some people want to be 
able to get them from a database, or from an XML file.  These 
features aren't used all that often, but the design is to support 
them.

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

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


Re: Static Access to MessageResources or Bundles?

Posted by Lukas Bradley <lu...@somnia.com>.
> Is there any way to statically access fields from resources defined in 
> Struts?

Well, I have a solution.

String bundle = "common" ;
String key = "date" ;
MessageResources.getMessageResources(bundle).getMessage(Locale.EN, key);

This is very straightforward.

So why does Struts prefer to store and access the resources through the 
ServletContext?  Is this faster?


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


Re: Static Access to MessageResources or Bundles?

Posted by Michael Jouravlev <jm...@gmail.com>.
Search for org.apache.struts.util.MessageResources	
I guess this should work:

MessageResourcesFactory messageFactory = 
  MessageResourcesFactory().createFactory();

MessageResources messages = 
  messageFactory.createResources();

Michael

On 4/22/05, Lukas Bradley <lu...@somnia.com> wrote:
> Is there any way to statically access fields from resources defined in
> Struts?
> 
> In other words, I would to be able to access terms stored in properties
> files without needing a ServletRequest.  My application exists within a
> servlet container, and utilizes Struts.  However, I have processes
> running that are independant of incoming requests.
> 
> Any ideas?

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


Re: Static Access to MessageResources or Bundles?

Posted by Je...@bcbstx.com.



Maybe a generic servlet style scheme similar to this:

web.xml

    <init-param>
       <param-name>properties</param-name>

<param-value>/path/to/properties/file/webapp.properties</param-value>
    </init-param>

use servlet config instance at servlet init() to load (once):

props =
(Properties)config.getServletContext().getAttribute("webapp.properties");

then retrieve (as needed):

props.getProperty(key);

-jeff


 I would to be able to access terms stored in properties
files without needing a ServletRequest.



------------------------------------------------------------------------------
**********
The information contained in this communication is confidential, private, proprietary, or otherwise privileged and is intended only for the use of the addressee.  Unauthorized use, disclosure, distribution or copying is strictly prohibited and may be unlawful.  If you have received this communication in error, please notify the sender immediately at (312)653-6000 in Illinois; (972)766-6900 in Texas; or (800)835-8699 in New Mexico.
**********
==============================================================================


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