5 Replies Latest reply on May 4, 2010 9:56 AM by xck3001

    KeepAlive Behaviour

      Hi,

      We've been making use of the keepalive facility within RichFaces to maintain state between page refreshes. In the process we have noticed a few subtle points related to how keepalives are implemented that may be useful to the community.

      Keepalives can be applied two ways, as an annotation on the bean or as a tag within the page. The implementation of these two methods is quite different (as of version 3.3.1GA). In the case of the annotation all the implementation is provided in org.ajax4jsf.application.AjaxStateManager, while the tag uses a a combination of the org.ajax4jsf.event.AjaxPhaseListener and a tag or handler class depending on wheter your using JSP or facelets.

      The key difference we noticed was that when using the annotation implementation the state bean is restored prior to phase one (restore view) of the JSF life cycle, whereas when using the tag implementation, the state bean is not restored until immediately after phase one. The latter has implications when injecting the state bean into a backing bean that also has bound components. When the bound component is being restored at phase one, the request scoped backing bean is being constructed, and the DI framework tries to inject the state bean. Because the state bean has not been restored the DI framework constructs a new state bean and injects that - effectively wiping out the state.

      It took us a bit if digging to realise that there were differences in how the keepalive is implemented. We're not sure if this should be considered a bug, it seems like it could be fixed by modifying the tag implementation so that the beans are restored in the beforePhase() method of the AjaxPhaseListener rather than in the afterPhase() method. in the meantime we're sticking to using @keepalive rather than the tag.

      Cheers,

      Dean

        • 1. Re: KeepAlive Behaviour
          nbelaevski

          Hi Dean,

          Thanks for investigating and describing this - message has good level of details and is easy to read!
          I've created JIRA issue: https://jira.jboss.org/jira/browse/RF-7807

          • 2. Re: KeepAlive Behaviour
            xck3001

            Hi,

             

            we tried to use some beans like this in JSF2 with RF 3.3.3.FINAL:

             

             

            @ManagedBean
            @RequestScoped
            @KeepAlive
            public class DataBean { ... }
            
            @ManagedBean
            @RequestScoped
            public class DataController {
            
                 @ManagedProperty( value="#{dataBean}" )
                 private DataBean dataBean;
            
                 ...
            }
            

             

            On a test page using some ajax interaction, this worked fine. The injected dataBean instance is always the same as long as the user doesn't leave the view.

             

             

            But in our application the same approach doesn't work. The injected dataBean instance differs each time DataController is called. JSF seems not to notice that there is already a bean of this type present and creates a new one each time. As our application is rather complex, it is hard to determine where the difference to my test page is.

             

            Can you name some requirements for the @KeepAlive - injection to work properly?

             

            Thanks in advance,

             

            Matthias

            • 3. Re: KeepAlive Behaviour
              ilya_shaikovsky

              not investigated the reasons of the failure, just have quick proposal.. how about to use not request scope with keepAlive but just viewScope if you using JSF 2?

              • 4. Re: KeepAlive Behaviour
                xck3001

                We had some issues with @ViewScoped and another library (PrettyFaces). But thanks to your advice I check again and they released a new version which seems to fix this problem.

                 

                I will check if this error stays with @ViewScoped and report back soon.

                 

                Thank you so far!

                • 5. Re: KeepAlive Behaviour
                  xck3001

                  Works just like expected. The only thing I did was replacing @KeepAlive with @ViewScoped, so it must have been some special behaviour of keepAlive. Thank you!