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>