4

I've put a properties file within src/main/resources in my JSF project.

How do I get a handle to it? I understand that EL doesn't work within a backing bean.

Note: The file is in src/main/resources - NOT src/main/webapps/resources, so the following doesn't work:

FacesContext context = FacesContext.getCurrentInstance();
File value = context.getApplication().evaluateExpressionGet(context, "#{resource['resources:email.properties']}", File.class);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
8bitjunkie
  • 12,793
  • 9
  • 57
  • 70

1 Answers1

8

It's thus in the classpath. You can just use ClassLoader#getResourceAsStream() to get an InputStream out of it. Assuming that src is the classpath root and that main/resources is the package:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("main/resources/foo.properties");
Properties properties = new Properties();
properties.load(input);
// ...

Alternatively, if it's supposed to be specific to the webapp and thus isn't supposed to be overrideable by a file on the same path elsewhere in the classpath which has a higher classloading precedence (e.g. in appserver's lib or the JRE's lib), then use ExternalContext#getResourceAsStream() instead.

InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("main/resources/foo.properties");
Properties properties = new Properties();
properties.load(input);
// ...

As to the #{resource} syntax, this is indeed specifically for CSS/JS/image resources placed in /resources folder of public web content. See also How to reference CSS / JS / image resource in Facelets template?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Superb! Thanks once again BalusC. – 8bitjunkie May 14 '12 at 20:58
  • I think I have something wrong with my TomCat 7 install - this is actually coming up with a Null Pointer Exception and was not working as I expected? I have tried a leading slash and this does not work either. My JSF application has the usual Maven archetype and the path to the file is src/main/resources/foo.properties – 8bitjunkie May 14 '12 at 21:29
  • 1
    Oh, then I believe you need `getResourceAsStream("foo.properties")` instead. Or maybe `getResourceAsStream("resources/foo.properties")`. Depends on how Maven builds the WAR, I can't tell that from top of head as I don't use it. Just read Maven docs or extract the WAR and explore `/WEB-INF/classes` to learn the build structure. Leading slash should not be used in the thread's context classloader, it'll always return `null` then. It's optional in the webapp resource loader but using it would always expect it to be a public webcontent resource. – BalusC May 14 '12 at 21:30
  • InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("/foo.properties") worked. The example with FacesContext didn't. Thanks! – 8bitjunkie May 14 '12 at 21:34
  • No? Well, that'll be something Mavenish. Anyway, you're welcome again :) – BalusC May 14 '12 at 21:35