1 Reply Latest reply on Sep 26, 2008 7:54 PM by Smitha Ramaswamy

    rich:tree, Default Tree Model

    Smitha Ramaswamy Newbie

      I just started using richfaces and jumped right into tree. I am trying to get the tree model example working, but it gives me an error. I really need to get this working, need desperate help on this. This is the link I am following
      http://livedemo.exadel.com/richfaces-demo/richfaces/tree.jsf?tab=model

      Immediate help appreciated,

      error:
      #{simpleTreeBean.treeNode}' Error reading 'treeNode' on type org.richfaces.SimpleTreeBean

      stack Trace:
      org.apache.jasper.JasperException: javax.servlet.ServletException: /pages/index4.jsp(25,4) '#{simpleTreeBean.treeNode}' Error reading 'treeNode' on type org.richfaces.SimpleTreeBean
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:397)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      root cause

      javax.servlet.ServletException: /pages/index4.jsp(25,4) '#{simpleTreeBean.treeNode}' Error reading 'treeNode' on type org.richfaces.SimpleTreeBean
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
      org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
      org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
      org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
      org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
      org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:694)
      org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:665)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:59)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      root cause

      org.apache.jasper.el.JspELException: /pages/index4.jsp(25,4) '#{simpleTreeBean.treeNode}' Error reading 'treeNode' on type org.richfaces.SimpleTreeBean
      org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:107)
      javax.faces.component.UIData.getValue(UIData.java:609)
      org.ajax4jsf.component.UIDataAdaptor.getValue(UIDataAdaptor.java:1640)
      org.richfaces.component.UITree.createDataModel(UITree.java:664)
      org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:621)
      org.ajax4jsf.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:339)
      org.richfaces.renderkit.TreeRendererBase.writeContent(TreeRendererBase.java:632)
      org.richfaces.renderkit.TreeRendererBase.encodeChildren(TreeRendererBase.java:573)
      javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
      com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:234)
      com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:178)
      com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:126)
      javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
      javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
      javax.faces.render.Renderer.encodeChildren(Renderer.java:148)
      javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
      javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
      javax.faces.component.UIComponent.encodeAll(UIComponent.java:942)
      com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:271)
      com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:202)
      org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
      org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
      com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
      com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
      com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
      org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
      org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
      org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
      org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
      org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:694)
      org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:665)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:59)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      root cause

      java.lang.NullPointerException
      java.util.Properties$LineReader.readLine(Unknown Source)
      java.util.Properties.load0(Unknown Source)
      java.util.Properties.load(Unknown Source)
      org.richfaces.SimpleTreeBean.loadTree(SimpleTreeBean.java:56)
      org.richfaces.SimpleTreeBean.getTreeNode(SimpleTreeBean.java:98)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
      javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
      com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
      org.apache.el.parser.AstValue.getValue(AstValue.java:97)
      org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
      org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:101)
      javax.faces.component.UIData.getValue(UIData.java:609)
      org.ajax4jsf.component.UIDataAdaptor.getValue(UIDataAdaptor.java:1640)
      org.richfaces.component.UITree.createDataModel(UITree.java:664)
      org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:621)
      org.ajax4jsf.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:339)
      org.richfaces.renderkit.TreeRendererBase.writeContent(TreeRendererBase.java:632)
      org.richfaces.renderkit.TreeRendererBase.encodeChildren(TreeRendererBase.java:573)
      javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
      com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:234)
      com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:178)
      com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:126)
      javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
      javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
      javax.faces.render.Renderer.encodeChildren(Renderer.java:148)
      javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
      javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
      javax.faces.component.UIComponent.encodeAll(UIComponent.java:942)
      com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:271)
      com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:202)
      org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
      org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
      com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
      com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
      com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
      org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
      org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
      org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
      org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
      org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:694)
      org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:665)
      org.apache.jsp.index_jsp._jspService(index_jsp.java:59)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


      index.jsp

      <!-- DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd"-->
      
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
      <%@ taglib uri="http://richfaces.org/rich" prefix="dnd" %>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      
      
      <html>
       <head>
       <title>Tree</title>
      
       </head>
       <body>
       <f:view>
      
       <h:form>
      
       <h:outputText value="Config Tree:" />
      
       <h:panelGrid columns="2" width="100" columnClasses="col1,col2">
      
       <rich:tree style="width:300px" nodeSelectListener="#{simpleTreeBean.processSelection}"
       reRender="selectedNode"
       ajaxSubmitSelection="true"
       switchType="client"
       value="#{simpleTreeBean.treeNode}"
       var="item">
       </rich:tree>
      
       <h:outputText escape="false" value="Selected Node: #{simpleTreeBean.nodeTitle}" id="selectedNode" />
      
       </h:panelGrid>
      
       </h:form>
      
       </f:view>
      
       </body>
      </html>
      


      SimpleTreeBean.java
      package org.richfaces;
      
      import java.io.IOException;
      import java.io.InputStream;
      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Map;
      import java.util.Properties;
      
      import javax.faces.FacesException;
      import javax.faces.component.UIComponent;
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      
      import org.richfaces.component.UITree;
      import org.richfaces.component.html.HtmlTree;
      import org.richfaces.event.NodeSelectedEvent;
      import org.richfaces.model.TreeNode;
      import org.richfaces.model.TreeNodeImpl;
      
      public class SimpleTreeBean {
      
       private TreeNode rootNode = null;
       private List<String> selectedNodeChildren = new ArrayList<String>();
      
       private String nodeTitle=null;
       private static final String DATA_PATH = "simple-tree-data.properties";
      
       private void addNodes(String path, TreeNode node, Properties properties) {
       boolean end = false;
       int counter = 1;
      
       while (!end) {
       String key = path != null ? path + '.' + counter : String.valueOf(counter);
      
       String value = properties.getProperty(key);
       if (value != null) {
       TreeNodeImpl nodeImpl = new TreeNodeImpl();
       nodeImpl.setData(value);
       node.addChild(new Integer(counter), nodeImpl);
       addNodes(key, nodeImpl, properties);
       counter++;
       } else {
       end = true;
       }
       }
       }
      
       private void loadTree() {
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();
       InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);
       try {
       Properties properties = new Properties();
       properties.load(dataStream);
      
       rootNode = new TreeNodeImpl();
       addNodes(null, rootNode, properties);
      
       } catch (IOException e) {
       throw new FacesException(e.getMessage(), e);
       } finally {
       if (dataStream != null) {
       try {
       dataStream.close();
       } catch (IOException e) {
       externalContext.log(e.getMessage(), e);
       }
       }
       }
       }
      
       public void processSelection(NodeSelectedEvent event) {
       HtmlTree tree = (HtmlTree) event.getComponent();
       nodeTitle = (String) tree.getRowData();
       selectedNodeChildren.clear();
       TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
      
      
       if (currentNode.isLeaf()){
       System.out.println("current tree node is a leaf");
       selectedNodeChildren.add((String)currentNode.getData());
       }else
       {
       System.out.println("current tree node is not a leaf");
      
       Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
       while (it!=null &&it.hasNext()) {
       Map.Entry<Object, TreeNode> entry = it.next();
       selectedNodeChildren.add(entry.getValue().getData().toString());
       }
       }
       }
      
       public TreeNode getTreeNode() {
       if (rootNode == null) {
       loadTree();
       }
      
       return rootNode;
       }
      
       public String getNodeTitle() {
       return nodeTitle;
       }
      
       public void setNodeTitle(String nodeTitle) {
       this.nodeTitle = nodeTitle;
       }
      }


        • 1. Re: rich:tree, Default Tree Model
          Smitha Ramaswamy Newbie

          I got the code working, by doing the following. Hope it helps others looking for same answer and that's why forums helps us learning.

          Make sure that the path of properties file is correct. I was using eclipse IDE. Hence DATA_PATH = "simple-tree-data.properties" must be DATA_PATH = "/simple-tree-data.properties" and drop the properties file under WEB-INF.