如何從使用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
直到我去.../next.jsf(注意.jsf),它不適用於我(我的示例,與您的示例相同) – 2014-10-29 23:23:08