2014-10-29 84 views
0

如何從使用CDI的NextClient bean獲得輸出到facelet?@ Glass Bean上的@ApplicationScoped(CDI)@Named bean「hello world」

我試圖使用CDI進口:

package dur.beans; 

import javax.enterprise.context.ApplicationScoped; 
import javax.inject.Named; 


@Named("nextClient") 
@ApplicationScoped 
public class NextClient implements NextClientLocal { 

    private int next = 1009; 

    @Override 
    public int getNext() { 
     next = next + 1; 
     return next; 
    } 

} 

有塊小例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     > 

    <h:head></h:head> 
    <h:body> 
     This and everything before will be ignored 
     <ui:composition template="template.xhtml"> 
      <ui:define name="navigation"> 
       <ui:include src="menu.xhtml"/> 
      </ui:define> 
      <ui:define name="main"> 
       <h1>bird</h1> 
       <p> 
        next #{nextClient.next} 
       </p> 
      </ui:define> 
     </ui:composition> 
     This and everything after will be ignored 
    </h:body> 
</html> 

,但似乎沒有被從bean的任何輸出:

[email protected]:~$ 
[email protected]:~$ lynx -dump http://localhost:8080/EntAppWeb-war/next.xhtml         birds... 

crud ops 
    __________________________________________________________________ 

    [1]Home 
    [2]Parrot 
    [3]Eagle 
    [4]Falcon 
    [5]next 

             bird 

    next 

References 

    1. http://localhost:8080/EntAppWeb-war/next.xhtml 
    2. http://localhost:8080/EntAppWeb-war/next.xhtml 
    3. http://localhost:8080/EntAppWeb-war/next.xhtml 
    4. http://localhost:8080/EntAppWeb-war/next.xhtml 
    5. http://localhost:8080/EntAppWeb-war/next.xhtml 
[email protected]:~$ 

本實例改編自Facelets Essentials;但是,我想使用CDI。

問題是beans.xml?有些地方說它是可選的,其他的則需要beans.xml。這似乎可選:

23.13配置CDI應用

當你的豆用範圍類型註釋,服務器識別 的應用程序作爲一個bean存檔,並沒有額外的配置是需要 。使用 作用域中列出了CDI bean可能的作用域類型。 CDI使用一個名爲beans.xml的可選部署描述符。 與其他Java EE部署描述符類似,除了CDI 類中的註釋設置之外,還使用beans.xml中的配置設置 。如果 存在衝突,beans.xml中的設置將覆蓋註釋設置。歸檔必須包含的beans.xml部署描述符 只是在某些有限的情況下...

了Java EE 7 教程 7版對Java EE平臺 p 404

從我閱讀,例如,似乎推薦CDI超過@ManagedBean。我從沒有找到比這更簡單的例子。

也參見:

https://stackoverflow.com/a/4397444/262852

https://stackoverflow.com/questions/26110888/what-is-the-alternative-to-managedbean

源代碼:

https://github.com/THUFIR/EntAppWeb

+1

直到我去.../next.jsf(注意.jsf),它不適用於我(我的示例,與您的示例相同) – 2014-10-29 23:23:08

回答

1

背襯豆是正確的。您需要檢查您的服務器是否支持CDI,或者您需要使用一些額外的庫來使其工作(例如Apache Tomcat)。

我認爲有CDI的beans.xml是必需的,因爲容器需要掃描所有使用CDI批註註釋的bean。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" 
     bean-discovery-mode="annotated"> 
</beans> 

豆發現模式=「註明」就是掃描你的類。

+0

我創建了分支4,如下所示:https://github.com/ THUFIR/EntAppWeb/tree/4_increments_correctly with working bean/xhtml,不需要'beans.xml'。我不知道爲什麼它以前沒有工作。也許我在部署它時犯了一個錯誤;但我小心地重新創建.ear並取消部署,然後部署到glassfish。我不介意它現在起作用,但我不明白爲什麼它不是。也許我的部署錯誤... – Thufir 2014-10-31 20:00:02