Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the conversation scope an appropriate substitute for the view scope?

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.

like image 717
Steve Avatar asked Jan 01 '12 12:01

Steve


1 Answers

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.

like image 137
Mr.J4mes Avatar answered Sep 21 '22 03:09

Mr.J4mes