You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by SimonK <sl...@gmail.com> on 2007/07/19 06:42:47 UTC

Setting application properties

Hi all,

I am new to struts, so please forgive me if this question has an obvious
answer.

I want to be able to define some global settings for my application, such
as:

- the path to the applications temporary directory;
- the path to a static (input) data directory;
- the location/name of a db.

This (and other such constants) will be required by multiple actions and
Inteceptors.

Ideally I'd like this information to be separate to the web.xml and
struts.xml files, and in "key = value" form (... exactly like that of a
property file).

What is the canonical way of doing this?

Note - I'm having trouble findng information on how to deal with property
files.

Cheers,
Simon.
-- 
View this message in context: http://www.nabble.com/Setting-application-properties-tf4108144.html#a11682293
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Setting application properties

Posted by bartlebooth <ba...@telenet.be>.
You could put a context parameter into your web.xml

        <context-param>
            <param-name>tmpDir</param-name>
            <param-value>/tmp/test</param-value>
        </context-param>

If your action is ServletContextAware (you implement the setter public 
void setServletContext(ServletContext servletContext)),
then you can access this parameter using

servletContext.getContext(your_application_context_name).getInitParameter("tmpDir")"

hope this helps

bartlebooth


SimonK wrote:
> Thanx for you reply.
>
> The 'problem' with this solution is that I then have to explicitly access
> the file each time I want a property. I was hoping that There was a place
> that I could load such parameters then simply access it.
>
> The things I have experimented with (and which have failed) are:
>
> attempt 1
> ---------
> place the line:
> <constant name="gov.brs.mapping.RegionSelector.tmpDirectory" value="/temp"
> />
>
> in struts.xml (just after <struts>).
>
> attempt 2
> ---------
> place the line:
> gov.brs.mapping.RegionSelector.tmpDirectory=/temp
>
> in struts.properties, which is in "WEB-INF/classes"
>
> In both cases, the class gov.brs.mapping.RegionSelector has static getter
> and setters for tmpDirectory, which is a static member. I was hoping that
> this would set tmpDirectory (a String) in gov.brs.mapping.RegionSelector.
> This does not happen.
>
>
>
> The above solutions would not be ideal in any case. What I would really like
> to know how to get struts to put key value pairs in the application map
> (which I *think* is the appropriate place) or atleast on the stack, when the
> application is loaded by tomcat, so I can write something like:
>
> String tempDir = ActionContext.getApplication().get("tempDirectory"); or
> String tempDir = ActionContext.getValueStack().get("tempDirectory");
>
> to get at it.
>
> Can I do this... and if so, how?
>
> Cheers again,
> Simon.
>   


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


Re: Setting application properties

Posted by Richard Sayre <ri...@gmail.com>.
You could read the global properties from a file or DB when the
application first loads into an object of your choice (A Map for
Key,Value) .  Then put this object in session.

On 7/19/07, SimonK <sl...@gmail.com> wrote:
>
> Thanx for you reply.
>
> The 'problem' with this solution is that I then have to explicitly access
> the file each time I want a property. I was hoping that There was a place
> that I could load such parameters then simply access it.
>
> The things I have experimented with (and which have failed) are:
>
> attempt 1
> ---------
> place the line:
> <constant name="gov.brs.mapping.RegionSelector.tmpDirectory" value="/temp"
> />
>
> in struts.xml (just after <struts>).
>
> attempt 2
> ---------
> place the line:
> gov.brs.mapping.RegionSelector.tmpDirectory=/temp
>
> in struts.properties, which is in "WEB-INF/classes"
>
> In both cases, the class gov.brs.mapping.RegionSelector has static getter
> and setters for tmpDirectory, which is a static member. I was hoping that
> this would set tmpDirectory (a String) in gov.brs.mapping.RegionSelector.
> This does not happen.
>
>
>
> The above solutions would not be ideal in any case. What I would really like
> to know how to get struts to put key value pairs in the application map
> (which I *think* is the appropriate place) or atleast on the stack, when the
> application is loaded by tomcat, so I can write something like:
>
> String tempDir = ActionContext.getApplication().get("tempDirectory"); or
> String tempDir = ActionContext.getValueStack().get("tempDirectory");
>
> to get at it.
>
> Can I do this... and if so, how?
>
> Cheers again,
> Simon.
> --
> View this message in context: http://www.nabble.com/Setting-application-properties-tf4108144.html#a11684192
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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


Re: Setting application properties

Posted by SimonK <sl...@gmail.com>.
Thanx for you reply.

The 'problem' with this solution is that I then have to explicitly access
the file each time I want a property. I was hoping that There was a place
that I could load such parameters then simply access it.

The things I have experimented with (and which have failed) are:

attempt 1
---------
place the line:
<constant name="gov.brs.mapping.RegionSelector.tmpDirectory" value="/temp"
/>

in struts.xml (just after <struts>).

attempt 2
---------
place the line:
gov.brs.mapping.RegionSelector.tmpDirectory=/temp

in struts.properties, which is in "WEB-INF/classes"

In both cases, the class gov.brs.mapping.RegionSelector has static getter
and setters for tmpDirectory, which is a static member. I was hoping that
this would set tmpDirectory (a String) in gov.brs.mapping.RegionSelector.
This does not happen.



The above solutions would not be ideal in any case. What I would really like
to know how to get struts to put key value pairs in the application map
(which I *think* is the appropriate place) or atleast on the stack, when the
application is loaded by tomcat, so I can write something like:

String tempDir = ActionContext.getApplication().get("tempDirectory"); or
String tempDir = ActionContext.getValueStack().get("tempDirectory");

to get at it.

Can I do this... and if so, how?

Cheers again,
Simon.
-- 
View this message in context: http://www.nabble.com/Setting-application-properties-tf4108144.html#a11684192
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Setting application properties

Posted by Nuwan Chandrasoma <my...@gmail.com>.
Hi,

Add a property file to your classpath, read it from your java code. this is 
what i got from google :)

http://www.exampledepot.com/egs/java.util/Props.html

http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html?page=1

Thanks,

Nuwan


----- Original Message ----- 
From: "SimonK" <sl...@gmail.com>
To: <us...@struts.apache.org>
Sent: Thursday, July 19, 2007 4:42 AM
Subject: Setting application properties


>
> Hi all,
>
> I am new to struts, so please forgive me if this question has an obvious
> answer.
>
> I want to be able to define some global settings for my application, such
> as:
>
> - the path to the applications temporary directory;
> - the path to a static (input) data directory;
> - the location/name of a db.
>
> This (and other such constants) will be required by multiple actions and
> Inteceptors.
>
> Ideally I'd like this information to be separate to the web.xml and
> struts.xml files, and in "key = value" form (... exactly like that of a
> property file).
>
> What is the canonical way of doing this?
>
> Note - I'm having trouble findng information on how to deal with property
> files.
>
> Cheers,
> Simon.
> -- 
> View this message in context: 
> http://www.nabble.com/Setting-application-properties-tf4108144.html#a11682293
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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