JSF 2.2 introduces the concept of Resource Library Contracts, which allows a digital developer to organize a library of assets and templates in a reusable set of resources. A resource library contract must reside in the contracts directory folder in the application's web context root. In the standard Maven or Gradle build convention, this folder is src/main/webapp/contracts
. Each subfolder in the contracts folder represents a named resource contract.
The setup for the Chapter 6, JSF Flows and Finesse used a layout for the Resource Library Contracts, as follows:
/src/main/webapp/contracts/
/src/main/webapp/contracts/default/
/src/main/webapp/contracts/default/template.xhtml
/src/main/webapp/contracts/default/styles/app.css
/src/main/webapp/contracts/default/images/
/src/main/webapp/contracts/victoria/
/src/main/webapp/contracts/victoria/template.xhtml
/src/main/webapp/contracts/victoria/styles/app.css
/src/main/webapp/contracts/victoria/images/
Every contract must have at least one declared template.xhtml
file. A contract may have more than one template for its own customization. The declared template has at least one declared insertion point, which is defined as the <ui:insert>
tag. A template usually relies on the digital assets, and these are known as declared resources.
Contracts can be packaged in the JAR files for a customer's use. A resource library contract must be placed in the META-INF/contracts
folder of the JAR file. So, we could repackage the flow examples in the following layout:
META-INF/contracts/
META-INF/contracts/default/javax.faces.contract.xml
META-INF/contracts/default/template.xhtml
META-INF/contracts/default/styles/app.css
META-INF/contracts/default/images/
META-INF/contracts/victoria/
META-INF/contracts/victoria/javax.faces.contract.xml
META-INF/contracts/victoria/template.xhtml
META-INF/contracts/victoria/styles/app.css
META-INF/contracts/victoria/images/
We will need to add an empty marker file to each contract, javax.faces.contract.xml
. The reference for this filename is found in the static string: javax.faces.application.ResourceHandler.RESOURCE_CONTRACT_XML
.