11 Replies Latest reply on May 15, 2007 6:24 PM by Susanne Jarl

    Parameter passing for page action

    Milli Coelho Newbie

      Hi,

      How do I pass parameters in the following scenerio?(instead of using session scope)

      @Name("home")
      Class Home
      {
      private Category category;
      
      public getCategory()
      {
       if (null == category)
       category = CategoryHome.find(id);
       return category;
      }
      }

      home.xhtml:
      .....
      <h:commandLink action="#{categoryAction.action(home.category)}" value="Action" />

      @Name(categoryAction)
      Class CategoryAction
      {
      private Category category;
      
      public Category getCategory()
      {
       return category;
      }
      
      public String action(Category category)
      {
      this.category = category
      return "category.xhtml"
      }
      }
      category.xhtml:

      displays #{categoryAction.category.name} and #{categoryAction.category.description}

      If I do as above, the category is always null in category.xhtml. If I set the categoryAction scope to Session it works. I'm trying to find out if there is a better way to do this without using session scope.

      If I'm doing something wrong could you please let me know the right way of doing it. Basically I want to be able to pass objects from home page(home component) to category page(categoryAction component) avoiding another DB query.


      Any help would be appreciated.

      Thanks.


        • 1. Re: Parameter passing for page action
          Christian Bauer Master

          I just posted about this yesterday, search.

          • 2. Re: Parameter passing for page action
            Milli Coelho Newbie

            Thanks Christian for your response. I did search before posting but couldn't find an answer to my question. Anyways I did another search for your posting. Were you referring to this post http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4044391? Either it does not answer my question or I don't get it.

            Could you please me know how I can make #{categoryAction.category} (from home) available in category.xhtml?

            • 3. Re: Parameter passing for page action
              Christian Bauer Master

              It's the same issue I talked about in the other forum thread: You are trying to use doStuff(foo.bar) for something it simply doesn't do. What you wrote is just a shortcut for this:

              @Name(categoryAction)
              Class CategoryAction
              {
              
              @In("#{home.category}")
              private Category category;
              
              public String action()
              {
              this.category = category
              return "category.xhtml"
              }
              }
              


              As you can probably guess, when action() is called, the @In will not find "home" because that component is not in any stateful context. So you need to put it in a stateful context - and there are only two choices for such a simple case: Session and Conversation.

              Session is not good because it is not isolated for different browser windows and concurrent access. So you want to read up on Conversation and how you can span a conversation context across requests, to put stuff into it and read out of it.


              • 4. Re: Parameter passing for page action
                Milli Coelho Newbie

                I appreciate your immediate response.

                So in other words, if you want to pass parameters(particularly objects) to an action method using EL extension, the parameter has to be in session or conversation context.

                Is my understanding correct?

                • 5. Re: Parameter passing for page action
                  Christian Bauer Master

                  Not fully, the method argument is just a name. That name will be looked up during the next request before that method is finally called.

                  • 6. Re: Parameter passing for page action
                    Milli Coelho Newbie

                    Thanks again for your response. I just have one more question along the same line.

                    Can I initialize some member variables(id) in load method and make it available in category.xhtml(categoryAction.id)? I tried it but again unless categoryAction is in session context, it does not work.

                    In the previous case I understand the parameter is just the name in the page and seam injects it from session/conversation context when the action is performed. But here the Action bean is loading the page but it goes out of context when the page is rendered I guess. I expected at least event context(which I assumed is equivalent to request context in servlet/JSP) to work but it does not.

                    @Name(categoryAction)
                    Class CategoryAction
                    {
                    private String id;
                    
                    public String getId()
                    {
                     return Id;
                    }
                    
                    public String action()
                    {
                    this.id = 1;
                    return "category.xhtml"
                    }
                    }


                    • 7. Re: Parameter passing for page action
                      Christian Bauer Master

                      That should absolutely be possible, Event context spans _all_ phases in a request.

                      • 8. Re: Parameter passing for page action
                        Milli Coelho Newbie

                        That's what even I thought but may be I'm doing something wrong.

                        Here is the simple code I'm trying to run but it does not seem to work with event scope. Both category and id are always null unless I change the scope of categoryAction to session. Could you spot anything wrong with this approach? Thanks

                        @Name("home")
                        Class Home
                        {
                        private String category;
                        
                        public getCategory()
                        {
                         if (null == category)
                         category = new String("Testing Event Scope");
                         return category;
                        }
                        }


                        home.xhtml:
                        <h:commandLink action="#{categoryAction.action}" value="#{home.category}">
                        <f:param name="category" value="#{home.category}" />
                        </h:commandLink>
                        


                        @Name(categoryAction)
                        @Scope(ScopeType.Event)
                        Class CategoryAction
                        {
                        @RequestParameter("category")
                        private String category;
                        
                        public String getId()
                        {
                         if (null == id)
                         System.out.println("id is null");
                         return Id;
                        }
                        
                        public String getCategory()
                        {
                         if (null == category)
                         System.out.println("category is null");
                         return category;
                        }
                        
                        public void setCategory(String category)
                        {
                         this.category = category;
                        }
                        
                        public String action()
                        {
                        this.id = 1;
                        return "category.xhtml"
                        }
                        }


                        category.xhtml:
                        
                        <h:outputText value="categoryAction.id" />
                        <h:outputText value="categoryAction.category" />


                        • 9. Re: Parameter passing for page action
                          Christian Bauer Master

                          <f:param name="category" value="#{home.category}" />

                          ?! What is that supposed to do? It renders the toString() method of the Category class as a URL parameter. Seriously, you need to read up on conversations.

                          • 10. Re: Parameter passing for page action
                            Milli Coelho Newbie

                             

                            "christian.bauer@jboss.com" wrote:
                            <f:param name="category" value="#{home.category}" />

                            ?! What is that supposed to do? It renders the toString() method of the Category class as a URL parameter. Seriously, you need to read up on conversations.


                            Sorry that I didn't make it clear. I changed category to a string variable(in the latest one) just to see if it would get passed. Basically I'm passing a category name(string) as a parameter to categoryAction(categoryAction.action) to see if it would available in category.xhtml if categoryAction is in EVENT scope. When the action method(categoryAction.action) is called, the member variables "category" is set to "Testing Event Scope"(thru' @RequestParametr) and Id is set to 1.
                            But when the page category.xhtml is loaded they are not available.

                            I am looking in to conversations as well. But EVENT scope meets my requirements if it works the way it is supposed to work.

                            • 11. Re: Parameter passing for page action
                              Susanne Jarl Newbie

                              I think what you are looking for is the @Out annotation and also @Factory. Read more about these and look at the examples and I think you will figure out how to use seam in a nice and straightforward way.

                              If you want to pass a param when you click on a button you could use <s:button> or <s:link> with an argument in the action attribute.