retrodev
retrodev

Reputation: 2393

PrimeFaces tree selection is null

I am using PrimeFaces 2.2.1 Tree component on Glassfish 3.1

I am trying to set the selected node on the Tree to a TreeNode object in my backing bean, but it is always null.

I asked for support on the PrimeFaces forum, but unfortunately received no reply.

<p:tree id="contextTree" value="#{contextTreeBean.contextRoot}" var="node" selectionMode="single" selection="#{contextTreeBean.selectedNode}">
        <p:treeNode>
            <h:outputText value="#{node.name}"/>
        </p:treeNode>
    </p:tree>
    <h:outputText id="output" value="#{contextTreeBean.output}"/>
    <p:commandButton id ="createButton" value="+" actionListener="#{contextTreeBean.createContext()}" update="contextTree, output"/>


@ManagedBean
@RequestScoped
public class contextTreeBean {

    @EJB
    private ContextFacadeLocal contextFacade;
    private Context context = new Context();
    private TreeNode contextRoot;
    private TreeNode selectedNode;
    private String output;

    /** Creates a new instance of contextTreeBean */
    public contextTreeBean() {
    }

    public void createContext() {
        output = selectedNode.getData().toString();
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public ContextFacadeLocal getContextFacade() {
        return contextFacade;
    }

    public void setContextFacade(ContextFacadeLocal contextFacade) {
        this.contextFacade = contextFacade;
    }

    public TreeNode getContextRoot() {
        return contextRoot;
    }

    public void setContextRoot(TreeNode contextRoot) {
        this.contextRoot = contextRoot;
    }

    @PostConstruct
    private void postConstruct() {
        populateContextTree();
    }

    private void populateContextTree() {
        buildContextTree(new DefaultTreeNode("Root", null), contextFacade.findRootContexts());
    }

    private void buildContextTree(TreeNode parentNode, List<Context> children) {
        for (Context currentContextNode : children) {
            TreeNode tempNode = new DefaultTreeNode(currentContextNode, parentNode);
            buildContextTree(tempNode, currentContextNode.getChildren());
        }
        contextRoot = parentNode;
    }
}

Upvotes: 2

Views: 4145

Answers (2)

retrodev
retrodev

Reputation: 2393

I had the Tree component in a JSF template client with the form in the template. Moving the form into the client page worked. I was able to keep the backing bean RequestScoped.

Upvotes: 0

maple_shaft
maple_shaft

Reputation: 10463

Did you try verifying if selectedNode is null by logging? Perhaps it is getting set but the update attribute of your <p:commandButton> is not set correctly. Remember that by default the <h:form> will prepend its id to child elements.

Also verify that there are no validation errors being thrown by other elements in the <h:form>

Further still I do not believe that a Primefaces tree component will work correctly when backed by a @RequestScoped managed bean. Try changing the managed bean to @ViewScoped so that the lifecycle of the managed bean will span across individual requests.

Upvotes: 2

Related Questions