6 Replies Latest reply on Sep 13, 2006 12:53 PM by Gus Gu

    @RequestParameter value become null in conversation sfsb

    Gus Gu Expert

      Hi,

      I used "@RequestParameter String key" in a conversation scope sfsb. When click a action method @Begin, the key got a value in the conversation session bean, but when I click second action method @End, the key value began null. Anybody can give me a help? Thanks in advance.

        • 1. Re: @RequestParameter value become null in conversation sfsb
          hdslkf hklsdg Apprentice

          Request parameters usually are only sent once, namely when there is a corresponding parameter in the HTTP request. If there are other requests following with no key parameter, then the variable gets null ;).

          So if you want to have that "String key" during the whole conversation, there are two possibilities:

          - add (f:param name="key" value="#{yourBean.key}" /) to every commandLink, commandButton, ... on your page, so that it gets always re-injected (BAD IDEA, blows up your page code and it's silly to pass the same parameters from client to server on and on ;))

          - copy the key to another property during the conversation's begin:

          @RequestParameter
          String key;
          
          String keyCopy; //choose a better name ;)
          
          @Begin
          public String beginActionMethod() {
           keyCopy = key;
           ...
          }
          
          public String someOtherActionMethod() {
           doSomethingWith(keyCopy); //key would be null here, so use keyCopy
          }


          • 2. Re: @RequestParameter value become null in conversation sfsb
            Gus Gu Expert

            Thank you so much, Bfo81. That is a good idea.
            Best regards,

            Gus

            • 3. Re: @RequestParameter value become null in conversation sfsb
              Gus Gu Expert

              Hi,

              I am thinking whether the conversation scope SFSB can be designed to hold a state (keep some values) from @Begin call to @End call. It should be reasonable for the conversation Stateful Session bean.

              • 4. Re: @RequestParameter value become null in conversation sfsb
                hdslkf hklsdg Apprentice

                I'm not sure if I understood your question correctly (sorry, German ;)).

                @Stateful
                @Scope(CONVERSATION)
                @Name("whatEver")
                public class WhatEverBean implements WhatEver {
                
                @RequestParameter
                private String someParam;
                
                private String someParamCopy;
                
                @Begin
                public String begin() {
                 someParamCopy = someParam;
                 ...
                }
                
                @End
                public String end() {...}
                }

                In this case, the injected request parameter stored in someParamCopy is available all the time from calling begin() till calling end(). Even any other property will keep its state during that time, unless it's changed by the code or by Seam (e.g. @In annotated properties get a new value during every request).

                Is that about what you wanted to know?



                • 5. Re: @RequestParameter value become null in conversation sfsb
                  Gus Gu Expert

                  Hi Bfo81,

                  Thank you very much for your detailed explanation. Yes, you exactly answered my question. Thanks a lot. My second question is whether it is possible to improve conversation SFSB functionality in the future to save the code: "someParamCopy = someParam;" in @Begin method. For example, when click

                  <h:commandLink value="Begin" action="#{testBean.begin}">
                   <f:param name="someParam" value="1"/>
                  </h:commandLink>
                  the conversation SFSB will hold the "someParam" param in session bean until call @End end(). The conversation SFSB should look like:
                  @Stateful
                  @Scope(CONVERSATION)
                  @Name("testBean")
                  public class TestBean implements Test {
                  
                   @RequestParameter
                   private String someParam;
                  
                   private String someParamCopy;
                  
                   @Begin
                   public String begin() {
                   //someParamCopy = someParam; save the code
                   ...
                   }
                  
                   @End
                   public String end() {
                   someParam // have the value 1
                   }
                  }

                  But, currently your idea is best to solve my first question. Thank you.

                  • 6. Re: @RequestParameter value become null in conversation sfsb
                    Gus Gu Expert

                    Hi Gavin,

                    I don't know whether we can let a Conversation SFSB to hold intance fields from @Begin, to @Join, @Join, ... until @End.
                    Bfo81 gave a good suggestion, but if there are several intance fields need to be input at each @Join, this method (e.g. someParamCopy1 = someParam1; someParamCopy2 = someParam2;...; at in each @Join method ) will looks a little clumsy. The good suggestion like this:

                    @Begin
                    public method1 () {
                     intance1 = ...;
                    }
                    
                    @Join
                    public method2 () {
                     intance2 = ...;
                    }
                    
                    ...
                    
                    @End
                    public execute() {
                     total = instance1 + instance2 + ..
                    }

                    Thank you in advance.