Jsf getCurrentInstance()即使在托管bean线程中也返回null

Jsf getCurrentInstance()即使在托管bean线程中也返回null,jsf,nullpointerexception,jboss-eap-6,facescontext,Jsf,Nullpointerexception,Jboss Eap 6,Facescontext,我有一个相当标准的架构,PrimeFaces页面调用类中的登录操作,该类用javax.inject.@Named和javax.enterprise.context.@SessionScoped doLogin()方法从FacesContext.getCurrentInstance()接收null返回,即使Faces Servlet应该被正确调用。很多帖子本质上说,“只有在托管bean线程外部调用时,FacesContext.getCurrentInstance()才会返回null”。但我确实在另

我有一个相当标准的架构,
PrimeFaces
页面调用类中的登录操作,该类用
javax.inject.@Named
javax.enterprise.context.@SessionScoped


doLogin()
方法从
FacesContext.getCurrentInstance()
接收null返回,即使Faces Servlet应该被正确调用。

很多帖子本质上说,“只有在托管bean线程外部调用时,
FacesContext.getCurrentInstance()
才会返回null”。但我确实在另一个论坛上找到了一篇文章,指出了导致它的构建/类装入器问题。不知何故,由于不正确地指定了依赖项,您的
FacesContext
类可以为容器加载,也可以为应用加载。所以应该是一个单件的东西变成了两个单件(不知怎么的…?)。不管怎样,我发布这篇文章是为了记录修复情况,而不是为了充分解释症状背后的问题

我正在使用
jbossdeveloper Studio
,使用
Maven
构建
jbosseap 6.2
EAP
附带了
jsf1
jsf2.1
的模块。我想使用2.1,它存储在这里:

C:\JBoss\jboss-eap-6.2.0\modules\system\layers\base\javax\faces\api\main.
在我的
EJB
项目
POM
中,我错误地为
jsfapi
compile
添加了一个依赖项,这导致
FacesContext.getCurrentInstance()
返回
null
,如上所述

解决方案是:在我的
ejb
web
项目的
POM
中,我添加了以下依赖项:

<dependency>
    <groupId>org.jboss.spec.javax.faces</groupId>
    <artifactId>jboss-jsf-api_2.1_spec</artifactId>
    <scope>provided</scope>
</dependency>
这也可以通过
Maven
来实现,方法是在
POM
中为创建构建插件的
ejb
项目放置一个归档配置:

<!-- 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>

maven ejb插件
2.3
3.1
javax.faces.api:main
对我来说,
maven ejb插件
条目已经存在,我只是将归档定义添加到现有的
中。我假设如果您使用EJBJAR插件或EJBWAR插件构建,那么可以使用相同的
定义

<!-- 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>