[英]MissingResourceException - Can't find bundle for base name
我知道在stackoverflow和其他論壇上有很多關於這個錯誤的問題和答案。 但我還是找不到解決方案......
出於速度原因,我有一個實用程序類,它根據提供的語言環境加載所有靜態數據映射(例如幾個月)。
所以這個實用程序類看起來像這樣:
public static final String GLOBAL_MESSAGES = "globalMessages";
private static Map<Integer,Month> monthsMap;
private ResourceBundle getResourceBundle(Locale locale) {
ResourceBundle rb = ResourceBundle.getBundle(GLOBAL_MESSAGES, locale);
return rb;
}
private Map<Integer,Month> getMonths() {
if(monthsMap == null) {
setMonths();
}
return monthsMap;
}
private void setMonths() {
try {
monthsMap = getFactory().getDAO().getAllMonths();
} catch (SQLException e) {
logger.error(e);
} catch (EmptyResultException e) {
logger.error(e);
}
}
public Map<Integer,Month> getMonths(Locale locale) {
if(locale == null) {
return monthsMap;
} else {
if(this.locale != locale) {
this.locale = locale;
setMonths();
}
}
ResourceBundle rb = getResourceBundle(locale);
Map<Integer,Month> map = new HashMap<Integer, Month>();
for(Month akVO : getMonths().values()) {
try {
akVO.setName(rb.getString(akVO.getName()));
} catch (MissingResourceException e) {
//already done
}
map.put(akVO.getId(), akVO);
}
return map;
}
文件globalMessages.properties(globalMessages_en_US.properties,...)直接在源包資源中 。 在Tomcat上部署時,有WEB-INF / classes文件夾。
現在問題。 這一切都適用於此應用程序 。 但我有另一個應用程序通過REST API(JAX-RS)連接到這個。 在發出請求App / rest / months.xml時,我收到以下錯誤:
java.util.MissingResourceException: Can't find bundle for base name globalMessages, locale en_us
我真的迷路了。 絕望......
好的...發現錯誤。 一天f *之后......問題出在敏感信件上。 盡管如此,當使用“en_US”以某種方式設置來自rest的語言環境時,ResourceBundle(當通過REST時)正在尋找“en_us”。
編輯 :好的,也發現了為什么它都是小寫字母的錯誤。 問題是,因為我正在創建語言環境:
Locale locale = new Locale("en_US");
代替:
Locale locale = new Locale("en","US");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.