代码之家  ›  专栏  ›  技术社区  ›  Trick

MissingResourceException-找不到基名称的包

  •  8
  • Trick  · 技术社区  · 15 年前

    出于速度原因,我有一个实用程序类,它根据提供的区域设置加载所有静态数据映射(例如months)。

    所以这个实用类看起来像这样:

    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\u en\u US.properties,…)直接位于源程序包中 资源 . 在Tomcat上部署时,文件夹中有 WEB-INF/类

    现在问题来了。 在这个应用程序中工作时,一切都正常 . 但是我有另一个应用程序,它通过restapi(JAX-RS)连接到这个应用程序。提出请求时 App/rest/months.xml 我得到以下错误:

    java.util.MissingResourceException: Can't find bundle for base name globalMessages, locale en_us
    

    2 回复  |  直到 15 年前
        1
  •  14
  •   Trick    15 年前

    好 啊。。。找到错误。一天之后。。。问题在于区分大小写的信件。尽管如此,当使用“en\u-US”从rest设置locale时,ResourceBundle(在使用rest时)还是在寻找“en\u-US”。

    编辑 好的,我们还发现了为什么都是小写字母的错误。 问题是,因为我在创建区域设置时使用了:

    Locale locale = new Locale("en_US");
    

    Locale locale = new Locale("en","US");
    
        2
  •  0
  •   TeaTime    8 年前

    在这种情况下,对于任何使用Glassfish服务器有问题的人,都可以在管理概述中设置区域设置

    enter image description here

    简单地覆盖默认区域设置,如图所示