2011-03-30 76 views
1

我們正在使用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(); 

我錯過了什麼問題?

+0

你確定你有權處理正確的包嗎?代碼中的「標籤」字符串需要替換爲您的包。我重點介紹了這段代碼,併爲ApplicationAssociate.getCurrentInstance()。getResourceBundles()添加了一個監視器,然後查看該監視器中的內容。 – cwash 2013-02-26 16:00:23

+0

順便說一句,我使用JSF2,JDK1.6,Glassfish 3.1.1 – cwash 2013-02-26 16:02:01

回答

3

這用於處理一些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方法來實現相同。

+0

感謝您的答覆。 – user684434 2011-03-30 20:08:54

+0

根據你的「答案」(應將該答案發布爲對此答案的評論或作爲你問題的更新),你就會得到一個NPE。你需要更清楚地知道'null'究竟是什麼。它是'applicationBundle'嗎?或者'getDeclaredField()'返回'null'?你確定你在JSF上下文中運行它,而不是普通的vanilla應用程序或其他東西嗎?至於這個例子,請注意,我在答案中使用了包名'Label',正如您在問題中使用的那樣。 – BalusC 2011-03-30 20:38:17

+0

我用你提到的第一個答案的代碼。我從servlet調用了清除緩存代碼。我現在沒有得到NPE。但它沒有刷新價值。我有一個問題是,你得到getDeclaredField(「資源」),屬性文件中的「資源」字段。 – user684434 2011-03-31 15:36:26

相關問題