You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mark Silva <ms...@authenex.com> on 2002/09/09 21:07:14 UTC

Simple Question

This is probably a simple question, but i cannot find the answer in any straighforward place.

how do i get a property from my ApplicationResources.properties file within some java code.  i know how to do this using a bean tag, but not otherwise.  is there a helper class to do this?  

i am using this to assign default values for a form (within a form object).

thanks,
mark

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Simple Question

Posted by micael <ca...@harbornet.com>.
One last thing, I would not use your ApplicationResources.properties file 
to do this.  Why not use another properties file?  Maybe there is a good 
reason to do that and I will learn something here.

At 12:58 PM 9/9/2002 -0700, you wrote:
>You might also note that you can have default values in an included 
>property file.  See Properties API
>
>At 12:07 PM 9/9/2002 -0700, you wrote:
>>This is probably a simple question, but i cannot find the answer in any 
>>straighforward place.
>>
>>how do i get a property from my ApplicationResources.properties file 
>>within some java code.  i know how to do this using a bean tag, but not 
>>otherwise.  is there a helper class to do this?
>>
>>i am using this to assign default values for a form (within a form object).
>>
>>thanks,
>>mark
>>
>>--
>>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Simple Question

Posted by micael <ca...@harbornet.com>.
Just write a helper class that opens the file, loads it into a properties 
object, reads the properties, and closes the file.  Something like the 
following:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public final class SilvaProperties {

     public SilvaProperties setProperty(String file, String key, String 
value) {
                                  key  = Replace.replace(key, " ", "");
         File                   hold  = new File(file);
         Properties  properties = new Properties();
         try {
             FileInputStream input = new FileInputStream(hold);
             properties.load(input);
             properties.setProperty(key, value);
             FileOutputStream output= new FileOutputStream(hold);
             properties.store(output, " -- TRESBEAU -- ");
             output.close();
             input.close();
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }
         return this;
     }

     public String getProperty(String file, String key) {
                                  key  = Replace.replace(key, " ", "");
         File                   hold  = new File(file);
         Properties  properties = new Properties();
         try {
             FileInputStream input = new FileInputStream(hold);
             properties.load(input);
             input.close();
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }
         return properties.getProperty(key);
     }

     public Properties getProperties(String file) {
         Properties   properties = new Properties();
         File            hold       = new File(file);
         try {
             FileInputStream input  = new FileInputStream(hold);
             properties.load(input);
             input.close();
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }
         return properties;
     }


         public final class Replace {
                 public synchronized static String replace(String content, 
String before, String after) {
                                 int position = content.indexOf(before);
                                 while (position > -1) {
                                 content  = content.substring(0, position) 
+ after + content.substring(position + before.length());
                                 position = content.indexOf(before, 
position + after.length());
                                 }
                         return content;
                 }
         }
}
}

At 03:21 PM 9/9/2002 -0400, you wrote:
>Are doing this within an Action?
>
>
>James Mitchell
>Software Engineer\Struts Evangelist
>Struts-Atlanta, the "Open Minded Developer Network"
>http://www.open-tools.org/struts-atlanta
>
>
>
>
> > -----Original Message-----
> > From: Mark Silva [mailto:msilva@authenex.com]
> > Sent: Monday, September 09, 2002 3:07 PM
> > To: Struts Users Mailing List
> > Subject: Simple Question
> >
> >
> > This is probably a simple question, but i cannot find the answer
> > in any straighforward place.
> >
> > how do i get a property from my ApplicationResources.properties
> > file within some java code.  i know how to do this using a bean
> > tag, but not otherwise.  is there a helper class to do this?
> >
> > i am using this to assign default values for a form (within a
> > form object).
> >
> > thanks,
> > mark
> >
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
> >
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Simple Question

Posted by James Mitchell <jm...@telocity.com>.
Are doing this within an Action?


James Mitchell
Software Engineer\Struts Evangelist
Struts-Atlanta, the "Open Minded Developer Network"
http://www.open-tools.org/struts-atlanta




> -----Original Message-----
> From: Mark Silva [mailto:msilva@authenex.com]
> Sent: Monday, September 09, 2002 3:07 PM
> To: Struts Users Mailing List
> Subject: Simple Question
> 
> 
> This is probably a simple question, but i cannot find the answer 
> in any straighforward place.
> 
> how do i get a property from my ApplicationResources.properties 
> file within some java code.  i know how to do this using a bean 
> tag, but not otherwise.  is there a helper class to do this?  
> 
> i am using this to assign default values for a form (within a 
> form object).
> 
> thanks,
> mark
> 
> --
> To unsubscribe, e-mail:   
> <ma...@jakarta.apache.org>
> For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
> 
> 

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>