简体   繁体   中英

Getters/Setters for java.util.properties

I want display properties in HTML/XML via jsp. Like as:

${MyClass.properties.propertieOne}

I created extended class of properties, MProperties, but how can I create getters in that class for my properties?

BR Kolesar

You can use store(Writer writer, String comments) method in Properties class. Write your properties to a StringWriter and use String from it to print on the HTML.

${yourObject.properties.propertyOne} works. Properties extends Hashtable implement Map and ${map.key} gives you the value under that key. But you are not supposed to use a setter from a JSP, so this is only for display purposes.

But you can't access it directly from a static field. You have to add it to some context - request or application. For example, in a ServletContextListener.contextInitialized(..) :

servletContext.addAttribute("yourProperties", MyClass.properties);

(then you'd have to map the <listener> in web.xml, of course)

If you extended Properties , you probably know exactly which fields you want to have. In that case, it would seem to be better to just a create a POJO (simple object) with those fields and the appropriate getters (and setters and/or constructor as you wish). If you somehow need the dynamic-ness (?) of Properties , ignore this answer.

you can create class to handle your properties with constructor like

private Properties props = null;

    private MyProperties() throws IOException {

        FileInputStream propFile = new FileInputStream(FULL_PATH);
        props = new Properties(System.getProperties());
        props.load(propFile);

        RegistryManager rm = RegistryManager.singleton();
        rm.addRegistry("MyProperty", this);
    }

public static MyProperties Singelton() {
        synchronized (MyProperties.class) {
            if (theInstance == null) {
                try {
                    theInstance = new MyProperties();
                } catch (IOException e) {
                    throw new MissingResourceException("Unable to load property file \"" + FULL_PATH + "\"", MyProperties.class.getName(),
                            PROPERTIES_FILENAME);
                }
            }
        }
        return theInstance;
    }

and than get properties by one method like

public static String getProperty(String propertyName) {
        String value = Singelton().props.getProperty(propertyName);
        if (value == null) {
            LOGGER.warning("propertyName (" + propertyName + ") not found in property file (" + FULL_PATH + ")");
        }

        return value;
    }

finally in code you can call only

String desiredProperty = MyProperties.getProperty("propertyKey");

some code is missing and some you maybe wont need but you should get the idea if this is what you wanted to do...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM