Over a million developers have joined DZone.

Resource Library Contracts

DZone's Guide to

Resource Library Contracts

· DevOps Zone ·
Free Resource

DevOps involves integrating development, testing, deployment and release cycles into a collaborative process. Learn more about the 4 steps to an effective DevSecOps infrastructure.

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 

Read the 4-part DevOps testing eBook to learn how to detect problems earlier in your DevOps testing processes.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}