0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
marius0114
  • 145
  • 4
  • 18

2 Answers2

0

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.

Yamada
  • 723
  • 6
  • 23
  • Thanks for your answer. But i don't find the config.properties file in your first code fragment, when i execute this in my PageServiceBean. My problem is the path, i write in the `prop.load(new FileInputStream("config.properties"));` – marius0114 Jan 23 '14 at 16:06
  • It will be created somewhere in your disk, the point is that it doesn't matter where it will be placed. If you need to do a backup, run a search in your disk to locate it. In a glassfish server, this file will be created in the path specified in the docroot parameter. – Yamada Jan 23 '14 at 17:07
  • Thank you. But i used a tomcat 7 server. – marius0114 Jan 23 '14 at 17:11
  • If you implemented my code, just call the setProperty1 method and run a search/locate/find in the config.properties file in your disk. I will have a look in the tomcat 7 server docs... – Yamada Jan 23 '14 at 17:14
  • Look at this question about configuration file for tomcat 7. It might allow you to specify a path: http://stackoverflow.com/questions/7276989/howto-set-the-context-path-of-a-web-application-in-tomcat-7-0 – Yamada Jan 23 '14 at 17:25
0

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you. But after i read the link below. I think for using the `prop.store()` function it's better to place the propertie file in the classpath. Do you believe that too? – marius0114 Jan 24 '14 at 11:51
  • On a fixed disk file system path. – BalusC Jan 24 '14 at 11:55
  • It will be deployed on a server. – marius0114 Jan 25 '14 at 10:48
  • I'm not sure what you're implying. Of course you would deploy a web application on a web server. – BalusC Jan 25 '14 at 10:51
  • I build the project as maven project. I read propertie-files are store in the src/main/resources folder. Can i read the file config2.properties with the code: `ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); prop.load(ec.getResourceAsStream("config2.properties"));` Or did i need a path? – marius0114 Feb 03 '14 at 20:26
  • [Here's the javadoc](http://docs.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html#getResourceAsStream(java.lang.String)). Read the description of the `path` parameter. – BalusC Feb 03 '14 at 20:31
  • Thanks. I read the javadoc and it tells me to write a "/" before the path. But it throws a null path. I store the propertie file in the `src/main/resources`path, but i also put the propertie file in the package name `jsfatwork.messages`. This means i must use the path `/jsfatwork/messages/config2.properties`. Correct? – marius0114 Feb 04 '14 at 16:45
  • I don't do Maven, but you should ensure that it ends up at the right location in the Maven-built WAR. The answer is based on the WAR structure, not on the build system you're using to produce the WAR. If the file doesn't end up in `/WEB-INF/resources/...etc`, then you need to fix/reconfigure the build system in such way that it does exactly that. The "how" is subject for a different question. Or, you must edit the question (and therefore cause the existing answers to be incompletely invalid and subject for deletion). – BalusC Feb 04 '14 at 16:55