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

问题描述:

我有一个相当标准的archirecture,其中一个PrimeFaces页面调用[email protected][email protected]注解的类中的登录操作。即使在托管bean线程中FacesContext.getCurrentInstance()也会返回null

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

有很多帖子都说基本上,“唯一时间FacesContext.getCurrentInstance()将返回null是当它在托管bean线程之外被调用时”。但是我确实在另一个论坛上发现了一篇文章,指出了导致它的构建/类加载器问题。不知何故,由于不正确的指定依赖关系,您的FacesContext类可以加载容器,并再次加载应用程序。所以应该是单身人士变成两个单身人士(不知何故...?)。无论如何,我发布这个记录修复,而不是完全解释症状背后的问题。

我正在使用JBoss Developer Studio,使用Maven建立JBoss EAP 6.2EAP附带模块JSF 1JSF 2.1。我想用2.1,这是存储在这里:

C:\JBoss\jboss-eap-6.2.0\modules\system\layers\base\javax\faces\api\main. 

在我EJB项目POM我曾错误地增加了一个依赖于jsf-api<scope>compile,导致FacesContext.getCurrentInstance()返回null如上所述。

的解决方案是:在POMejb和我web项目中,我添加了这个依赖:

<dependency> 
    <groupId>org.jboss.spec.javax.faces</groupId> 
    <artifactId>jboss-jsf-api_2.1_spec</artifactId> 
    <scope>provided</scope> 
</dependency> 

,并确保我的部署ejb项目还告诉JBoss有关,我需要确保它获取与此在它出版的MANIFEST.MF

Dependencies: javax.faces.api:main 

可以通过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-plugin项已经在那里了,我刚添加的归档定义现有<configuration>。我假设你正在用ejb-jar-plugin或ejb-war-plugin构建,可以使用相同的<archive>...</archive>定义。