2014-10-29 54 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