6 Replies Latest reply on May 11, 2010 5:34 AM by nbelaevski

    component binging + dynamic component init

    svkap

      Hello.

       

      I have some troubles with component binding. I am creating some dynamic components on bean initialization. I am binding HtmlPanelGrid or HtmlPanel components and I am creating dynamic components as their children. However for some reason setters of the bound compoents are not always executed. As a result these components (the panels) do not have parent and therefor the dynamicly created components are not shown. I am initializing the components on @PostConstruct init method. I noticed that most of the times (at least on PanelGrids) if I have some other component inside the jsf page then it bound ok and it has parent (on @PostConstruct they are still null but at least the result is right). I suppose I am doing sth wrong.. Can u give me advice how this is done?

        • 1. Re: component binging + dynamic component init
          nbelaevski

          Hi Svetoslav,

           

          JavaDoc says that setter is not called if getter returns non-null component value.

          • 2. Re: component binging + dynamic component init
            svkap

            I knew that I have to read the JavaDoc first .  What would you advise me? In @PostConstruct method my components are null.. So should I generate the dynamic components in the setter then?

            • 3. Re: component binging + dynamic component init
              nbelaevski

              How does page & bean code look like?

              • 4. Re: component binging + dynamic component init
                mpickell

                Couldn't you return a new HtmlPanelGroup() instead when you otherwise would be returning null?

                 

                That would always return a component and would have no affect on your page layout (at least I think that's true with the little knowledge I have of your problem)

                • 5. Re: component binging + dynamic component init
                  svkap

                  Here is a part of my code:

                   

                   

                          private void initPanelList() { 
                              // add list all link
                              HtmlPanelGroup panelGroup = new HtmlPanelGroup();
                              panelGroup.setLayout("block");
                              panelGroup.setStyleClass("UIPortrait");
                              HtmlGraphicImage image = new HtmlGraphicImage();
                              image.setUrl("/img/px.gif");
                              image.setStyle("width:50px;height:50px;");
                              HtmlOutputLink textLink = new HtmlOutputLink();
                              textLink.setValue("./test.jsf");
                              HtmlOutputText textLinkTxt = new HtmlOutputText();
                              textLinkTxt.setValue("List All");
                              textLink.getChildren().add(textLinkTxt);
                              panelGroup.getChildren().add(image);
                              panelGroup.getChildren().add(textLink);
                              panelist.getChildren().add(panelGroup);
                           }
                  
                           public void setPanelList(HtmlPanelGroup panelList) {
                              this.panelList = panelList;
                              initPanelList();
                           }
                  

                   

                  This is working fairly right. However there is 1 problem. The setter is executed a couple of times and on every execute I am making new queries to the database so I can build the dynamic components. In short it is not a good idea to have init method in the setter but for now I do not know where to put it. I cannot initialize new HtmlPanelGroup component because that way I am loosing the parent and if I want to find the correct parent component I have to traverse the whole component tree. My bean is @ViewScoped . I cannot use sth like that:

                   

                   

                           public void setPanelList(HtmlPanelGroup panelList) {
                              if (panelList == null) {   
                                   this.panelList = panelList;
                                   initPanelList();
                              }   
                           }
                  

                   

                  because I am using another components along with RF and they are messing with the logic. By the way I have some troubles because of the second components and I will have to go with instantiating new HtmlPanelGroups on bean initialization. However, I hope that u could give me some advice and/or best practices for dynamic component creation.

                  • 6. Re: component binging + dynamic component init
                    nbelaevski

                    Svetoslav,

                     

                    I'd recommend to try creating components by action and use binding only to get parent component for the created components into bean.