JSF 2.2: Configurable resource directory

In my third post of the series on JSF 2.2 features I will present a small extension of the JSF resource handling mechanism. JSF 2.2 allows to configure the directory used for resource lookup.

By default, the resource handling mechanism introduced in JSF 2.0 looks up resource in the following locations:

  1. In /resources under the web application root folder.
  2. In /META-INF/resources in JAR files.

The disadvantage of /resources is that everything in that folder is accessible from outside by default. This is not always desirable, especially for composite components. Therefore, JSF 2.2 introduces the context parameter javax.faces.WEBAPP_RESOURCES_DIRECTORY to specify the directory used for resource lookup in the file system of the web application (point 1 in the list above). In the listing below, the path is moved to /WEB-INF/resources for instance. Resources inside /WEB-INF can be accessed by JSF but never from the “outside world”.

<context-param>
  <param-name>
    javax.faces.WEBAPP_RESOURCES_DIRECTORY
  </param-name>
  <param-value>/WEB-INF/resources</param-value>
</context-param>

For demonstration, I took the collapsible panel component from the ajaxified collapsible panel post and moved the resource directory to /WEB-INF/resources.

The following figure shows the updated content and folder structure of the library containing the component and all required resources:

JSF 2.2: Configurable resource directory (example)

The source code for the JSF 2.2 series examples can be found in the JSFlive Github repository jsf22-examples (module jsf22-resource-dir).

Further official details about JSF 2.2 can be found in the JSR 344: JavaServer Faces 2.2.

One response to “JSF 2.2: Configurable resource directory

  1. Pingback: What's new in JSF 2.2? | J-Development

Leave a comment