Over a million developers have joined DZone.

Resource Library Contracts

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

Facelets is an open source web template system under the Apache License. JavaServer Faces 2.2(JSR 344), is a part of JavaEE 7 specification, defined Resource Library Contracts which enables reusing Facelets templates in entire application dynamically at runtime. As it can be remembered, Facelets had been the default view handler technology with Java Server Faces version 2.0. In this tutorial, we will discuss the usage of Resource Library Contracts.

What is the Contracts?

Contracts, in Java web application, are template and css files that are grouped under the "contracts" directory (another default location is META-INF/contracts directory under the CLASSPATH) whose parent is root directory. Templates and other resources of each contract (eg, JavaScript files, images) is located in its own directory. The contract’s template file is called “declared template and contract’s CSS and other resources are called declared resources

Requirements for usage

First of all, a declared template, ui:insert tag which is called declared insertion points in this template are needed in order to use contracts. The declared templates, declared insertion points and optional declared resources, they make a definition of Resource Library Contract together. 

1. Project directory structure


2. Project Dependencies


3. Faces configuration file


A contract is applied on the basis of url pattern. Based on this configuration, “colored” contract in root directory and "colorless" contract under the member director are going to be applied in our implementation. 

4. /index.xhtml

<f:view contracts="#{myContracts.contract}">
            <ui:composition template="/template.xhtml">
                <ui:define name="content">
                        <a href="#{facesContext.externalContext.requestContextPath}/member/index.xhtml">Visit</a> member directory
                        <p>Choose a theme:</p>
                            <h:selectOneRadio value="#{myContracts.contract}" layout="pageDirection" required="true">
                                <f:selectItem itemValue="colored" itemLabel="Colored"/>
                                <f:selectItem itemValue="colorless" itemLabel="Colorless"/>
                            <h:commandButton value="Apply theme" action="index"/>

Notice that f:view tag encloses ui:composition tag and its contracts attribute. We will provide template exchange dynamically by setting different contract names at runtime to the Managed Bean property which is bound with EL expression to this attribute.

5. /member/index.xhtml

        <ui:composition template="/template.xhtml">
            <ui:define name="content">
                <a href="#{facesContext.externalContext.requestContextPath}/index.xhtml">Visit</a> home directory

6. MyContracts.java

package com.kodcu;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

 * @author hakdogan

public class MyContracts implements Serializable {
    String contract = "colored";

    public String getContract() {
        return contract;

    public void setContract(String contract) {
        this.contract = contract;

7. Demo application



Real content above and the demo application, can be accessed at Resource Library Contracts 

The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}