12

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.

AlanObject
  • 9,613
  • 19
  • 86
  • 142
  • I would call `conversation.end()` if and only if I want to explicit end the conversation for business logic reasons. My "solution" to your question is to just let the timeout to end the conversation. If the user closes the tab, she would not be able to use the conversation anyway. This is no problem to my applications - maybe is not an acceptable solution to your one, for performance questions, so I am curious about _real_ answers too. – brandizzi Aug 16 '11 at 16:39

2 Answers2

3

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.

Dar Whi
  • 822
  • 5
  • 14
  • I'll check that out, but I was hoping to stay with the EE specification. Thanks for the pointer. – AlanObject Aug 19 '11 at 05:47
  • 1
    CDI intended the creation and usage of portable CDI extensions. It's like you usually use a component library for JSF because the specification will never include advanced components. – Dar Whi Aug 19 '11 at 11:45
  • 1
    I just got around to reading the CODI documentation. If it does what it says I think it is more than desirable it become indispensable! How are people using JSF 2 without it? Thanks @"Dar Whi" – AlanObject Sep 20 '11 at 18:57
1

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

Catweazle
  • 619
  • 12
  • 25