JSF CDI:会话范围bean [s]最佳实践
我目前正在学习JSF 2.0,我很高兴这个会话范围功能的存在,这对于在同一页面上打开新选项卡或新窗口并拥有单独的资源非常有用,而不是相互覆盖。
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.
但我很好奇如何以一种好的方式实现这一点,关于何时开始对话以及何时关闭它。
But im curious on how to implement this in a good way, about when to start the conversation and when to close it.
在我的例子中,我为每个JSF页面都有每个CDI bean。让我们说我有一个菜单,当它被点击时,这将导致页面A,并且从A,可能导致B,B可能导致C,C可能导致D,所有这4个页面都连接在一起链。
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.
可以从B或C或D bean访问A的bean属性,也可以从C或D bean等访问B的属性。
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.
现在我很困惑:
- 对话范围内所有这些ABCD是否应为
或不,或
也许只是A?因为我认为
有时来自ABCD链外的另一个页面
,比如页面
F,它可以导航到页面B,
虽然我不知道如何供应
还有来自bean B的数据。 - 是否所有这些ABCD都应该是
组合成一个bean - where and when开始
对话,我想到
构造函数,但我不认为这是一个
的好主意,因为我更喜欢在第一次访问
时开始
的对话页面,而不是豆 - 在何时何地停止
对话,以便不会有
未使用的资源闲置
- 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
请分享您的想法。
JSF 2提供请求,视图,会话和应用程序范围。 CDI引入了Conversation范围,但更重要的是,它引入了一个标准,通过该标准可以将新范围添加到平台。
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.
您描述的范围可能更适合自定义范围,如窗口范围。实现此范围的两个项目是:
The Scope you are describing is probably better suited by a custom scope like a window scope. Two projects implementing this scope are:
- Apache MyFaces CODI
- IceFaces有一个JSF(非CDI)窗口范围实现。
- Apache MyFaces CODI
- IceFaces has a JSF (non-CDI) Window scope implementation.
尽管如此,我还是鼓励你重新思考你的bean结构。我自己非常喜欢View范围,再加上JSF 2视图参数,可以将信息从一个页面传播到另一个页面(以及从一个View范围实例传播到另一个页面)。
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).
MyFacesView Access范围看起来像另一种简洁的方法,只要您浏览的页面保持对该范围的引用,bean就会保留在范围内。
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.