如何处理JSF类中的资源?

8bi*_*kie 3 resources jsf properties classpath

src/main/resources在我的JSF项目中放了一个属性文件.

我该如何处理它?我知道EL在辅助bean中不起作用.

注意:该文件位于src/main/resources- NOT src/main/webapps/resources,因此以下内容不起作用:

FacesContext context = FacesContext.getCurrentInstance();
File value = context.getApplication().evaluateExpressionGet(context, "#{resource['resources:email.properties']}", File.class);
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 7

因此它在类路径中.你可以ClassLoader#getResourceAsStream()用来摆脱InputStream它.假设这src是类路径根,那main/resources就是包:

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

或者,如果它应该是特定于webapp的,因此不应该被类路径中具有更高类加载优先级的其他地方的相同路径上的文件覆盖(例如在appserver的lib或JRE的lib中),然后使用ExternalContext#getResourceAsStream()代替.

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

至于#{resource}语法,这确实专门用于放置在/resources公共Web内容文件夹中的CSS/JS /图像资源.另请参见如何在Facelets模板中引用CSS/JS /图像资源?