【问题标题】:@Named bean "hello world" with @ApplicationScoped (CDI) on Glassfish在 Glassfish 上使用 @ApplicationScoped (CDI) 的 @Named bean "hello world"
【发布时间】:2014-12-25 07:11:24
【问题描述】:

如何从使用 CDI 的 NextClient bean 获取输出到 facelet?​​

我正在尝试使用 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;
    }

}

以 facelets 为例:

<!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 似乎没有任何输出:

thufir@dur:~$ 
thufir@dur:~$ 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
thufir@dur:~$ 

本例改编自Facelets Essentials;不过,我想使用 CDI。

beans.xml 有问题吗?有些地方说它是可选的,有些地方说beans.xml 是必需的。这似乎是可选的:

23.13 配置 CDI 应用程序

当您的 bean 使用范围类型注释时,服务器会识别 应用程序作为 bean 存档,无需额外配置 必需的。 CDI bean 可能的作用域类型在使用中列出 范围。 CDI 使用名为 beans.xml 的可选部署描述符。 与其他 Java EE 部署描述符一样,配置设置 除了 CDI 中的注解设置外,还使用 ​​beans.xml 中的 类。 beans.xml 中的设置会覆盖注释设置,如果 有冲突。存档必须包含 beans.xml 部署 描述符仅在某些有限的情况下...

Java EE 7 教程 适用于 Java EE 平台的第 7 版 第 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)之前,它对我不起作用(我的例子和你的一样)

标签: jsf jakarta-ee ejb cdi el


【解决方案1】:

backing bean 是正确的。您需要检查您的服务器是否支持 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>

这个 bean-discovery-mode="annotated" 是扫描你的类的东西。

【讨论】:

  • 我使用工作 bean/xhtml 创建了分支 4:github.com/THUFIR/EntAppWeb/tree/4_increments_correctly,不需要 beans.xml。我不知道为什么它以前不起作用。也许我在部署它时出错了;但我很小心地重新创建了 .ear 并取消部署,然后部署到 glassfish。我不介意它现在有效,但我不明白为什么它不是。也许我的部署错误...
猜你喜欢
  • 2016-06-08
  • 2012-12-02
  • 1970-01-01
  • 2013-03-03
  • 2013-12-07
  • 2016-04-19
  • 1970-01-01
  • 2012-04-12
相关资源
最近更新 更多