[英]JSF2.0 ResourceBundle needs to be reloaded without server restart
我们将JSF2.0与JDK1.6和Tomcat6.1结合使用
我们需要在不重新启动服务器的情况下更新属性文件值(由JSF资源束加载),以免实时Web会话停止。
JDK1.6是否可能,我尝试了下面的clearCache代码,但没有成功。
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);
有没有人尝试过相同的。
更新
以下似乎无法解决重新加载资源包的问题
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,
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方法来实现相同目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.