我有一個相當標準的archirecture,其中一個PrimeFaces
頁面調用[email protected]
和[email protected]
註解的類中的登錄操作。即使在託管bean線程中FacesContext.getCurrentInstance()也會返回null
doLogin()
方法從FacesContext.getCurrentInstance()
收到null返回,即使Faces Servlet 應該被正確調用。
我有一個相當標準的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.2
。 EAP
附帶模塊JSF 1
和JSF 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
如上所述。
的解決方案是:在POM
我ejb
和我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>
定義。