Question

i want to read out from a propertie file in my jsf 2.2 project. i use eclipse kepler.

i try to use this in my java-bean in the folder src with the package de.exanple. The file of the bean is called PageServiceBean.java.

The propertie file is in the WEB-INF/resources/prop folder. The propertie file is called config.properties.

I have read that i can change the resouce folder in jsf 2.2 in the web.xml file with the javax.faces.WEBAPP_RESOUCRES_DIRECTORY param name and the param value like /WEB_INF/resoucres

But i don't get the path to the config file. Can you tell where i can get the path name. I think i must use a relativ path name. Can you please help me?

Update

I execute the second code fragment from you like:

private Properties getProperties() {
        Properties prop = new Properties();
        try {
            //load a properties file
            prop.load(new FileInputStream("config2.properties"));
        } catch(Exception e) {

        }
        return prop;
    }

    public void setProperty1(Integer value) {
        Properties prop = getProperties();
        prop.setProperty("ort", value.toString());
        try {
            prop.store(new FileOutputStream("config2.properties"), null);
            Properties prop2 = getProperties();                 
        } catch (IOException ex) {
            Logger.getLogger(PageServiceBean.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

It works! I use the Properties prop3 = getProperties(); to read the the propertie file config2.properties. The File is Store in the eclipse home path ECLIPSE_HOME = C:\elipse_jee\eclipse. Can i change the path into a specific path, like WEB_INF/resources?

Was it helpful?

Solution

I will show you my approach to your need, but I won't try to answer your question.

To use properties files in a JEE application I create a Stateless bean that serves the rest of the application with the getter and setter for the properties. Only this EJB will access the property file in the server and I use the java.util.Properties.

private Properties getProperties() {
    Properties prop = new Properties();
    try {
        //load a properties file
        prop.load(new FileInputStream("config.properties"));
    } catch(Exception e) {

    }
    return prop;
}

After I have the access methods for a specifc property:

public Integer getProperty1() {
    Properties prop = getProperties();
    String value = prop.getProperty("myProperty1Name");
    if(value != null) {
        return Integer.parseInt(value );
    }
    return 0;
}

public void setProperty1(Integer value) {
    Properties prop = getProperties();
    prop.setProperty("myProperty1Name", value.toString());
    try {
        prop.store(new FileOutputStream("config.properties"), null);
    } catch (IOException ex) {
        Logger.getLogger(PropertiesManager.class.getName()).log(Level.SEVERE, null, ex);
    }
}

In this approach, if the file doesn't exist it will be created. The default value of a property will be hard coded though. For this approach, it doesn't matter where your file is placed. The actual location will depend on your JEE server configuration, domain configuration, application deployment files, etc.

OTHER TIPS

Web content resources are available by ServletContext#getResourceAsStream() and its JSF delegator ExternalContext#getResourceAsStream(). So, this should do:

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
prop.load(ec.getResourceAsStream("/WEB-INF/resources/prop/config2.properties"));

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top