如何在 JSF 2 中的 beans 之间传递信息?

发布于 2024-10-22 11:35:51 字数 2038 浏览 2 评论 0原文

我有一个场景,但还没有一个明确的想法来实现这一点。

下面是示例场景:

  1. 我有一个 myView.xhtml 文件,可以从 2 个其他容器 xhtml 显示为弹出窗口或 primefaces 对话框弹出窗口,假设 container1.xhtmlcontainer2.xhtmlcontainer1.xhtml >myView.xhtml 包含在这样的内容中? )
  2. 而且,我们还有视图范围的 bean,我们称它们为 myViewBeancontainer1Beancontainer2Bean em>
  3. container1Bean 具有 transactionDate 属性,并且 container2Bean< /em>有一个属性transDate
  4. myViewBean的@PostConstruct方法会查询数据库基于日期类型的参数
  5. 在container1.xhtml上,如果用户单击将弹出myView.xhtml的按钮,container1Bean应该能够将其 transactionDate 提供给 myViewBeanmyViewBean 将能够在@PostConstruct方法中基于它进行查询。
  6. 在container2.xhtml上,如果用户单击将弹出myView.xhtml的按钮,container2Bean应该能够将其 transDate 提供给 myViewBeanmyViewBean 将能够在@PostConstruct方法中基于它进行查询。

我正在考虑在 MyViewBean< 中使用 @Inject Container1Bean@Inject Container2Bean /em>,这样在 MyViewBean 中,我可以获得 container1Bean 的 transactionDate 或 < strong>container2Bean 的 transDate。要决定哪个容器 bean 是活动的,就是检查哪个容器 bean 不为 null。

但是,如果容器 Bean 增长,可能会有其他容器 Bean 使用 MyViewBean,并且 @Inject ContainerXXBean 的数量将在 < em>MyViewBean。对此必须有其他解决方案。

请分享如何实现这一目标的想法..谢谢! :-)

谢谢 !

I have a scenario and dont have a clear idea for this to work yet.

Here's the example scenario :

  1. I have a myView.xhtml file that can be displayed as a window popup or a primefaces dialog popup from 2 other container xhtml, let's say container1.xhtml and container2.xhtml (the myView.xhtml is included with something like this ? <ui:include src="myView.xhtml" />)
  2. And, we also have the view-scoped beans, let's call them myViewBean, container1Bean and container2Bean
  3. container1Bean has the property of transactionDate, and container2Bean has a property of transDate
  4. myViewBean's @PostConstruct method will query the database based on a parameter of type date.
  5. On container1.xhtml, if a user clicks on a button that will popup the myView.xhtml, container1Bean should be able to provide it's transactionDate to the myViewBean, and myViewBean will be able to query based on it in the @PostConstruct method.
  6. On container2.xhtml, if a user clicks on a button that will popup the myView.xhtml, container2Bean should be able to provide it's transDate to the myViewBean, and myViewBean will be able to query based on it in the @PostConstruct method.

I was thinking about using @Inject Container1Bean and @Inject Container2Bean in the MyViewBean, so that inside MyViewBean, i can get the transactionDate of container1Bean or transDate of container2Bean. To decide which container bean is active is to check which one is not null.

But what if the container beans grows, there could be other container beans that make use of the MyViewBean, and the @Inject ContainerXXBean will grow in numbers inside the MyViewBean. There must be other solutions for this.

Please share ideas on how to accomplish this .. Thank you ! :-)

Thank you !

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

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

发布评论

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

评论(2

沙沙粒小 2024-10-29 11:35:51

如果您只想访问其他 bean 的内容,则可以通过编程方式进行。

从一个 bean 获取内部 FacesContext 的当前实例。使用该上下文,您可以获取其他 bean 的实例。

请参阅此插图。根据您的问题的标题,您想要了解 bean 之间的一些信息。您可以通过调用其他 bean 的公共方法来完成此操作,这些方法可能是专门为此目的而创建的。 (设置者-获取者)。

If you want to access only the contents of the other beans you can do that programmatic.

Get the current instance of the FacesContext inside from one bean. And using that context you can grab the instance of other beans.

See this illustration. As per the title of your question, you want to some information between beans. You can accomplish this by calling the other bean's public methods, which may be specifically created for this purpose. (setters-getters).

囚你心 2024-10-29 11:35:51

如果container1Bean、myViewBean和container2Bean共享一些属性,为什么不把它们放在一个新的bean中呢? (我们称之为sharedBean,但我相信您会找到一个更适合您的情况的名称)。然后,您可以在每个依赖 bean 中使用 @ManagedProperty 注入共享Bean。让我们尽量不要有循环引用。

我还建议检查您的架构。您确定需要所有这些豆子,完全像这样分区吗?这听起来像是很难维护的事情。

If container1Bean, myViewBean and container2Bean share some properties, why not putting them in a new bean ? (let's call it sharedBean, but I'm sure you'll find a better name adapted to your case). You could then inject sharedBean using @ManagedProperty in every dependent bean. Let's just try not to have circular references.

I'll also suggest to review your architecture. Are you sure you need all thoses beans, partitionned exactly like this? This sounds like something which will be difficult to maintain.

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