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.
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.
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