对话范围是否可以适当替代视图范围?

发布于 2024-12-23 16:07:25 字数 407 浏览 2 评论 0原文

在 JSF 2.0 中,视图范围最明显的用例是具有潜在多个 AJAX 回发的单个页面。使用 CDI 而不是 JSF 托管 bean 会使我们失去视图范围,因此我们要么自己实现,要么使用(可能存在错误的)第三方实现,要么使用会话范围。

我的问题:在典型的 AJAX 情况下,对话范围是否值得替代视图范围?与视图范围一样,它是否允许每个会话有多个实例?有哪些陷阱?

我知道其中一个陷阱,即当用户离开页面时,对话范围不会自动删除,而是在超时后删除。但我不确定当用户在对话超时之前返回该页面时会发生什么。

更新

对话范围确实支持每个会话多个实例。 这本书说明了这一点,我可以使用 ch 中的代码来确认这一点。 2.

In JSF 2.0, the most obvious use-case for the view scope is a single page with potentially multiple AJAX post-backs. Using CDI instead of JSF managed beans leaves us without the view scope so we're either left to implement our own, use a (possibly bug-ridden) third party implementation or use the conversation scope.

My question: Is the conversation scope a worthy substitute for the view scope in the typical AJAX situation? Like the view scope, does it allow multiple instances per session? What are the pitfalls?

I'm aware of one of the pitfalls, namely that the conversation scope isn't automatically deleted when the user navigates away from the page, but instead is deleted after a time-out. But I'm not sure what happens when the user navigates back to that page before the conversation has timed out.

UPDATE

The conversation scope does indeed support multiple instances per session. This book states as much and I was able to confirm this using code from ch. 2.

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

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

发布评论

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

评论(1

难如初 2024-12-30 16:07:25

在任何@ConversationScoped CDI bean中,您必须具有以下字段:

@Inject
private Conversation conversation; 

每当您想要开始对话时,您都需要检查bean是否处于瞬态状态。否则,将抛出IllegalStateException。它会是这样的:

public void beginConversation() {
  if (conversation.isTransient()) conversation.begin();
}

通过这样做,您的 bean 将处于长时间运行状态。因此,如果用户离开该页面并稍后返回,您可以随时检查他的对话是否超时,并将他带到他离开的页面。

此外,我已经将 @ViewScoped ManagedBeanCDI bean 一起使用了一段时间。您仍然可以使用 @InjectCDI bean 注入到 MangedBean 中。但我认为你不能采取相反的做法。无论如何,我不知道这是否会导致以后发生任何不好的事情。然而,到目前为止,我还没有遇到任何问题。如果你真的想使用@ViewScoped,我想你可以尝试:P。

更新:

在典型的 AJAX 情况下,对话范围是否值得替代视图范围?

我认为 @ConversationScoped 无法完全取代 @ViewScoped

与视图范围一样,它是否允许每个会话有多个实例?

不可以,每个会话不能有多个实例。正如我所提到的,如果您在旧对话仍处于长时间运行状态时启动新对话,您将收到 IllegalStateException。

有哪些陷阱?

嗯,@ViewScoped 相对于 @RequestScoped 的主要优点之一是,每次用户将表单提交到同一个视图时,您不需要重新启动数据。然而,对于@ConversationScoped,这个优势被过度利用了。虽然这个问题不像使用 @SessionScoped 那样严重,但只要 @ConversationScoped bean 存在,您仍然需要保存启动的数据。对话时间越长,您可能需要保存的数据就越多。

In any @ConversationScoped CDI beans, you must have the following field:

@Inject
private Conversation conversation; 

Whenever you want to begin the conversation, you need to check if the bean is in transient state. Otherwise, IllegalStateException will be thrown. It would be something like this:

public void beginConversation() {
  if (conversation.isTransient()) conversation.begin();
}

By doing this, your bean will be in the long-running state. Hence, if the user navigate aways from the page and navigates back later on, you can always check if his conversation has timed-out or not and bring him to the page where he left.

Besides, I have been using @ViewScoped ManagedBean together with CDI bean for a while. You can still use @Inject to inject a CDI bean into a MangedBean. I don't think you can do the other way around though. Anyway, I have no idea if this would cause anything bad to happen later. However, up until now, I have never met any issues. If you really want to use @ViewScoped, I think you can try :P.

UPDATE:

Is the conversation scope a worthy substitute for the view scope in the typical AJAX situation?

I don't think @ConversationScoped can ever fully replace @ViewScoped.

Like the view scope, does it allow multiple instances per session?

No, you cannot have multiple instances per session. As I mentioned, if you start a new Conversation while the old conversation is still in long-running state, you will get IllegalStateException.

What are the pitfalls?

Well, one of the main advantages of @ViewScoped over @RequestScoped is that you don't need to re-initiate data every time the user submits the form to the same View. However, with @ConversationScoped, this advantage is over-used. While this problem is not as serious as if you use @SessionScoped, you still need to hold the initiated data as long as the @ConversationScoped bean lives. The longer the conversation, the more data you may need to hold.

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