Sam*_*ami 2 java jsf properties
我有两个属性文件,但有些错误,inputStream总是为空?
<application>
<resource-bundle>
<base-name>resources/Bundle</base-name>
<var>bundle</var>
</resource-bundle>
<locale-config>
<default-locale>fi</default-locale>
<supported-locale>fi</supported-locale>
</locale-config>
<resource-bundle>
<base-name>resources/avainsanat</base-name>
<var>avainsanat</var>
</resource-bundle>
</application>
public static List getAvainsanat() throws IOException {
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("avainsanat.properties");
Properties properties = new Properties();
List<String> values = new ArrayList<>();
System.out.println("InputStream is: " + input);
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
values.add(value);
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
甚至可以在faces-config中有两个或更多属性文件吗?如果没有,我怎样才能从我的包中读取哪些键具有前缀key_的属性?
谢谢萨米
您忘记resources在路径中包含该包.上下文类加载器始终相对于类路径根搜索.
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/avainsanat.properties");
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,更正确的方法是使用ResourceBundle#getBundle(),这也正是JSF在封面下使用的<resource-bundle>:
ResourceBundle bundle = ResourceBundle.getBundle("resources.avainsanat", FacesContext.getCurrentInstance().getViewRoot().getLocale());
// ...
Run Code Online (Sandbox Code Playgroud)
(注意你实际上应该使用过a <base-name>resources.avainsanat</base-name>)
或者,如果bean是请求作用域,您也可以只注入#{avainsanat}托管属性:
@ManagedProperty("#{avainsanat}")
private ResourceBundle bundle;
Run Code Online (Sandbox Code Playgroud)
或者以编程方式评估它:
FacesContext context = FacesContext.getCurrentInstance();
ResourceBundle bundle = context.getApplication().evaluateExpressionGet(context, "#{avainsanat}", ResourceBundle.class);
// ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1756 次 |
| 最近记录: |