How to handle a resource in the JSF class?

I put the properties file in src/main/resourcesto my JSF project.

How do I get a pen? I understand that EL does not work within a bean.

Note: The file is in src/main/resources- NOT src/main/webapps/resources, so the following does not work:

FacesContext context = FacesContext.getCurrentInstance();
File value = context.getApplication().evaluateExpressionGet(context, "#{resource['resources:email.properties']}", File.class);
+3
source share
1 answer

Thus, on the way to classes. You can just use ClassLoader#getResourceAsStream()to get InputStream. Assuming what srcis the root of the classpath and what main/resourcesis the package:

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

, webapp , , , (, appserver lib JRE lib), ExternalContext#getResourceAsStream().

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

#{resource}, CSS/JS/image, /resources -. . CSS/JS/image Facelets?

+7

All Articles