HTML and CSS Reference
In-Depth Information
■
It is possible to map multiple contracts to a single mapping. In that case, it will go through each contract
sequentially to look for the required templates. Once a template has been found it will stop processing the other contracts.
Tip
Method 2: Specifying the contracts on each view
By specifying the contract on each view you can make your application skinnable by the user. That is, you can allow
the user to select which contract to apply for your application. You apply contracts to template clients by enclosing
the view in an
<f:view />
tag where you specify the name of the contract to apply in the
contracts
attributes, e.g.
<f:view contracts="basic">
. You can replace the explicit declaration of the view contract but using an EL binding,
e.g.
<f:view contracts="#{userSession.contract}">
, as shown in Listings 5-15 and 5-16.
Listing 5-15.
Allowing the User to Select the Contract to Apply to the View
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="
http://www.w3.org/1999/xhtml
"
xmlns:h="
http://xmlns.jcp.org/jsf/html
"
xmlns:ui="
http://xmlns.jcp.org/jsf/facelets
"
xmlns:f="
http://xmlns.jcp.org/jsf/core
"
xmlns:p="
http://xmlns.jcp.org/jsf/passthrough
"
>
<f:view contracts="#{userSession.contract}">
<ui:composition template="/page-template.xhtml">
<ui:define name="page-title">Welcome to JSF 2.2</ui:define>
<ui:define name="content">
<h:form>
Select a template
<h:selectOneRadio value="#{userSession.contract}" layout="pageDirection"
required="true">
<f:selectItem itemValue="basic" itemLabel="Basic" />
<f:selectItem itemValue="basic-plus" itemLabel="Basic Plus" />
</h:selectOneRadio>
<h:commandButton value="Save" />
</h:form>
</ui:define>
<ui:define name="top">Template: #{userSession.contract}</ui:define>
</ui:composition>
</f:view>
</html>
Listing 5-16.
Session-Scoped Managed Bean Used for Storing the Selected Contract
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserSession {
