Where to put the properties file in Struts 2?

I have a properties file placed at the root of a web project in Java. I am using Struts 2. My code cannot read the properties file. Where should I store the properties file?

I checked the default path, my Eclipse is installed there. But I want this system to read the file from the project folder itself.

+6
source share
4 answers

Property files usually either go:

  • in the classpath, for example, to open as a resource or
  • in a place inaccessible to customers, for example, under /WEB-INF

Which is more suitable depends on your needs. Classpath-based files allow you to add default property files without configuration. For example, Log4J will look for log4j.properties in the root of the path as the default configuration file.

This can sometimes lead to problems, however, depending on the order in which the classes are loaded: sometimes the system can pick up a stray configuration file. Its configuration in itself may be preferable; I'm still leaning toward class paths, but configuration files are also commonly found on WEB-INF . Any of these methods work, and both styles can be customized using JNDI, initialization parameters, environment variables, or system variables (e.g. -D ).

+5
source

Usually you should put the properties file in the src folder so that your application can read the properties file when the application starts, 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's output folder, so it will be used as the classpath folder, and the application can load the properties file if it is on the classpath .

An example of getting properties from a class path:

 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 file path in the file system, in this case use

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

If you are talking about struts.properties

The structure uses a number of properties that can be changed to suit your needs. To change any of these properties, specify the property key and value in the struts.properties file. The properties file can be found anywhere in the class path, but it is usually located in / WEB -INF / classes.

If you are looking for message resource properties, you can configure it in struts.properties or struts.xml , which is proposed later.

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

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

If you're looking for the right way to customize your application, consider using EasyConf .

+4
source

Save the myPropertyFile.properties file in the src folder (after the build project you will find it in WEB-INF / classes) and get access to them using this code:

  Properties prop = new Properties(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); prop.load(classLoader.getResourceAsStream("/myPropertyFile.properties")); 
+4
source

Ideally, you can save the properties files to: / SRC / COM / PFT / web / kit /. like, /src/com/cft/web/bundle/LabelResources.properties OR /src/com/cft/web/bundle/Applicationresources.properties.

in fact it is up to you to give any path that you like.

Remember to add the correct full path to web.xml / struts-config.xml

for ex = g. 1. in web.xml:

  <description>Application Resources</description> <env-entry-name>ApplicationResources</env-entry-name> <env-entry-value>com.cft.web.bundle.ApplicationResources</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> 
  1. In struts-config.xml

 <message-resources parameter="com.cft.web.bundle.LabelResources" key="yourPropertiesFileName"/> 
-1
source

All Articles