开发者

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 m开发者_如何学Goore 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.


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.


update: JSF 2.2 (jsr 344, in early draft review) is adding an @FlowScoped CDI scope for this. More info...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜