1 2 Previous Next 24 Replies Latest reply on Sep 17, 2011 7:42 AM by Marcos Antonio

    JSF2 ViewScope in CDI

    Alexander Bell Newbie


      I cannot find a appropriate Annotaion for JSF2's ViewScope.
      The ViewScope is very useful if you stay on the same page during some request (it replaces the t:saveState Tag from Tomahawk) because you don't have to manage a Conversation and you don't have a long running Session.
      I try to annotate my Bean with @ManagedBean and @ViewScope (both annotations from java.faces... package) but unfortunately the injection doesn't work with JSF Beans.
      Is there a way to use the ViewScope of JSF2 together with Weld?


        • 2. Re: JSF2 ViewScope in CDI
          Alexander Bell Newbie

          thanks nicklas, looks good.
          so for now I have to include seam2 or are there any weld-ext libraries?

          BTW what is the FlashScope?


          • 4. Re: JSF2 ViewScope in CDI
            steven verborgh Newbie

            i wrote a small howto on how to create a custom context in CDI. As an example I ported the JSF ViewScoped annotation to CDI by means of a custom context. It is not really that hard to understand. If you have any questions just ask... The example might be useful until seam 3 gets released.

            • 5. Re: JSF2 ViewScope in CDI
              Gavin King Master

              Looks good, I hope you can contribute the code to Seam3. I'll link this tutorial on our blog.

              • 6. Re: JSF2 ViewScope in CDI
                Alexander Bell Newbie

                pretty cool, thanks steven

                • 7. Re: JSF2 ViewScope in CDI
                  Nicklas Karlsson Master

                  Is there any way to observe when the view has been destroyed so that the context is properly cleaned out?

                  There is a JSF 2 event fired when a custom scope is being destroyed but is that triggered on @ViewScope (the JSF variant)? You might be able to sneak in a bean which takes down the context in the @PreDestroy but that sounds a bit hackish. Does JSF call any @PreDestroy on context stuff when it deconstructs it? And does it require some hook with @CustomScoped?

                  The duplication of scopes and managed beans in JSF and CDI is a bit confusing at a quick glance.

                  • 8. Re: JSF2 ViewScope in CDI
                    steven verborgh Newbie

                    I had a quick look at the api and there seems to be a PreDestroyViewMapEvent. I assume that JSF calls the lifecycle callbacks on beans with the javax.faces.ManagedBean annotation(, but when using a CDI context and @Named annotation I would think this (should) get handled by CDI?)

                    • 9. Re: JSF2 ViewScope in CDI
                      Ryan Collins Newbie

                      I tried using Steven Verborgh example of doing a port of the JSF viewscope.  I have it almost working, but I am not sure on his last point of

                      All that we now need to do is provide a file with the name javax.enterprise.inject.spi.Extension in the META-INF/services folder of you application in order to allow the java ServiceLoader that weld uses to load the extension.

                      I created this file, but I didn't put anything in the file (does something need to be put in the file?)? 

                      I end up getting the following exception.

                      type Exception report


                      descriptionThe server encountered an internal error () that prevented it from fulfilling this request.


                      javax.servlet.ServletException: No active contexts for scope type faces.ViewScoped

                      root cause

                      javax.enterprise.context.ContextNotActiveException: No active contexts for scope type faces.ViewScoped

                      note The full stack traces of the exception and its root causes are available in the GlassFish v3 logs.

                      • 10. Re: JSF2 ViewScope in CDI
                        John Leed Newbie

                        I created this file, but I didn't put anything in the file (does something need to be put in the file?)?

                        I have 4 files for implementing ViewScoped:

                        1. the ViewScoped annotation

                        2. ViewContext, which implements javax.enterprise.context.spi.Context.

                        3. ViewContextExtension, which implements javax.enterprise.inject.spi.Extension

                        4. META-INF/services/javax.enterprise.inject.spi.Extension, which contains the fully qualified class name of item 3 and nothing else.

                        • 11. Re: JSF2 ViewScope in CDI
                          steven verborgh Newbie

                          I put the code online at GitHub - Verborghs together with some other extensions I use in my courses (@ELValue [cfr.  JSF @ManagedProperty], @FlashScoped, @Flash, ...). I tried integrating them in seam 3 but since the faces module didn't/doesn't compile at the time of trying I haven't contributed them yet but as far as I care you do what you want with them. If I find some time I'll add some unit tests.

                          • 12. Re: JSF2 ViewScope in CDI
                            John Asermely Newbie

                            Great post on ViewScope it worked for me!  Any serious JSF 2.0 application using Weld is going to need this.  Is the first time we are going to see an official release of this functionality going to be in seam 3, or might we see it earlier in some sort of portable extension release?

                            Just to be clear, the ultimate idea is that when using weld scopes, you shouldn't be using any of the JSF scopes, if you want injection to work properly?  I think that because some JSF scope equivalents are missing from Weld, and I understand why, it is leading to confusion over there being two separate definitions of scopes: JSF's and Weld's.

                            I guess my recommendation is that there should be a separate portable extension jar for users who would like to use Weld with JSF 2.0 without having to wait for seam 3.0 or to use the entirety of seam 3.0 when it does get here.

                            • 13. Re: JSF2 ViewScope in CDI
                              Lincoln Baxter III Master

                              The preferred usage would be to avoid any and all JSF @...Scope annotations, certainly @ManagedBean should be avoided in all cases -- use @Named instead, but unfortunately with @ViewScoped and @FlashScoped, these are JSF specific features, and probably belong in the javax.faces namespace.

                              We're prototyping this functionality in Seam 3 now: Seam 3 Faces - Scopes Support

                              The Seam 3 Faces module will be completely portable and will not depend on the entirety of Seam 3's Modules - part of the new architecture for Seam 3, because people rarely need all of Seam at once -- it just does too much ;)

                              When you think of Seam 3, think of small, portable, modular extensions to bridge the gap between CDI, and other JEE technologies such as JMS, JSF, EJB, JPA, and other helpful things :)

                              I'm hoping to have a snapshot of this functionality out as soon as possible, since it should be just about done now. Keep your eyes peeled. Follow @jbossseam on twitter, or follow me @lincolnthree for updates -- or just keep your eyes peeled on seamframework.org :)

                              • 14. Re: JSF2 ViewScope in CDI
                                Lincoln Baxter III Master

                                Also, if any of you guys want to get involved in the development of said Seam 3 Faces module -- feel free, let me know! The SVN repo is located here(1) and should be buildable from now on ;) I had a little cleaning up to do when I took over / started over.

                                (1) http://anonsvn.jboss.org/repos/seam/modules/faces/trunk/

                                (2) http://seamframework.org/Documentation/Seam3FacesModuleDesignNotes

                                1 2 Previous Next