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

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>
        

Posted November 14, 2013

Up