Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

struts2 - Where to put a properties file in Struts 2?

I have a property file placed in the root of the web project in Java. I am using Struts 2. My code is unable to read properties file. Where should I keep my properties file?

I have checked default path , it is where my Eclipse in installed. But I want that system should read file from project folder itself.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Usually you should put the properties file to the src folder so your application is able to read the properties file when you do run your application the properties file is copied from the src folder to the classes folder. As far as you know the classes folder should be the project output folder, so it will be used as a classpath folder and application is able to load the properties file if it is on the classpath.

An example to get properties from the classpath:

Properties prop = new Properties();

try {
  //load properties from the class path
  prop.load(this.getClass().getClassLoader().getResourceAsStream("myproperties.properties"));

  //get the property 
  System.out.println(prop.getProperty("mykey"));

} catch (IOException ex) {
  ex.printStackTrace();
  throw ex;
}

However, you can load properties if you know the path to the file on the filesystem, in this case use

prop.load(new FileInputStream("/path/to/myproperties.properties"));

If you are talking about struts.properties

The framework uses a number of properties that can be changed to fit your needs. To change any of these properties, specify the property key and value in an struts.properties file. The properties file can be locate anywhere on the classpath, but it is typically found under /WEB-INF/classes.

If you are looking for Message Resource properties it could be configured in the struts.properties or struts.xml the later is proffered.

<constant name="struts.custom.i18n.resources" value="path/to/resources/MessageResources"/>

the value is a filepath src/path/to/resources/MessageResources.properties

If you are looking for the proper way to configure your application consider the choice to use EasyConf.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...