在 JSF 页面中重用同一类的多个 CDI @Named bean 实例

发布于 2024-10-09 16:05:13 字数 342 浏览 0 评论 0原文

我想在 JSF2 页面中使用同一 CDI 命名 bean 的多个实例。 (假设我有一个支持 bean 来访问 contentmanager 类别,但我想在同一个 JSF 页面中显示多个类别)我想知道如何“覆盖”@Named 注释的默认名称,以便我可以拥有这 3 或 4 个实例在我的页面中。 (记住旧的 JSP“useBean”标签在页面中使用 bean...您可以通过这种方式使用同一类的多个实例) 我搜索过 beans.xml 但没有找到它。我可以使用旧的 faces-context.xml“托管 bean”条目吗?不知道这是否适用于 CDI @Named bean,还是只能用于旧的非 CDI 托管 bean。

感谢您提供任何信息, 伊格纳西奥

I'd like to use multiple instances of the same CDI Named bean in a JSF2 page. (Imagine I have a backing bean to access a contentmanager category, but I want to show multiple categories in the same JSF page) I wonder how can I "override" the default name of @Named annotation so I can have those 3 o 4 instances in my page. (¿Remember the old JSP "useBean" tag to use a bean in a page... you could use multiple instances of the same class this way)
I've search about beans.xml but don't find it. Can I use old faces-context.xml "managed bean" entry? Don't know if that will apply to a CDI @Named bean or can only be use for old non CDI managed beans.

thanks for any info,
Ignacio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2024-10-16 16:05:13

您可以将您的 bean 设置为 @Dependent (或删除现有范围注释),并且只要您不在同一个 EL 表达式中使用该 bean 两次,您就会获得新的底层实例。

You can just make your bean @Dependent (or remove existing scope annotation), and as long as you don't use the bean twice in the same EL expression, you'll get new underlying instances.

日记撕了你也走了 2024-10-16 16:05:13

在给定范围内不能有多个 bean 实例。例如,在一个请求中,您不能拥有请求范围 bean 的两个实例。这就是范围的重点。

所以我想你根本不应该管理你的豆子。只需将它们作为请求属性并通过 EL 访问它们即可。

You can't have more than one bean instance in a given scope. So for example, in one request, you can't have two instances of a request-scoped bean. That's the point of the scopes.

So I guess you should not have your beans managed at all. Just put them as request attributes and access them via EL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文