8 Replies Latest reply on May 31, 2009 2:09 PM by meghiddo

    How to get my List<String> to bind to richfaces tree nodes?

      I have code where I pull data from a web service, use xml to parse it, and then add the text nodes I want it to into a list. This all works as intended because when I print out the list it prints out the values I expect it to.
      But I have tried using this list to populate a tree and it just doesnt seem to work. I keep getting just a blank page. Im not sure if Im just doing the html wrong (thought Ive tried quite a few things) or if the tree node is calling the value of this lest before the operations are performed to populate the list.

      Here is my code to populate and get the list that contains the needed values:

      public class Nodes(){
      static List<String> ids = new ArrayList<String>();
      
      
       public static List<String> getIds() {
      
      
       XmlData nodes = new XmlData();
       DeviceCollectionXMLAO access = new DeviceCollectionXMLAO();
       HttpClientMethods method = new HttpClientMethods();
       DeviceCollection deviceCollection = null;
      
       try {
       deviceCollection = access.readXML(method.getResponseBodyAsStream(null));
       } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
      
      
      
       for (Device device : deviceCollection.getDevices()){
       String x = device.getDeviceId();
       ids.add(x);
       }
      
       return ids;
      
       }
      
      
       public static void setIds(List<String> ids) {
       Temp.ids = ids;
       }
      }}


      So as you can see the values are getting stored in ids. Have I done something wrong here, or am I just binding this to the tree incorrectly?

      Here is how I tried to bind it to the tree btw:

      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:a4j="http://richfaces.org/a4j"
       xmlns:rich="http://richfaces.org/rich">
      
       <h:form>
      
       <rich:tree style="width:300px" switchType="ajax" >
       <rich:recursiveTreeNodesAdaptor nodes="#{nodesBean.ids}" />
       </rich:tree>
      
      
       </h:form>
      
      </ui:composition>


        • 1. Re: How to get my List<String> to bind to richfaces tree nod

          oh yes, and I also have tried to use nodesBean.getIds but no luck

          • 2. Re: How to get my List<String> to bind to richfaces tree nod
            nbelaevski

            Hi,

            Methods should not be static.

            • 3. Re: How to get my List<String> to bind to richfaces tree nod

              AH OK, thanks, I forgot that I had called that in my main to prnt out the values and that I would need to change it back from static.

              Unfortunately right now the web service is down so I cant see if this worked.

              SO Ill just ask one other question, in my jsf, sould I use nodes="#{nodesBean.ids}" or nodes="#{nodesBean.getIds}" ?

              • 4. Re: How to get my List<String> to bind to richfaces tree nod
                nbelaevski

                #{nodesBean.ids}

                • 5. Re: How to get my List<String> to bind to richfaces tree nod

                  I must be missing something I need pertaining to how to use a list to populate the tree. SO instead of using the code above that has a lot of potential for errors, I simplified things as far as I could, but I am still not able to use a list to populate the tree. Here is what i did:

                  First I made a simple class to declare a list, populate it with one entry and have setter and getter:

                  public class RootNodes {
                  
                   List<String> ids = new ArrayList<String>();
                  
                   public List<String> getIds() {
                   ids.add("Test");
                   System.out.println(ids);
                   return ids;
                   }
                  
                   public void setIds(List<String> ids) {
                   this.ids = ids;
                   }
                  
                  }


                  When I printed out the value of ids it printed out [Test] so it seems the list is getting the value, and should not be null. SO I created a managed bean, and then tried to bind ids to a tree:

                  <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                   xmlns:ui="http://java.sun.com/jsf/facelets"
                   xmlns:h="http://java.sun.com/jsf/html"
                   xmlns:f="http://java.sun.com/jsf/core"
                   xmlns:a4j="http://richfaces.org/a4j"
                   xmlns:rich="http://richfaces.org/rich">
                  
                   <h:form>
                  
                   <rich:tree style="width:300px" switchType="ajax" >
                   <rich:recursiveTreeNodesAdaptor nodes="#{nodesBean.ids}" />
                   </rich:tree>
                  
                  
                   </h:form>
                  
                  </ui:composition>


                  But all that does is print out a blank page, just like my other code up there. This is as simple as it can get, so I must be missing something about how to bind this to a treeNode. Can anyone tell me what Im doing wrong?

                  • 6. Re: How to get my List<String> to bind to richfaces tree nod

                    I even simplified it a bit further by doing this:

                    int[] ids = {1, 2, 3, 4};
                    
                     public int[] getIds() {
                     return ids;
                     }
                    
                     public void setIds(int[] ids) {
                     this.ids = ids;
                     }
                    


                    ANd still a blank page. However, I did change nodes="#{nodesBean.ids}" to roots="#{nodesBean.ids}" and I did get a one branch tree with a value of
                    [I@1486306
                    Not what I was looking for, but compared to the luck I have had with this it is almost a mircale

                    • 7. Re: How to get my List<String> to bind to richfaces tree nod
                      nbelaevski

                      Should be roots="#{nodesBean.ids}" and ids should be List not array.

                      • 8. Re: How to get my List<String> to bind to richfaces tree nod

                        man, the reason my original code isnt working is the web service is down, I keep forgetting.
                        Ill have to try it again once things are up again and then Ill let you know what happens.