JSF 2.3 Part2, Using @inject for FacesContext

One of the wonderful features of the upcoming JSF 2.3 is the ability to inject many JSF objects such as UIViewRoot, ViewMap, ApplicationMap, ExternalContext and FacesContext. In this post, I will show you how to simply inject FacesContext without having to use the old way FacesContext.getCurrentInstance().

Continuing working on the sample that was introduced in the previous post, let’s modify User managed bean to get FacesContext in order to display an information message to the user once the operation is done. The following code snippet shows the modified User managed bean.

package beans;

import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

public class User implements Serializable {
    private static final long serialVersionUID = 4629817047379532658L;
    private static final String INFO_MESSAGE = "Operation is done ...";
    private String name;
    FacesContext facesContext;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public String greet() {
        facesContext.addMessage("form1", new FacesMessage(FacesMessage.SEVERITY_INFO, INFO_MESSAGE, INFO_MESSAGE));
        return null;

As shown, all what you need to inject FacesContext in your managed bean is to annotate its declared instance with @inject annotation.

Running Application on GlassFish 4.1

In order to run our JSF application this time on GlassFish, we need to build Mojarra jars for GlassFish and then configure them on the server as follows:

  1. First of all, you need to make sure that you have both SVN and Apache Ant installed and configured in your operating system.
  2. Check out Mojarra 2.3 source code using the following SVN command:
    svn checkout https://svn.java.net/svn/mojarra~svn/trunk

    In order to build the JSF Mojarra jars for GlassFish then create a new file called build.properties under trunk. Then copy build.properties.glassfish file (which is located directly under trunk) content into it. Do not forget to set jsf.build.home property to the directory in which the sources were checked out into as follows for example.

    # --------------------------------------------------
    # Set this to the directory in which the sources
    # were checked out into
    # --------------------------------------------------
  3. CD to the jsf.build.home path, and then execute the following ant command:
    ant clean main
  4. After the successful execution of the previous command, you can get Mojarra jars from:
    1. jsf-api-intermediate.jar from trunk/jsf-api/build/lib directory.
    2. javax.faces.jar from trunk/jsf-ri/build/lib directory.
  5. Finally, overwrite the default javax.faces.jar file under glassfish-4.1/glassfish/modules with the new built javax.faces.jar file, and deploy our JSF application to GlassFish. After running our JSF application, you can see the information message after clicking "Greet" button as shown below.
    Screen Shot 2014-12-21 at 4.03.07 AM

Getting the JSF sample source code:

The sample source code is available in GitHub:

This entry was posted in JSF and tagged , , , , , , by Hazem Saleh. Bookmark the permalink.

About Hazem Saleh

Hazem Saleh has more than eleven years of experience in Cloud, Mobile and Open Source technologies. He worked as a software engineer, technical leader, application architect, and technical consultant for many clients around the world. He is an Apache PMC (Project Management Committee) member and a person who spent many years of his life writing open source software. Beside being the author of the "JavaScript Unit Testing" book, "JavaScript Mobile Application Development" book, "Pro JSF and HTML5" book and the co-author of the "Definitive guide to Apache MyFaces" book, Hazem is also an author of many technical articles, a developerWorks contributing author and a technical speaker in both local and international conferences such as ApacheCon North America, Geecon, JavaLand, JSFDays, CON-FESS Vienna and JavaOne. Hazem is an XIBMer, he worked in IBM for ten years. Now, He is working for Nickelodeon New York as a Mobile Architect. He is also an OpenGroup Master Certified Specialist.