Skip to main content

JSF Tip #38 - Using a view-based resource library contract

Posted by mriem on November 14, 2013 at 1:00 PM PST

In JSF 2.2 you have the ability to define a resource library contract so you can deliver a different experience on the basis of which user visits your website. This blog entry shows you how you can switch between a resource library contract on the basis of an EL expression.

To show this off we define 2 CSS styles, blue and red.

The blue style

body {
    background-color: blue;
}

The red style

body {
    background-color: red;
}

The landing page

<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Resource Library Contract f:view sample</title>
    </h:head>
    <h:body>
        <h1>Resource Library Contract f:view sample</h1>
        <ul>
            <li><a href="#{facesContext.externalContext.requestContextPath}/faces/viewrlc.xhtml?contract=blue">Blue contract</a></li>
            <li><a href="#{facesContext.externalContext.requestContextPath}/faces/viewrlc.xhtml?contract=red">Red contract</a></li>
        </ul>
    </h:body>
</html>

The using page

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <f:view contracts="#{param.contract}">
        <h:head>
            <title>Resource Library Contract</title>
            <h:outputStylesheet name="style.css"/>
        </h:head>
        <h:body>
            <h:form>
                <p>
                    The color is coming from a resource library contract.
                </p>
            </h:form>
        </h:body>
    </f:view>
</html>

Note the code for this sample is available as part of the Glassfish samples (see the subversion repository at https://svn.java.net/svn/glassfish-samples~svn/trunk/ws/javaee7/jsf/viewrlc/)

And that is it.

Enjoy!

Related Topics >>