Can we store messages.properties under WEB-INF in a JSF application?

1.2k views Asked by At

I've tried this mapping and JSF just wouldn't find it.

<application>
    <resource-bundle>
        <base-name>/WEB-INF/i18/messages</base-name>
        <var>msg</var>
    </resource-bundle>
</application>

All examples I have seen put messages.properties in the Java Source folder. Some use a package name, some don't - which is pretty much still like using a blank package name. Is JSF forcing us to store resource bundles under Java Source?

1

There are 1 answers

0
BalusC On BEST ANSWER

This is not specific to JSF. This is specific to the ResourceBundle API. Bundles are by specification loaded via caller's class loader with a fully qualified base name. From the javadoc (emphasis mine):

getBundle

public static final ResourceBundle getBundle(String baseName)

Gets a resource bundle using the specified base name, the default locale, and the caller's class loader. [...]

Parameters:

baseName - the base name of the resource bundle, a fully qualified class name

[...]

Under the covers, it is using ClassLoader#getResourceAsStream() to obtain an InputStream of the classpath resource.

The /WEB-INF folder is not part of the classpath. It's part of the web content. Web content resources are programmatically only available via ServletContext#getResourceAsStream() and inherently in JSF via ExternalContext#getResourceAsStream(). You can of course homegrow a custom ResourceBundle.Control which loads the resource as a web content resource, but it is really not recommended to do things differently from the standard without any valid technical reason.

Just put those resources in the classpath the usual way. There's no point putting them in web content.