Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Use @ConversationScoped

I am writing a JSF 2.0 application, and I want to use CDI annotations instead of the "equivalent" JSF annotations. In other words, @Model or @Named instead of @ManagedBean, and @Inject instead of @ManagedProperty.

The only thing I cannot get to work is @ViewScoped which is necessary for AJAX components. The only reliable work-around is to use @SessionScoped, which is not a good practice.

What is the correct practice? As much as I search I just get more confused.

This is on GlassFish 3.1.1, which I understand has Weld 1.1.0 in it.

UPDATE: The original form of this question said that I could not get @ConversationScoped to work. Since then I found my error and I did get it to work like so:

@Model
@ConversationScoped
public class Abean implements Serializable {

@Inject Conversation conversation;

// stuff omitted for brevity

public String getSomething() {
    if (conversation.isTransient()) conversation.begin();
    return "something";
}

This seems to do the trick. However now my question is changed. Where exactly are you supposed to call conversation.end()? Do I have to write a filter to detect when the user leaves the page? Or if it is left alone, just when would the Abean instance be de-referenced?

SECOND UPDATE: A very good discussion of CDI's @ConversationScoped I found here.

I am still left with the problem of how to call conversation.end(). My bean provides stateful backing to a data table browser updated via AJAX, and the optimal place to call end() is when the user navigates away from the page. However short of writing a filter to monitor the pages I don't really see any way of doing that. Any suggestion of "best practice" is welcome.

like image 804
AlanObject Avatar asked Aug 15 '11 04:08

AlanObject


1 Answers

That's way simpler with the (CDI) scopes of MyFaces CODI. They have a better @ConversationScoped and you will love the @ViewAccessScoped for what you are trying.

like image 176
Dar Whi Avatar answered Oct 09 '22 04:10

Dar Whi