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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With