11 Replies Latest reply on Sep 21, 2011 11:58 AM by Jason Porter

    View scope with CDI

    Franek Jop Newbie

      The is no view scope in pure CDI (only in JSF). Can u tell me how can i achieve the same funcionality in pure CDI (or with Seam 3). Do I have to implement my custom scope or someone else made it earlier?

        • 1. Re: View scope with CDI
          Jason Porter Master

          It's already in the Seam Faces module.

          • 2. Re: View scope with CDI
            Didier Schonne Newbie

            Hi,


            It's in Seam Faces module but it doesn't work :


            https://issues.jboss.org/browse/SEAMFACES-44


            You can use this custom annotation http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/ while this bug is not fixed.
            But it's a temporary solution, you can't use Seam Facelet with it and some other minor stuffs.

            • 3. Re: View scope with CDI
              Brian Leathem Master

              Saying the Seam Faces @ViewScoped does not work is a mis-representation.  The @ViewScope does work, but one has to make sure the beans are Serializable, and you can't use it on Producer methods.  Also, if you post back to the same form, be sure to use null, rather than a navigation case if you want the scope to be preserved.


              I've been using it myself in several applications, and it works fine so long as you stay within the boundaries of what it's currently capable of.  And hopefully we can address these issues in a future version of Seam Faces.

              • 4. Re: View scope with CDI
                Didier Schonne Newbie

                Hi Brian,


                I'm surprised because I can't make @ViewScoped working with Weld. I've got a JBoss AS 6.0.0.Final (name="weld-api.jar" specVersion="1.1.Beta2", name="weld-core-jsf-only.jar" specVersion="1.1.0.CR3") and Seam Faces 3.0.2.Final.


                Here my bean :


                import java.io.Serializable;
                
                import javax.annotation.PostConstruct;
                import javax.inject.Named;
                
                import org.jboss.logging.Logger;
                
                //import be.verborgh.enterprise.context.ViewScoped;
                import javax.faces.bean.ViewScoped;
                
                @Named("TestViewscopeBBean")
                @ViewScoped
                public class TestViewscopeBBean implements Serializable {
                
                     private static final long serialVersionUID = 1022525016745203651L;
                
                     private static final Logger logger = Logger.getLogger(TestViewscopeBBean.class);
                     private String chaineATester;
                
                     @PostConstruct
                     public void init() {
                          chaineATester = "Phase init.";
                     }
                
                     public void appelFonction() {
                          logger.info("Appel de méthode.");
                     }
                
                     public String getChaineATester() {
                          return chaineATester;
                     }
                
                     public void setChaineATester(String chaineATester) {
                          this.chaineATester = chaineATester;
                     }
                }



                And my error when I try to display my page :


                2011-08-11 10:42:36,045 GRAVE [javax.enterprise.resource.webcontainer.jsf.application] (http-0.0.0.0-8080-5) Error Rendering View[/pages/test/testViewscope_page_1.xhtml]: java.io.NotSerializableException: org.jboss.weld.bean.ManagedBean
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at java.util.concurrent.ConcurrentHashMap.writeObject(ConcurrentHashMap.java:1246) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_26]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_26]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_26]
                     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at java.util.HashMap.writeObject(HashMap.java:1001) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_26]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_26]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_26]
                     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at java.util.HashMap.writeObject(HashMap.java:1001) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_26]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_26]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_26]
                     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:325) [:2.0.4-b09]
                     at com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:173) [:2.0.4-b09]
                     at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122) [:2.0.4-b09]
                     at com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:159) [:2.0.4-b09]
                     at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225) [:2.0.4-b09]
                     at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:416) [:2.0.4-b09]
                     at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) [:2.0.4-b09]
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273) [:2.0.4-b09]
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273) [:2.0.4-b09]
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273) [:2.0.4-b09]
                     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [:2.0.4-b09]
                     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [:2.0.4-b09]
                     at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [:2.0.4-b09]
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) [:2.0.4-b09]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
                     at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) [:6.0.0.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
                     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
                     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
                     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
                     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
                     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
                     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
                     at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
                     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
                     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
                     at java.lang.Thread.run(Thread.java:662) [:1.6.0_26]
                
                2011-08-11 10:42:36,065 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/test.war.public].[Faces Servlet]] (http-0.0.0.0-8080-5) "Servlet.service()" pour la servlet Faces Servlet a généré une exception: java.io.NotSerializableException: org.jboss.weld.bean.ManagedBean
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at java.util.concurrent.ConcurrentHashMap.writeObject(ConcurrentHashMap.java:1246) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_26]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_26]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_26]
                     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at java.util.HashMap.writeObject(HashMap.java:1001) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_26]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_26]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_26]
                     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at java.util.HashMap.writeObject(HashMap.java:1001) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_26]
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_26]
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_26]
                     at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_26]
                     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) [:1.6.0_26]
                     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) [:1.6.0_26]
                     at com.sun.faces.renderkit.ClientSideStateHelper.doWriteState(ClientSideStateHelper.java:325) [:2.0.4-b09]
                     at com.sun.faces.renderkit.ClientSideStateHelper.writeState(ClientSideStateHelper.java:173) [:2.0.4-b09]
                     at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122) [:2.0.4-b09]
                     at com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:159) [:2.0.4-b09]
                     at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225) [:2.0.4-b09]
                     at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:416) [:2.0.4-b09]
                     at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) [:2.0.4-b09]
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273) [:2.0.4-b09]
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273) [:2.0.4-b09]
                     at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273) [:2.0.4-b09]
                     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [:2.0.4-b09]
                     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [:2.0.4-b09]
                     at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [:2.0.4-b09]
                     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) [:2.0.4-b09]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
                     at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) [:6.0.0.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.0.0.Final]
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
                     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
                     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
                     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
                     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
                     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
                     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
                     at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
                     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
                     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
                     at java.lang.Thread.run(Thread.java:662) [:1.6.0_26]



                What is my mistake if @ViewScoped works well ?


                Thanks you in advance for your help.



                • 5. Re: View scope with CDI
                  Brian Leathem Master

                  Sounds like you are trying to use a recent version of Weld with JBoss AS 6?  Is that the case?  Have you tried your test with the version of Weld that ships with AS 6 (vanilla AS6)?  Or try AS7?


                  Also, try deploying the faces-viewconfig example.  It has @ViewScoped beans in it, and is known to work.


                  Brian


                  • 6. Re: View scope with CDI
                    Thomas Andraschko Newbie

                    As far as i understand:
                    Mojarra does not serializable the bean into viewmap - MyFaces does.


                    So Weld with Seam ViewScoped does only work with Mojarra but how about clustering then?


                    ViewScoped with OWB works for example without problems because OWB beans are serializable.

                    • 7. Re: View scope with CDI
                      Didier Schonne Newbie

                      Brian,


                      I've got a JBoss AS 6.0.0.Final and the version of Weld is the one which is shipped. I didn't try with AS7.


                      I will try the faces-viewconfig.


                      Didier.

                      • 8. Re: View scope with CDI
                        Didier Schonne Newbie

                        Ok I found why, I had an error with the view scope because my javax.faces.STATE_SAVING_METHOD was client. With server it works.

                        • 9. Re: View scope with CDI
                          Marcos Antonio Newbie

                          Didier Schonne wrote on Sep 08, 2011 08:06:


                          Ok I found why, I had an error with the view scope because my javax.faces.STATE_SAVING_METHOD was client. With server it works.


                          Yes, I've just had this same problem. After changing the state from client to server the error went away. But I think that it is very important to save state in the client as it save resources like memory on the server. It would be nice if there was another way instead of this to prevent this bug. Saving state in the client is very important. Again, as I already said before, I hope that the @ViewScoped is included in the next version of the CDI specification, so we don't need to use libraries to use such an important and handy scope in our applications.


                          Marcos

                          • 10. Re: View scope with CDI
                            Anton Lisovenko Newbie

                            Saving on the client increases page size thouroughly and also increases CPU for parsing/serializing view at the page. Server mode has its shortcomings, but makes http responses much faster/

                            • 11. Re: View scope with CDI
                              Jason Porter Master

                              That's been the typical argument between the two. It's a very fine line to walk for each application to consider.