如何在JSF 2中的bean之间传递信息? [英] How to pass information between beans in JSF 2?

查看:90
本文介绍了如何在JSF 2中的bean之间传递信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方案,尚不清楚要解决这个问题.

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

这是示例场景:

  1. 我有一个 myView.xhtml 文件,该文件可以从 2个其他xhtml容器中显示为窗口弹出窗口或素面对话框弹出窗口 ,比如说 container1.xhtml container2.xhtml ( myView.xhtml 包含在其中吗?<ui:include src="myView.xhtml" />)
  2. 而且,我们还有视野范围的bean,我们将其称为 myViewBean container1Bean container2Bean
  3. container1Bean 具有 transactionDate container2Bean 的属性具有 transDate
  4. 的属性
  5. myViewBean 的@PostConstruct方法将基于类型为date 的参数查询数据库 .
  6. 在container1.xhtml上,如果用户单击将弹出 myView.xhtml container1Bean 的按钮, strong>应该能够将其 transactionDate 提供给 myViewBean myViewBean 将能够在@PostConstruct方法中基于它进行查询.
  7. 在container2.xhtml上,如果用户单击将弹出 myView.xhtml container2Bean 的按钮, strong>应该能够将其 transDate 提供给 myViewBean myViewBean 将能够在@PostConstruct方法中基于它进行查询.
  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.

我当时正在考虑在 中使用 @Inject Container1Bean @Inject Container2Bean > MyViewBean ,这样我就可以在 MyViewBean 中获取container1Bean的 transactionDate container2Bean的transDate .要确定哪个容器bean是活动的,就是要检查哪个不为null.

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.

但是,如果容器bean增长了,那么可能会有其他容器豆利用MyViewBean,并且 @Inject ContainerXXBean 的数量将在 MyViewBean .对此必须有其他解决方案.

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 ! :-)

谢谢!

推荐答案

如果container1Bean,myViewBean和container2Bean共享某些属性,为什么不将它们放在新的bean中呢? (我们称它为sharedBean,但我相信您会找到一个适合您情况的更好的名称).然后,您可以在每个从属bean中使用@ManagedProperty注入sharedBean.让我们尝试不使用循环引用.

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.

我还将建议您回顾一下您的体系结构.您确定需要所有像这样分区的bean吗?这听起来很难维护.

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.

这篇关于如何在JSF 2中的bean之间传递信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆