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

contracts_body1

2. Project Dependencies

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.faces</artifactId>
  <version>2.2.0</version>
</dependency>

3. Faces configuration file

<application>
        
        <resource-library-contracts>
            
            <contract-mapping>
                <url-pattern>/member/*</url-pattern>
                <contracts>colorless</contracts>
            </contract-mapping>
            
            <contract-mapping>
                <url-pattern>*</url-pattern>
                <contracts>colored</contracts>
            </contract-mapping>
        
        </resource-library-contracts>
    
</application>

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">
                    <h:panelGrid>
                        <a href="#{facesContext.externalContext.requestContextPath}/member/index.xhtml">Visit</a> member directory
                        <p>Choose a theme:</p>
                        <h:form>
                            <h:selectOneRadio value="#{myContracts.contract}" layout="pageDirection" required="true">
                                <f:selectItem itemValue="colored" itemLabel="Colored"/>
                                <f:selectItem itemValue="colorless" itemLabel="Colorless"/>
                            </h:selectOneRadio>
                            <h:commandButton value="Apply theme" action="index"/>
                        </h:form>
                    </h:panelGrid>
                </ui:define>
            </ui:composition>
        </f:view>
    </body>

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

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

6. MyContracts.java

package com.kodcu;

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

/**
 * @author hakdogan
 */

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

    public String getContract() {
        return contract;
    }

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

7. Demo application

contracts_body2

contracts_body3

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

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}