填充rich:tree组件时出错?

时间:2012-01-28 06:30:06

标签: java tree richfaces jsf-1.2

我正在运行树代码

我的JSF页面:

<rich:tree style="width:300px"
                nodeSelectListener="#{simpleTreeBean.processSelection}"
                reRender="selectedNode" value="#{simpleTreeBean.treeNode}"
                ajaxSubmitSelection="true" var="item">
            </rich:tree>

            <h:outputText escape="false"
                value="Selected node: #{simpleTreeBean.nodeTitle}" id="selectedNode" />

我的java文件:

package com.ge.beans;

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;
    private static final String DATA_PATH = "/rich_tree.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 (Exception e) {

            // throw new FacesException(e.getMessage(), e);
            System.out.println("Exception occured");
        } 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()) {
            selectedNodeChildren.add((String) currentNode.getData());
        } else {
            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;
    }

}

但是我无法填充树结构。

我得到以下错误

任何人都可以帮助我摆脱它..我需要在一天结束时完成这项任务。我认为我的属性文件的路径是错误的..你能说保留属性文件的位置吗?

错误:

javax.servlet.ServletException: /pages/fullTree.jsp(19,3) '#{simpleTreeBean.treeNode}' Error reading 'treeNode' on type com.ge.beans.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)


root cause 

org.apache.jasper.el.JspELException: /pages/fullTree.jsp(19,3) '#{simpleTreeBean.treeNode}' Error reading 'treeNode' on type com.ge.beans.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)


root cause 

java.lang.NullPointerException
    java.util.Properties$LineReader.readLine(Unknown Source)
    java.util.Properties.load0(Unknown Source)
    java.util.Properties.load(Unknown Source)
    com.ge.beans.SimpleTreeBean.loadTree(SimpleTreeBean.java:70)
    com.ge.beans.SimpleTreeBean.getTreeNode(SimpleTreeBean.java:99)
    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:118)
    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)

1 个答案:

答案 0 :(得分:1)

将var“dataStream”传递给properties.load(dataStream)时,它为null。发生这种情况是因为externalContext.getResourceAsStream(DATA_PATH)无法找到资源。你确定路径是正确的吗?