CDI missing @ViewScoped and @FlashScoped
Asked Answered
P

2

6

Why is Java EE 6 CDI missing the @ViewScoped and @FlashScoped annotations? (especially the former makes me wonder, because CDI stems from the Seam world, which already knew the very similar ScopeType.PAGE...)

What are the recommended workarounds when using CDI? Use Seam 3?

Thanks

Predicative answered 18/10, 2011 at 20:3 Comment(0)
R
12

The @ViewScoped is specific to the MVC framework JSF, not to the dependency injection framework CDI. The view scope lives as long as you're interacting with the same JSF view. CDI has not really a notion of "views". The CDI alternative to that is @ConversationScoped which lives longer than the request scope, but shorter than the session scope. You only have to control the termination yourself. You can if necessary use MyFaces CODI to bridge the JSF @ViewScoped to CDI @Named beans. The upcoming JSF 2.2 will have a CDI compatible @ViewScoped in the javax.faces.view package.

The @FlashScoped doesn't exist in JSF. The JSF flash scope exist of basically a map which is backed by a short-living cookie which survives HTTP redirects. You cannot let JSF put managed beans in this scope. You've to manually put/get the values in/from the map yourself and/or use the #{flash} reference in EL which basically refrences the map. Seam Faces has however hijacked the JSF specific javax.faces.bean package for its @FlashScoped annotation, but this is definitely not from standard JSF API.

See also:

Relume answered 18/10, 2011 at 20:8 Comment(6)
This CDI / JSF stuff is still killing me conceptually. But it makes sense to know CDI has no notion of views. I then have to get a hold on this: verborgh.be/articles/2010/01/06/… ... then Seam 3 must rather be sitting between CDI and JSF + other Java EE techs than on top of CDI. Hard to grasp IMHO.Predicative
It's indeed confusing if you're new to it. I strongly suggest you to not mix various dependency injection approaches into a single webapp for now. This way you can only concentrate on the annotations of a certain framework. As to Seam, that's a completely different framework. It was several years ago however the innotator behind the annotation based injection approach which is later been taken over by JSF2 and CDI.Relume
Seam Faces page still claims that it implements javax.faces.bean.FlashScoped. It sounds like the spec changed regarding that "scope". Do you maybe know what exactly happened there and what does Seam actually do?Callean
It has been a while since I used Seam, I develop straight CDI now. There is a @RequestScope in CDI which corresponds highly to Flash Scope.Lodgings
@Tristan: the CDI request scope is absolutely not the same as JSF flash scope. It has an identical lifetime as JSF request scope. Your confusion is probably caused by the state of the JSF component tree. It still keeps its own view scope across postbacks.Relume
I just tried the new JSF 2.2 @javax.faces.view.ViewScoped successfully. It´s nice that it´s working and I´m quite happy that I now can combine CDI with ViewScoped-scope - but I see that as one more design flaw of Java EE. Now we have scopes for JSF in javax.faces.bean, we have scopes for CDI (usable in JSF) in javax.enterprise.context AND we have the new one in javax.faces.view for CDI/JSF.Hulbert
P
1

You can implement the context and use the @NormalScope to create your own CDI Scope witout using any other framework or waiting for the new JEE7

  • CDI fires an event AfterBeanDiscovery after each bean call
  • You can use CDI extension to @Observes this event and add your context implementation
  • In your scope implementation you can :
    1. Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
    2. Use CreationalContext if the bean name from first step is not found to create it in the FacesContext ViewRoot Map

for a more in-depth explanation i recommand this link : http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/

Pylle answered 28/4, 2013 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.