【发布时间】: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
源代码:
【问题讨论】:
-
直到我转到 .../next.jsf(注意 .jsf)之前,它对我不起作用(我的例子和你的一样)
标签: jsf jakarta-ee ejb cdi el