Java MissingResourceException - 找不到基本名称的包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3318465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MissingResourceException - Can't find bundle for base name
提问by Trick
I know that there are a lot of questions and answers exactly about this error on stackoverflow and other forums. But I still can not find the solution...
我知道在 stackoverflow 和其他论坛上有很多关于这个错误的问题和答案。但我仍然找不到解决方案......
For speed reasons I have one utility class which loads all static data maps (for example months) depending on locale provided.
出于速度原因,我有一个实用程序类,它根据提供的语言环境加载所有静态数据映射(例如几个月)。
So this utility class looks something like this:
所以这个实用程序类看起来像这样:
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;
}
Files globalMessages.properties (globalMessages_en_US.properties,...) are directly in source package resources. When deployed on Tomcat there are in folder WEB-INF/classes.
文件 globalMessages.properties (globalMessages_en_US.properties,...) 直接在源包资源中。当部署在 Tomcat 上时,文件夹WEB-INF/classes 中有。
Now the problem. It all works when working in this application. But I have another application which connects throught REST API (JAX-RS) to this one. When making a request App/rest/months.xmlI get the following error:
现在的问题。在此应用程序中工作时,这一切都有效。但是我有另一个应用程序,它通过 REST API (JAX-RS) 连接到这个应用程序。发出请求App/rest/months.xml 时,出现以下错误:
java.util.MissingResourceException: Can't find bundle for base name globalMessages, locale en_us
I am really lost. And desperate...
我真的很失落。还有绝望...
采纳答案by Trick
Ok... Found the error. After one f* day... The problem is in case sensitive letters. Even though, when setting the locale from rest with "en_US" somehow ResourceBundle (when going through REST) is looking for "en_us".
好的...找到错误。一天后......问题是区分大小写的字母。尽管如此,当使用“en_US”从 rest 设置语言环境时,ResourceBundle(通过 REST 时)正在寻找“en_us”。
EDIT: Ok, found also the error why it was all in small letters. Problem was, because I was creating locale with:
编辑:好的,还发现了为什么都是小写字母的错误。问题是,因为我正在创建语言环境:
Locale locale = new Locale("en_US");
instead of:
代替:
Locale locale = new Locale("en","US");
回答by TeaTime
For anybody having trouble with a Glassfish server in this case, there is the possibility to set the Locale within the Admin Overview
对于在这种情况下使用 Glassfish 服务器遇到问题的任何人,都可以在管理概览中设置区域设置
Simple overwrite the default locale as shown in the picture
简单覆盖默认区域设置,如图
Somebody might find this useful, I had trouble for multiple hours and dont want anyone to have this kind of problem aswell
有人可能会觉得这很有用,我遇到了好几个小时的麻烦,也不希望任何人也遇到这种问题