This tutorial covers the steps required to apply JavaServer
Faces 2.2 (JSF 2.2) Resource Library Contracts while using
facelet templates in a web application.
Time to Complete
Approximately 45 minutes
JSF technology is a user interface (UI) framework for developing Java web applications. It is based on components, events, and Model-View-Controller (MVC) architecture. JSF 2.0 introduced facelets as the default View Description Language (VDL). With facelets, you can create templates by using XHTML and CSS, and you can then use the templates to provide a consistent look and feel across different pages of an application.
Resource Library Contracts allows facelet templates to be
applied to an entire application in a reusable and
In this tutorial, you modify a sample web application, which
has a standard header template for all pages. You create
multiple templates and apply them to different pages of the same
application. You also allow users to select the template that
they want to apply.
Hardware and Software Requirements
The following is a list of hardware and software requirements:
- Java Platform, Standard Edition 7 (Java SE 7; Java SE 7u11
- NetBeans 7.3.1 IDE Java Platform, Enterprise Edition (Java
- GlassFish 4
Before starting this tutorial, you should:
- Have some experience writing and deploying JSF web applications.
- Have some experience creating JSF 2.0 templates and template
- Have installed NetBeans 7.3.1 Java EE and GlassFish 4.
- Have started NetBeans.
- Have unzipped the
- Have opened the
JSFTemplateExampleAppproject in NetBeans.
Running and Examining the
Complete the following steps:
Creating Contracts, Templates, and Resources for the
A resource library contract is a resource library that resides
in the contracts
directory of the
web-app root. In this section,
you create contracts by creating a contracts
folder and then adding resources to the folder.
Adding Resources to the
cssLayout.css files in the blue and green
folders, modify the values as listed in the following
table, and then save and close the files:
Applying Resource Library Contracts
In this section, you apply the three resource library contracts in your web application.
Note: A contract is applied based on the invoked URL pattern. Thus, the "red" contract will be applied to "
faces/index.xhtml", the "green" contract will be applied to "
faces/user/*l", and the "blue" contract will be applied to "
Changing Templates Dynamically
In this section, you change the template of a web page dynamically:
Running and Verifying the Output of
In this section, you run
and verify its output.
In this tutorial, you learned how to:
- Create multiple templates for an application by using JSF
2.2 Resource Library Contracts
- Configure your application to use multiple templates based
on URL patterns
- Enable the user to select and apply template to the pages
The application in this tutorial uses JSF facelets and JSF 2.2 Resource Library Contracts. To learn more about these technologies, see the following resources:
blog about Resource Library Contracts
344, JSF 2.2 Specification
Java EE 7 Tutorial: The tutorial is an excellent source
of information about the technologies included in Java EE 7
- To learn more about Java EE 7, visit other OBE tutorials in the Oracle Learning Library
- Lead Curriculum Developer: Paromita Dutta
- Editor: Susan Moxley
- QA: Madhavi Siddireddy
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.