JSF2.0资源包需要在不重新启动服务器
我们正在使用JSF2.0与JDK1.6和Tomcat6.1重新加载JSF2.0资源包需要在不重新启动服务器
我们有一个要求,更新属性文件中的值(由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版本中,缓存机制在实现本身中获得了额外的一层。假设你确实使用钻嘴鱼科,除了
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();
是的,这是一个黑客的路线,但据JSF没有按” t提供任何干净的API方法来实现相同。
感谢您的答复。 – user684434 2011-03-30 20:08:54
根据你的“答案”(应将该答案发布为对此答案的评论或作为你问题的更新),你就会得到一个NPE。你需要更清楚地知道'null'究竟是什么。它是'applicationBundle'吗?或者'getDeclaredField()'返回'null'?你确定你在JSF上下文中运行它,而不是普通的vanilla应用程序或其他东西吗?至于这个例子,请注意,我在答案中使用了包名'Label',正如您在问题中使用的那样。 – BalusC 2011-03-30 20:38:17
我用你提到的第一个答案的代码。我从servlet调用了清除缓存代码。我现在没有得到NPE。但它没有刷新价值。我有一个问题是,你得到getDeclaredField(“资源”),属性文件中的“资源”字段。 – user684434 2011-03-31 15:36:26
你确定你有权处理正确的包吗?代码中的“标签”字符串需要替换为您的包。我重点介绍了这段代码,并为ApplicationAssociate.getCurrentInstance()。getResourceBundles()添加了一个监视器,然后查看该监视器中的内容。 – cwash 2013-02-26 16:00:23
顺便说一句,我使用JSF2,JDK1.6,Glassfish 3.1.1 – cwash 2013-02-26 16:02:01