[英]How to store a bean per conversation
Is there a way to store some bean inside the conversation context? 有没有办法在会话上下文中存储一些bean? Ie for each new conversation, a new separate bean is created belonging to it.
即,对于每个新对话,创建属于它的新的单独bean。
The easiest way to do what you want is to declare a ConversationScoped managed bean or EJB where JSF2 manages the scope. 执行所需操作的最简单方法是声明一个ConversationScoped托管bean或EJB,其中JSF2管理范围。
There are some good explanations here: 这里有一些很好的解释:
... any of which will do a better job than I will. ......任何比我更好的工作。 The very short version is that you annotate a bean - which can be a plain POJO that follows the bean conventions - with the
@ConversationScoped
annotation. 非常简短的版本是你使用
@ConversationScoped
注释来注释bean - 它可以是遵循bean约定的普通POJO。 You then @Inject
a Conversation
object, which you can use to begin()
and end()
conversations. 然后
@Inject
一个Conversation
对象,您可以使用它来begin()
和end()
对话。 Inject this @ConversationScoped
bean into other things. 将此
@ConversationScoped
bean注入其他内容。 The Conversation.begin
and Conversation.end
methods control its lifecycle. Conversation.begin
和Conversation.end
方法控制其生命周期。
There's a bit much code to just post here, but the above links should help. 这里有一些代码可以发布,但上面的链接应该有所帮助。
An alternative to @ConversationScoped
POJO managed beans can be @Stateful @ConversationScoped
EJBs. @ConversationScoped
POJO托管bean的替代方法可以是@Stateful @ConversationScoped
EJB。 They can be really handy when you need EJB services in a conversation. 当您在对话中需要EJB服务时,它们可以非常方便。
For some of the conceptual background and detail read the CDI/Weld reference on scopes - and the rest of the CDI/Weld manual. 对于一些概念背景和细节,请阅读关于范围的CDI / Weld参考 - 以及CDI / Weld手册的其余部分。 It's really well written and really good.
这真的写得很好,真的很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.