Bringing your own JSF implementation

duration 10 minutes

Prerequisites:

Learn how to get CDI integration when you bring your own JSF implementation using Open Liberty

What you’ll learn

You will learn how to build a simple JSF application using the Mojarra JSF implementation packaged inside of the application.

Normally OpenLiberty uses an implementation of JSF called Apache MyFaces. If you want to use a different version of Apache MyFaces, or an entirely different JSF implementation such as Mojarra, you can follow this guide to find out how.

In this guide you will start off with a basic "hello world" JSF application that uses Mojarra. Then, you will enable the JSF Container feature which will allow the application to make use of CDI integrations.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone https://github.com/openliberty/guide-jsf-container.git
cd guide-jsf-container

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project that you will build.

Before you begin, make sure you have all the necessary prerequisites.

What is JSF Container

TODO

Starting up the basic JSF application

To start off there is a simple JSF application with one static page and one Java class to back it.

The index.xhtml file has some static text, and also displays the value of #{JSFGreetingBean.greeting}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
        <h:head>
            <title>OpenLiberty JSF Container Guide</title>
        </h:head>
        <h:body>
            <p>Our JSF bean says... <br/> <h:outputText value="#{JSFGreetingBean.greeting}" /></p>
        </h:body>
</html>

The to evaluate the #{JSFGreetingBean.greeting} expression, the server looks for a bean named JSFGreetingBean, then invokes a getGreeting() method on it. This will map to the JSFGreeingBean class in the application:

/*******************************************************************************
 * Copyright (c) 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package io.openliberty.guides.jsfcontainer;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;

@SuppressWarnings("serial")
@ManagedBean(name = "JSFGreetingBean")
@SessionScoped
public class JSFGreetingBean implements Serializable {

    private String greeting = "I'm a regular JSF Bean";

    @PostConstruct
    public void start() {
        System.out.println(getClass() + " postConstruct called");
        this.greeting = "I'm using lifecycle annotations";
    }

    public void setGreeting(String newGreeting) {
        this.greeting += newGreeting;
    }

    public String getGreeting() {
        return this.greeting;
    }
}

One of the goals of this guide is to use Mojarra as the JSF implementation instead of the one that comes with OpenLiberty by default (Apache MyFaces). To do this, a dependency is specified on Mojarra:

dependencies {
    compile group: 'com.sun.faces', name: 'jsf-api', version: '2.2.14'
    compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.2.14'
        providedCompile group: 'javax.enterprise', name: 'cdi-api', version: '1.2'
    libertyRuntime group: 'io.openliberty', name: 'openliberty-runtime', version: '[17.0.0.4,)'
}

By specifying the dependencies as type compile, the artifacts will be packaged into the war application under the WEB-INF/lib/ directory, and therfore be on the application classpath at runtime.

Since the default JSF implementation is not being used, there is no JSF feature enabled in server.xml:

<server>

    <featureManager>
            <feature>cdi-2.0</feature>
            <feature>el-3.0</feature>
            <feature>jsp-2.3</feature>
            <feature>servlet-4.0</feature>
            <feature>concurrent-1.0</feature>
    </featureManager>
    
    <httpEndpoint id="defaultHttpEndpoint" httpPort="${httpPort}" httpsPort="${httpsPort}" />
    
</server>

To build and run this application, run the following command:

./gradlew libertyStart open

This will do the following things: 1. Download and install liberty to the project build folder 1. Compile and package the war application (including the index.xhtml file, JSFGreetingBean class, and Mojarra jars) 1. Install the war application named JSFContainerSample-1.0-SNAPSHOT to the server 1. Start the server

Adding CDI to the JSF application

TODO

Enable CDI integration using the JSF Container feature

TODO

Great work! You’re done!

You built and tested a web application project with an Open Liberty server using Gradle.

You can quickly create this project structure by using the Liberty App Accelerator and choose to create a Gradle project.

Contribute to this guide

Is something missing or broken? Raise an issue, or send us a pull request.

Great work! You're done!

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.

Star

Guide license

Where to next?