java JSF CDI:对话范围 bean[s] 最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4244475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:29:07  来源:igfitidea点击:

JSF CDI : Conversation scope bean[s] best practice

javajsfjsf-2

提问by bertie

I'm currently learning about JSF 2.0 and im so glad for the existence of this conversation scope feature, which is very helpful in opening a new tab or a new window on the same page and having separate resources, not overriding one another.

我目前正在学习 JSF 2.0,我很高兴这个对话范围功能的存在,这对于在同一页面上打开新选项卡或新窗口并拥有单独的资源而不是相互覆盖非常有帮助。

But im curious on how to implement this in a good way, about when to start the conversation and when to close it.

但我很好奇如何以一种好的方式实现这一点,关于何时开始对话以及何时结束对话。

In my case, i have each CDI bean for each JSF page. And let's say that i have a menu, and when it's clicked, this will lead to page A, and from A, could lead to B, B could lead to C, C could lead to D, all these 4 pages are connected in one chain.

就我而言,每个 JSF 页面都有每个 CDI bean。假设我有一个菜单,当它被点击时,这将导致页面 A,从 A,可能导致 B,B 可能导致 C,C 可能导致 D,所有这 4 个页面连接在一起链。

Accessing A's bean properties from B or C or D beans is possible, accessing B's properties is also possible from C or D beans and so forth.

可以从 B 或 C 或 D bean 访问 A 的 bean 属性,也可以从 C 或 D bean 访问 B 的属性,等等。

Now im quite confused about :

现在我很困惑:

  • whether all these A B C D should be in conversation scope or not, or perhaps just A ? Because i think sometimes from another page that is outside the ABCD chain, like a page F, it could navigate to page B, although i dont know how to supply the data to the bean B yet.
  • whether all these A B C D should be combined into one bean
  • where and when to start the conversation, im thinking about the constructor, but i dont think it's a good idea, because i prefer starting the conversation when first accessing the page, not the bean
  • where and when to stop the conversation, so that there wont be unused resources hanging around
  • 所有这些 ABCD 是否应该在对话范围内,或者只是 A ?因为我认为有时从 ABCD 链之外的另一个页面,例如页面 F,它可以导航到页面 B,尽管我还不知道如何将数据提供给 bean B。
  • 是否应将所有这些 ABCD 合并为一个 bean
  • 何时何地开始对话,我在考虑构造函数,但我认为这不是一个好主意,因为我更喜欢在第一次访问页面时开始对话,而不是 bean
  • 何时何地停止对话,以便不会有未使用的资源闲逛

Please share your thoughts on this.

请分享您对此的看法。

回答by Brian Leathem

JSF 2 provides Request, View, Session, and Application scopes. CDI introduces the Conversation scope, but more importantly, it introduces a standard by which new scopes can be added to the platform.

JSF 2 提供请求、视图、会话和应用程序范围。CDI 引入了 Conversation 范围,但更重要的是,它引入了一个标准,通过该标准可以将新范围添加到平台。

The Scope you are describing is probably better suited by a custom scope like a window scope. Two projects implementing this scope are:

您所描述的范围可能更适合自定义范围,例如窗口范围。实施此范围的两个项目是:

  1. Apache MyFaces CODI
  2. IceFaces has a JSF (non-CDI) Window scope implementation.
  1. Apache MyFaces CODI
  2. IceFaces 有一个 JSF(非 CDI)窗口范围实现

Nevertheless, I would encourage you to rethink your bean structure. I've become quite fond of the View scope myself, coupled with the JSF 2 view parameters to propagate information from one page to another (and from one View scope instance to another).

尽管如此,我还是鼓励您重新考虑您的 bean 结构。我自己已经非常喜欢 View 范围,再加上 JSF 2 视图参数,可以将信息从一个页面传播到另一个页面(以及从一个 View 范围实例传播到另一个页面)。

MyFaces "View Access" scope seems like another neat approach, where a bean stays in scope so long as the pages you navigate through maintain a reference to that scope.

MyFaces“查看访问”范围似乎是另一种巧妙的方法,只要您浏览的页面维护对该范围的引用,bean 就会保持在范围内。