Over a million developers have joined DZone.

Resource Library Contracts

DZone's Guide to

Resource Library Contracts

· DevOps Zone ·
Free Resource

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

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 

Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}