1

For some reason the messages.properties file is located somewhere in WEB-INF folder but outside WEB-INF/classes folder. Specifically the file's path is /WEB-INF/messages/messages.properties. How do I load this resource bundle using the method ResourceBundle.getBundle( ? )?

4

2 回答 2

1

您可以使用 Servlet 上下文获取路径,如下所示;

getServletContext().getResource("/messages/messages.properties).getPath();

然后使用 URLClassLoader() 创建一个类加载器并将其传递给 getBundle() 方法。

于 2012-11-30T07:47:29.420 回答
0

我只是组合了一个 ResourceBundle.Control 扩展来处理这种确切的情况:

https://gist.github.com/Tzrlk/82d74c074e63955a8a35

用法:

ServletContext servletContext = //! get access to servlet context instance
Locale locale = //! get access to locale instance

try {
    ResourceBundle.Control control = new ServletContextBundleControl(servletContext);
    ResourceBundle bundle = ResourceBundle.getBundle("/WEB-INF/messages/messages", locale, control);
} catch (MissingResourceException error) {
    // handle exception 
}
于 2014-12-03T01:23:33.823 回答