Tri*_*ick 8 java locale bundle struts2 properties
我知道在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;
}
Run Code Online (Sandbox Code Playgroud)
文件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
Run Code Online (Sandbox Code Playgroud)
我真的迷路了.绝望......
Tri*_*ick 14
好的...发现错误.一天f*之后......问题出在敏感信件上.尽管如此,当使用"en_US"以某种方式设置来自rest的语言环境时,ResourceBundle(当通过REST时)正在寻找"en_us".
编辑:好的,也发现了为什么它都是小写字母的错误.问题是,因为我正在创建语言环境:
Locale locale = new Locale("en_US");
Run Code Online (Sandbox Code Playgroud)
代替:
Locale locale = new Locale("en","US");
Run Code Online (Sandbox Code Playgroud)