14 Replies Latest reply on Feb 6, 2007 2:35 PM by norman.richards

    multiple component instances in one page

      Allright, I might be asking something dumb, but: I have build a simple helloworld application. Below is the component:

      @Name("helloworld")
      public class Helloworld {
      
       private DumbUser dumbUser;
      
       public String sayHello() {
       System.out.println("sayHello");
       return "success";
       }
      
       public DumbUser getDumbUser() {
       System.out.println("retrieving user");
       return dumbUser;
       }
      
       public void setDumbUser(DumbUser dumbUser) {
       System.out.println("setting user");
       this.dumbUser = dumbUser;
       }
      }
      


      And this simple class

      
      public class DumbUser {
      
       private String name;
      
       public DumbUser() {
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      }
      
      


      This is the JSF page:

       <h:form id="helloworld">
       <div>
       <h:inputText value="#{helloworld.dumbUser.name}"/>
       <h:commandButton type="submit" value="sayHello!" action="#{helloworld.sayHello}"/>
       </div>
       </h:form>
      
      


      When I hit the button, the helloworld.sayHello is never triggered. I can`t figure out why. helloworld.getDumbUser is called but then execution stops.

      Uhm, what am I doing wrong?

        • 1. Re: multiple component instances in one page

          I forgot to mention, when I remove the line:

          <h:inputText value="#{helloworld.dumbUser.name}"/>

          everything works fine.

          • 2. Re: multiple component instances in one page
            sbublava

            Well dumbUser is null, so the value you enter cannot be assigned to dumbUser.name.

            Don't you see a NullPointerException in the log file?

            • 3. Re: multiple component instances in one page
              pmuir

              You've forgotten to create an instance of DumbUser in Helloworld

              • 4. Re: multiple component instances in one page
                fernando_jmt


                Because your attribute DumbUser is a plain class (not injected), you sould initialize your class manually,


                private DumbUser dumbUser = new DumbUser();
                
                


                Try above, I think it will work.

                • 5. Re: multiple component instances in one page

                   

                  "sbublava" wrote:
                  Well dumbUser is null, so the value you enter cannot be assigned to dumbUser.name.

                  Don't you see a NullPointerException in the log file?


                  OMG, i`m an idiot, been working to long today I guess...

                  Indeed this was the problem, but no nullpointers in the log (strange).

                  Thnx :)

                  • 6. Re: multiple component instances in one page
                    sbublava

                     

                    "soudmaijer" wrote:

                    Indeed this was the problem, but no nullpointers in the log (strange).


                    Then it's probably a regular error message. Try adding:

                    <h:messages />


                    • 7. Re: multiple component instances in one page

                      Alright,

                      this works, but now to another problem. Lets say my simple object looks like this:

                      @Name("dumbUser")
                      public class DumbUser {
                      
                       private String name;
                      
                       public DumbUser() {
                       }
                      
                       public String getName() {
                       return name;
                       }
                      
                       public void setName(String name) {
                       this.name = name;
                       }
                      }
                      


                      and my helloworld component looks like this:

                      @Name("helloworld")
                      public class Helloworld {
                      
                       @In("dumbUser")
                       private DumbUser dumbUser;
                      
                       public String sayHello() {
                       System.out.println("sayHello: "+ dumbUser.getName());
                       return "success";
                       }
                      }
                      


                      my jsf like this:

                       <h:form>
                       <div>
                       <h:inputText value="#{dumbUser.name}"></h:inputText>
                      
                       <h:commandButton type="submit" value="sayHello!" action="#{helloworld.sayHello}"/>
                       </div>
                       </h:form>
                      


                      This all works fine, but if I want to add another dumbUser to the Helloworld component, something like this:

                      @Name("helloworld")
                      public class Helloworld {
                      
                       @In("dumbUser")
                       private DumbUser dumbUser1;
                      
                       @In("dumbUser")
                       private DumbUser dumbUser2;
                      
                       public String sayHello() {
                       System.out.println("sayHello: "+ dumbUser1.getName());
                       System.out.println("sayHello: "+ dumbUser2.getName());
                       return "success";
                       }
                      }


                      Is this possible? How do I access them from the JSF? Something like this?

                       <h:form>
                       <div>
                       <h:inputText value="#{dumbUser1.name}"></h:inputText>
                       <h:inputText value="#{dumbUser2.name}"></h:inputText>
                      
                       <h:commandButton type="submit" value="sayHello!" action="#{helloworld.sayHello}"/>
                       </div>
                       </h:form>
                      


                      This doesnt work... but i`ve tried many variations... is it possible?

                      • 8. Re: multiple component instances in one page
                        sbublava

                         

                        @Name("helloworld")
                        public class Helloworld {
                        
                         @In("dumbUser")
                         private DumbUser dumbUser1;
                        
                         @In("dumbUser")
                         private DumbUser dumbUser2;
                        }


                        I believe the second one should be @In("dumbUser2").


                        • 9. Re: multiple component instances in one page
                          fernando_jmt

                          You can try defining a new different role for the second DumbUser.

                          First you need to modify your dumbClass like this (adding a new role):

                          @Name("dumbUser")
                          @Role(name="dumbUser1")
                          public class DumbUser {
                          
                           private String name;
                          
                           public DumbUser() {
                           }
                          
                           public String getName() {
                           return name;
                           }
                          
                           public void setName(String name) {
                           this.name = name;
                           }
                          }
                          


                          Then your hello world component:
                          @Name("helloworld")
                          public class Helloworld {
                          
                           @In("dumbUser")
                           private DumbUser dumbUser1;
                          
                           @In("dumbUser1")
                           private DumbUser dumbUser2;
                          
                           public String sayHello() {
                           System.out.println("sayHello: "+ dumbUser1.getName());
                           System.out.println("sayHello: "+ dumbUser2.getName());
                           return "success";
                           }
                          }
                          


                          Then the JSF should looks like:
                          <h:form>
                           <div>
                           <h:inputText value="#{dumbUser.name}"></h:inputText>
                           <h:inputText value="#{dumbUser1.name}"></h:inputText>
                          
                           <h:commandButton type="submit" value="sayHello!" action="#{helloworld.sayHello}"/>
                           </div>
                           </h:form>
                          




                          • 10. Re: multiple component instances in one page

                             

                            "sbublava" wrote:
                            @Name("helloworld")
                            public class Helloworld {
                            
                             @In("dumbUser")
                             private DumbUser dumbUser1;
                            
                             @In("dumbUser")
                             private DumbUser dumbUser2;
                            }


                            I believe the second one should be @In("dumbUser2").


                            Tried this, but that doesn`t work.... (thanks for the suggestion)

                            • 11. Re: multiple component instances in one page
                              sbublava

                              Do you need exactly two DumbUser instances or many of them?

                              • 12. Re: multiple component instances in one page

                                 

                                "fernando_jmt" wrote:
                                You can try defining a new different role for the second DumbUser.



                                Cool, this works!

                                So it means, that if you want to have multiple occurences of an component in one page, you can`t do this without modifying the component (This surprises me a bit).

                                Thnx all for the responses.



                                • 13. Re: multiple component instances in one page

                                   

                                  "sbublava" wrote:
                                  Do you need exactly two DumbUser instances or many of them?


                                  I was just testing around a bit. If I need multiple instances I would probably store it in a collection.

                                  But in this particular case I was trying to let seam inject two instances of an component for me.



                                  • 14. Re: multiple component instances in one page

                                    You can also declare the second named instance in XML. It has to be declared somewhere.