3 Replies Latest reply on Mar 27, 2008 1:43 PM by h b

    Help me design a conversation scope using a4j:KeepAlive

    h b Newbie

      Hello,

      I'm trying to avoid the server session and instead use the a4j:KeepAlive feature to manage state. I've written a little web-app where I capture the user information into a request-scope bean called Visit, like so: <a4j:keepAlive id="visitBean" beanName="visit" ajaxOnly="false" />

      LifeCycle:
      The first JSF page just relies on JSF bean management facility to instantiate the bean (Visit). And I know that its state is being maintained across page navigation (because I check my logs and I don't see it getting constructed again).

      Finally the Problem:
      How can access the visit instance from another backing bean?

      I don't want to solve it this way:
      You see, my bean name is just "visit"...not #{myBean.visit}...in other words it is not a property of my backing bean.

      Moreover, I don't want to make it a property of a backing bean because my backing beans change from page to page...I'd be forced to do something like this

      for page 1, I'd have to put something like:
      <a4j:keepAlive beanName="#{backingBean1.visit}" ajaxOnly="false" />

      for page 2, I'd have to put something like:
      <a4j:keepAlive beanName="#{backingBean2.visit}" ajaxOnly="false" />

      for page 3, I'd have to put something like:
      <a4j:keepAlive beanName="#{backingBean3.visit}" ajaxOnly="false" />

      instead of something like this in all pages: <a4j:keepAlive id="visitBean" beanName="visit" ajaxOnly="false" /> (I can acutally just do this once in a facelet template).

      Any other ideas?

      Thanks!

        • 1. Re: Help me design a conversation scope using a4j:KeepAlive
          Fab Mars Novice

          Imo "visit" should be reinstanciated upon navigation.
          keepAlive is only to prevent reinstanciation at each ajaxRequest on one same page, that is it enables making conversation scoped beans. I'm not an expert of the whole RF platform but if you change pages, this is a new non-ajax request, and thus we're not in a conversation anymore and the bean should be reinstanciated.

          That said, you may access any value by:

          FacesContext facesContext = FacesContext.getCurrentInstance();
          ELContext elContext = facesContext.getELContext();
          ValueExpression ve = facesContext.getApplication().getExpressionFactory().createValueExpression(elContext, #{visit}, Visit.class);
          Visit visit = (Visit)ve.getValue(elContext); //here comes your instance
          


          • 2. Re: Help me design a conversation scope using a4j:KeepAlive
            h b Newbie

            Thanks for your input...you are correct, keepAlive tag isn't going to help me with what I want.

            Is there a way I can create a conversational state using richfaces or myfaces? I really don't want to use yet another framework, just because I need conversational state...

            Any ideas?

            Thanks
            P.S. Thank you for the sample code...it worked like a charm.

            • 3. Re: Help me design a conversation scope using a4j:KeepAlive
              h b Newbie

              Just an update on this...

              The a4j:keepAlive works as long as all page navigation is done through posts.

              I retrieve the actual visit bean using the code snippet fabmars posted (THANKS!).

              I'll continue to post my findings on this thread...