问题描述
我们将JSF2.0与JDK1.6和Tomcat6.1一起使用
We are using JSF2.0 with JDK1.6 and Tomcat6.1
我们需要在不重新启动服务器的情况下更新属性文件值(由JSF资源束加载),以免实时Web会话停止.
We have a requirement to update the property file values (loaded by JSF resource bundle) without restarting the server so that the live web sessions won't be stopped.
使用JDK1.6是否可行,我尝试了下面的clearCache代码,但没有用.
Is it possible with JDK1.6 , i tried the below clearCache code but it didn't work.
ResourceBundle bundle = ResourceBundle.getBundle("Label");
String s = bundle.getString("profile.firstName");
out.println("Value before: %"+ s);
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
bundle = ResourceBundle.getBundle("Label");
s = bundle.getString("profile.firstName");
out.println("Value after: {}"+s);
以前有人尝试过吗?
更新
以下内容似乎无法解决重新加载资源包的问题
The below doesn't seems to resolve the problem of reloading the resource bundle
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
ApplicationResourceBundle applicationBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("Label");
Field field = applicationBundle.getClass().getDeclaredField("resources");
field.setAccessible(true);
Map<Locale, ResourceBundle> resources = (Map<Locale, ResourceBundle>) field.get(applicationBundle);
resources.clear();
我想念什么吗?
推荐答案
这曾经用于某些JSF实现/版本.但是,在最新的Mojarra版本中,缓存机制在实现本身中增加了一层.假设您确实在使用Mojarra,除了该行
This used to work on some JSF implementations/versions. However, on more recent Mojarra versions the caching mechanism got an extra layer in the implementation itself. Assuming that you're indeed using Mojarra, in addition of the line
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
您还需要从com.sun.faces.application.ApplicationAssociate
ApplicationResourceBundle applicationBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("Label");
Field field = applicationBundle.getClass().getDeclaredField("resources");
field.setAccessible(true);
Map<Locale, ResourceBundle> resources = (Map<Locale, ResourceBundle>) field.get(applicationBundle);
resources.clear();
是的,这是一个hack,但是到目前为止,JSF并未提供任何干净的API方法来实现相同的目的.
Yes, that's a hack, but as far JSF doesn't provide any clean API methods to achieve the same.
这篇关于需要在不重新启动服务器的情况下重新加载JSF2.0 ResourceBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!