Injecting JSF artifacts
The JSF specification predates CDI. As such, many JSF artifacts, such as FacesContext
and ExternalContext
, had to be obtained via static entry
methods; this resulted in hard-to-read boilerplate code. JSF 2.3 introduces the ability to inject JSF artifacts via CDI's @Inject
annotation, as seen in the following example:
package net.ensode.javaee8book.jsfarbitrarymess; import java.io.Serializable; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; @Named @ViewScoped public class ArbitraryMessageController implements Serializable { @Inject FacesContext facesContext; public void saveData() { FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Data saved successfully", "All Data successfully saved."); facesContext.addMessage(null, facesMessage);...