supertonsky
supertonsky

Reputation: 2733

Loading resource bundle outside WEB-INF/classes folder

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( ? )?

Upvotes: 1

Views: 3916

Answers (2)

tzrlk
tzrlk

Reputation: 896

I just put together a ResourceBundle.Control extension that handles this exact situation:

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

usage:

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 
}

Upvotes: 0

dinukadev
dinukadev

Reputation: 2297

You can get the path using the Servlet context as follows;

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

then using the URLClassLoader() create a class loader and pass this on to the getBundle() method.

Upvotes: 1

Related Questions