我正在使用
struts.custom.i18n.resources=file1,file2
file1(ApplicationResources1.properties)
enter.user = User name
file2(ApplicationResources2.properties)
enter.user = User name1
根据某些情况,我需要将文件从 file1 切换到 file2。? 我使用了以下代码...两个文件都已加载,但我需要根据条件引用这些文件中的任何一个...
Struts.xml
<constant name="struts.custom.i18n.resources"
value="ApplicationResources2,ApplicationResources1" />
Test.java
if(condition){
LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources1.properties");
LocalizedTextUtil.setReloadBundles(true);
}else{
LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources2.properties");
LocalizedTextUtil.setReloadBundles(true);
}
最佳答案
如果您的属性文件名称是 ApplicationResources1.properties
和 ApplicationResources2.properties
那么您需要在 addDefaultResourceBundle
方法中使用这些名称 LocalizedTextUtil
.
if(condition) {
LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources1");
LocalizedTextUtil.setReloadBundles(true);
}else {
LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources2");
LocalizedTextUtil.setReloadBundles(true);
}
关于java - struts2 中的多个应用程序属性,需要根据某些条件更改 java 中的属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161583/