Where to put a properties file in Struts 2?

后端 未结 4 379
春和景丽
春和景丽 2020-12-16 04:58

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?

相关标签:
4条回答
  • 2020-12-16 05:26

    Property files will generally either go:

    1. on the the classpath, e.g., for opening as a resource, or
    2. at a location inaccessible to clients, e.g., under /WEB-INF

    Which is more appropriate depends on your needs. Classpath-based files allow bundled default property files without configuration. For example, Log4J will look for log4j.properties at the root of the classpath as its default configuration file.

    This can occasionally lead to issues, however, depending on class loading order: sometimes the system can pick up a "stray" config file. Configuring it yourself may still be preferable; I still tend towards the classpath, but config files are also commonly found in WEB-INF. Either method works, and both styles can be configured using JNDI, init params, environment variables, or system variables (e.g., -D).

    0 讨论(0)
  • 2020-12-16 05:35

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

      Properties prop = new Properties();
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      prop.load(classLoader.getResourceAsStream("/myPropertyFile.properties"));
    
    0 讨论(0)
  • 2020-12-16 05:38

    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.

    0 讨论(0)
  • 2020-12-16 05:38

    Ideally, you can save properties files in: /src/com/cft/web/bundle/. like, /src/com/cft/web/bundle/LabelResources.properties OR /src/com/cft/web/bundle/Applicationresources.properties.

    in fact its up to you to give whatever the path you like.

    Just remember to add the correct complete path in 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"/>

    0 讨论(0)
提交回复
热议问题