5 Replies Latest reply on May 4, 2009 5:52 AM by Nick Belaevski

    a4j:keepAlive doesn't work as expected

    Stasa Medjedovic Newbie

      I'm trying to test a4j:keepAlive tag. I have three simple .jsp files: test1.jsp, test2.jsp, test3.jsp. All of them have buttons test1, test2, test3, which let me navigate to the next page in order:

      test1.jsp -> test2.jsp -> test3.jsp -> test1.jsp -> ... etc.

      Here are those .jsp files:

      test1.jsp

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      
      <%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
      <%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
      
      <f:view>
       <h:form>
       <h:inputText value="#{test.testValue}" />
      
       <!-- staticly navigate to test2.jsp -->
       <h:commandButton value="test1" action="test1" />
       </h:form>
      </f:view>
      


      test2.jsp
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      
      <%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
      <%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
      
      <f:view>
       <h:form>
       <h:outputText value="#{test.testValue}" />
      
       <!-- staticly navigate to test3.jsp -->
       <h:commandButton value="test2" action="test2" />
       </h:form>
      </f:view>
      


      test3.jsp
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      
      <%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
      <%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
      
      <f:view>
       <h:form>
       <h:outputText value="#{test.testValue}" />
      
       <!-- staticly navigate to test1.jsp -->
       <h:commandButton value="test3" action="test3" />
       </h:form>
      </f:view>
      


      This way I'm testing on which pages is managed bean value #{test.testValue}, which I entered on test1.jsp, available. 'test' is a managed bean defined in faces-config.xml, with request scope.

      faces-config:
       <navigation-rule>
       <navigation-case>
       <from-action>test1</from-action>
       <to-view-id>/test2.jsf</to-view-id>
       </navigation-case>
       </navigation-rule>
       <navigation-rule>
       <navigation-case>
       <from-action>test2</from-action>
       <to-view-id>/test3.jsf</to-view-id>
       </navigation-case>
       </navigation-rule>
       <navigation-rule>
       <navigation-case>
       <from-action>test3</from-action>
       <to-view-id>/test1.jsf</to-view-id>
       </navigation-case>
       </navigation-rule>
      
       <managed-bean>
       <managed-bean-name>test</managed-bean-name>
       <managed-bean-class>test.Test</managed-bean-class>
       <managed-bean-scope>request</managed-bean-scope>
       </managed-bean>
      



      Situations which confuse me are:

      1) When I put @KeepAlive annotation on 'test' managed bean, bean behaves like a JSF session bean. I do not need to put any <a4j:keepAlive> tags (and I don't), because it is always available. The value entered in test1.jsp, prints on test2.jsp, test3.jsp, and on test1.jsp back again. I don't understand this kind of redundancy with @KeepAlive, when the same funcionality can be achieved with JSF session scope?!

      This led me to conclusion that I shoudn't use @KeepAlive annotation, because this functionality is not what I want. What I want is, to save bean state only on those pages on which I want ...

      2) When I remove @KeepAlive annotation from 'test' managed bean, and try to put <a4j:keepAlive> tag on test1.jsp only, or on all three pages, nothing gets saved, it behaves like a regular JSF request scope bean.

      RichFaces documentation says nothing about this, forums and google also. Now, I really don't know what kind of functionality I can expect from a4j:keepAlive, when it behaves like a regular request and session scoped managed beans, and when I don't see any change in behaviour when I use/not use <a4j:keepAlive> tag?

        • 1. Re: a4j:keepAlive doesn't work as expected
          Ilya Shaikovsky Master

          1) but if you will navigate to page where the bean not used and then return back - it will be recreated.

          2) it seems that ajaxOnly is true by default while using the tag.

          • 2. Re: a4j:keepAlive doesn't work as expected
            Stasa Medjedovic Newbie

             

            "ilya_shaikovsky" wrote:
            1) but if you will navigate to page where the bean not used and then return back - it will be recreated.

            ... which is the same functionality I can achieve with declaring managed bean as session scoped.

            "ilya_shaikovsky" wrote:
            2) it seems that ajaxOnly is true by default while using the tag.

            I tried with ajaxOnly="false", nothing happens, it still behaves like a regular request scope bean.

            • 3. Re: a4j:keepAlive doesn't work as expected
              Ilya Shaikovsky Master

               


              "ilya_shaikovsky" wrote:
              1) but if you will navigate to page where the bean not used and then return back - it will be recreated.


              ... which is the same functionality I can achieve with declaring managed bean as session scoped.

              No. session scoped bean will not be recreated. You could freely check this as mentioned. Apply some value navigate to the page where the bean isn't referenced and return back. The value will be the same. And keepAlived bean will be recreated in this case.

              • 4. Re: a4j:keepAlive doesn't work as expected
                Stasa Medjedovic Newbie

                 

                "ilya_shaikovsky" wrote:

                "ilya_shaikovsky" wrote:
                1) but if you will navigate to page where the bean not used and then return back - it will be recreated.


                ... which is the same functionality I can achieve with declaring managed bean as session scoped.

                No. session scoped bean will not be recreated. You could freely check this as mentioned. Apply some value navigate to the page where the bean isn't referenced and return back. The value will be the same. And keepAlived bean will be recreated in this case.

                So, you are saying that when I apply @KeepAlive annotation on a managed bean class, every time I reference that bean, it will be recreated, but with the same data model (field values) as the last time it was instantiated?

                What is the difference between using @KeepAlive annotation and <a4j:keepAlive> tags? Is @KeepAlive used generaly for saving state on all pages which reference that bean, and <a4j:keepAlive> tag for only those pages which we want?

                • 5. Re: a4j:keepAlive doesn't work as expected
                  Nick Belaevski Master

                  Note that you can open several browser windows sharing the same session. Session bean won't work as desired, keep-alive beans will.