4 Replies Latest reply on Mar 21, 2010 8:00 PM by Rodrigo Bento

    Keeping @In-jected variable value between method calls

    Rodrigo Bento Newbie
      Hello all,

      I'm using Seam 2.2.0GA with Tomcat 6.0.24.

      I have an action bean which scope is conversation. This bean has a In(create=true,required=false) attribute which is meant to keep the old state of a component.

      So I have:

      @In(create=true, required=false)
      @Out(required=false)
      @DataModelSelection
      private Person person;

      @In(create=true,required=false)
      private Person oldPerson;

      The 'person' is used in the ui/form, but before editing a person I must keep its old state, so I have implemented the clone() method for the Person entity.

      public String edit() {

         oldPerson = person.clone();

         return INPUT;
      }

      I have a page which lists all the people from database, and to edit a person you must click and 'edit' button. By doing this I call an edit() method which will clone the clicked person state into the oldPerson and redirect to the edit form.

      Everything works fine until the edit method returns. I have some logic for the oldPerson in the save() method later, but when calling save my oldPerson is null.

      Hello all,

      I'm using Seam 2.2.0GA with Tomcat 6.0.24.

      I have an action bean which scope is conversation. This bean has a In(create=true,required=false) attribute which is meant to keep the old state of a component.

      So I have:

      @In(create=true, required=false)
      @Out(required=false)
      @DataModelSelection
      private Person person;

      @In(create=true,required=false)
      private Person oldPerson;

      The 'person' is used in the ui/form, but before editing a person I must keep its old state, so I have implemented the clone() method for the Person entity.

      public String save() {

         ...some logic

         // oldPerson is null at this point!

         return SUCCESS;
      }

      I've seen in the documentation that it is an expected behavior, but then what should I do in order to keep the state of the oldPerson until my conversation is ended?

      I've tried scope and value options in the @In annotation but that didn't worked.

      The oldPerson is not meant to be outjected, so I'm not supposed to annotate it with @Out, but doing it fixes my problem.

      Is this correct way to do this? Could someone please point me in the right direction?

      Thanks,

      Rodrigo






        • 1. Re: Keeping @In-jected variable value between method calls
          Rodrigo Bento Newbie

          Well, It's not possible to erase or edit posts here. :(


          Something went wrong and so I'm posting again below.


          • 2. Re: Keeping @In-jected variable value between method calls
            Rodrigo Bento Newbie
            Hello all again,

            I'm using Seam 2.2.0GA with Tomcat 6.0.24.

            I have an action bean which scope is conversation. This bean has an @In(create=true,required=false) attribute which is meant to keep the old state of a component.

            So I have:

            @In(create=true, required=false)
            @Out(required=false)
            @DataModelSelection
            private Person person;

            @In(create=true,required=false)
            private Person oldPerson;

            The 'person' is used in the ui/form, but before editing a person I must keep its old state, so I have implemented the clone() method for the Person entity.

            I have a page which lists all the people from database, and to edit a person you must click and 'edit' button. By doing this I call an edit() method which will clone the clicked person state into the oldPerson and redirect to the edit form.

            public String edit() {

               oldPerson = person.clone();

               return INPUT;
            }

            Everything works fine until the edit method returns. I have some logic for the oldPerson in the save() method later, but when calling save my oldPerson is null.

            public String save() {

               // ...some logic

               // oldPerson is null at this point!

               return SUCCESS;
            }

            I do begin and end conversations in pages.xml

            I've read in the documentation that it is an expected behavior, but then what should I do in order to keep the state of the oldPerson until my conversation is ended?

            I've tried scope and value options in the @In annotation but that didn't worked.

            The oldPerson is not meant to be outjected, so I'm not supposed to annotate it with @Out, but doing it fixes my problem.

            Is this correct way to do this? Could someone please point me in the right direction?

            Thanks,

            Rodrigo
            • 3. Re: Keeping @In-jected variable value between method calls
              Rob Shepherd Newbie

              If you don't Outject it, it can't be injected. (as you have proved)


              So it seems you really don't need this object to be in the Context, so you don't need to put @In on oldPerson.


              Just save it as a local variable.




              Running through your example.... as long as the Bean that has the save() and edit() functions is being kept (statefully) between invocations, you local variable will be there.


              It would appear then that your combination of @In means your local variable is being overwritten with null.

              • 4. Re: Keeping @In-jected variable value between method calls
                Rodrigo Bento Newbie

                Hello Rob.


                You're absolutely right. I kind of got used to the seam annotations and kind of forgot that I could not use them.


                By removing annotations everything worked fine. Just normal Java behavior.


                Thanks a lot.