How to make property files available to all classes (not just Action classes) in struts2

628 Views Asked by At

I am working on the web application using Struts2 framework. I have a task to access the values from the property files where the application specific values are configured. I know that it can be accessed using Struts Resource Bundle, by defining the i18n resources in struts.xml file and the values can be obtained using getText method, by extending ActionSupport class. This way of accessing the property is applicable for Action classes or in JSP.

But, I want to get the property in the Business class for one of the validations. For ex, there is a property maxCount=4. One of the business validations is to check if its more than maxCount, which is available in the property file. I do not want to load the properties on my own. Instead, the struts should load the properties and I need to get it from there. For time being, I have written a static method to load and get the properties, which I think, its wrong way of doing.

Can you please guide me?

Note for people who marked it as duplicate:

I checked the duplicate question and the question was to know where to keep the property file so that it will be accessible by the app. My question is entirely different. I have already achieved in getting the properties, which has been loaded in the classpath. I wanted to know the perfect and standard way of doing it in Struts. I wanted to know the similar way of doing how its done using i18n. I have attached the code below that I am currently using. I clearly have explained that I need the guidance on the technical standards and not how to do it.

public class CFAProperties {
private static Properties props = new Properties();

private static Properties load() {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        props.load(classLoader.getResourceAsStream("resources/cfa-config.properties"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return props;
}

public static String getProp(String key) {
    load();
    return props.getProperty(key);
}}
1

There are 1 best solutions below

0
On

My answer does not address your question as i think you must use different tools for your application parameters.

There is no predefined way to manage the application configuration files in struts, although you can miss-use the struts internal message resources, but it is not a good way.

I suggest to use a light frame work at apache https://commons.apache.org/proper/commons-configuration/.

  1. It can read application configuration parameters from : Property Files, XML, System Settings, INI files
  2. It can convert the application parameters to types and objects like: integer, float, String,... You can even get parameters as List
  3. The expression are supported, so you can have this: user.file = ${expr:System.getProperty("user.home"}/settings.xml ( change applicaiton setting path based on other properties.
  4. You can build configuration files.

You can easily integrate it in you application and access to your parameters any where.