aspose file tools*
The moose likes Struts and the fly likes Using Properties File in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Properties File in struts" Watch "Using Properties File in struts" New topic
Author

Using Properties File in struts

yash Vi
Ranch Hand

Joined: Jul 17, 2005
Posts: 41
Please can someone help me how to load a properties file of my own in struts and use it.

I am using the following code in action classs,

Properties prop=new Properties();
InputStream is=ClassLoader.getSystemClassLoader().getResourceAsStream("\\phaseOneEAR\\web\\WEB-INF\\classes\\TabMapping.properties");
prop.load(is);
String s=prop.getProperty("AccountSummary");

Is it the right approach,shd I set any param in web.xml
Please held me


Thanks for your reply and time.<br />Windows 2000,j2sdk1.4.2_08
Manuel Sanchez Iba�ez
Greenhorn

Joined: Jul 12, 2005
Posts: 20
This is my code:

Properties config = new Properties();
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("db.properties");
config.load(is);

db.properties is in /WEB-INF/db.properties
Aash Patel
Greenhorn

Joined: Sep 14, 2005
Posts: 24
This is how I did it:

String s = new String();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("ApplicationResources.properties");
Properties defaultProps = new Properties();
if( in != null ) {
defaultProps.load(in);
in.close();
dbConfigInfo = new ArrayList();
Enumeration i = defaultProps.keys();
while(i.hasMoreElements()) {
String tmp = (String)i.nextElement();
dbConfigInfo.add(new DbConfigInfoBean(tmp, defaultProps.getProperty(tmp)));
}
}
else {
log.fatal("Error: Couldn't locate ApplicationResources.properties file");
}

Make sure the above code is within try/catch blocks. DbConfigInfoBean is a bean of variables with getter and setter methods.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Properties File in struts