There are many posts that say essentially, "the only time FacesContext.getCurrentInstance()
will return null is when it's called outside a managed bean thread". But I did find one post on another forum that identified a build/class loader issue that causes it. Somehow, due to improperly specified dependencies, your FacesContext
class can get loaded for the container and again for the app. So what is supposed to be a singleton turns into two singletons (somehow...?). Anyway, I'm posting this to record the fix, not to fully explain the problem behind the symptom.
I'm using JBoss Developer Studio
, building with Maven
for JBoss EAP 6.2
. EAP
comes with modules for JSF 1
and JSF 2.1
. I want to use 2.1, which is stored here:
C:\JBoss\jboss-eap-6.2.0\modules\system\layers\base\javax\faces\api\main.
In my EJB
project POM
I had incorrectly added a dependency for jsf-api
<scope>compile
, which resulted in FacesContext.getCurrentInstance()
returning null
as described above.
The solution was: in the POM
for my ejb
and my web
projects, I added this dependency:
<dependency>
<groupId>org.jboss.spec.javax.faces</groupId>
<artifactId>jboss-jsf-api_2.1_spec</artifactId>
<scope>provided</scope>
</dependency>
And to make sure my deployed ejb
project also tells JBoss
about that I needed to make sure it gets published with this in it's MANIFEST.MF
:
Dependencies: javax.faces.api:main
That can be done by Maven
too, by putting an archive configuration in the POM
for the ejb
project where the build plugin was created:
<!-- most of this <build> element was already in my POM -->
<build>
<plugins>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
<!-- I added this <archive> element to tell Maven to add the dependency line in MANIFEST.MF -->
<archive>
<manifestEntries>
<Dependencies>javax.faces.api:main</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
For me, the maven-ejb-plugin
entry was already there and I just added the archive definition to the existing <configuration>
. I assume if you are building with ejb-jar-plugin or ejb-war-plugin, the same <archive>...</archive>
definition could be used.