p:tree nodeSelectListener

UI Components for JSF
Post Reply
yhrchan
Posts: 11
Joined: 03 Sep 2010, 17:40

03 Sep 2010, 19:16

I am trying to get a simple example working for a p:tree and I have hooked up the nodeSelectListener but I am getting the below exception when selecting a node from the tree:

Caused by: java.lang.IllegalArgumentException: Cannot convert Node 0.0 of type class org.primefaces.model.DefaultTreeNode to class [Lorg.primefaces.model.TreeNode;
at org.apache.el.lang.ELSupport.coerceToType(ELSupport.java:375)
at org.apache.el.parser.AstValue.setValue(AstValue.java:141)
at org.apache.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:249)
at org.apache.myfaces.view.facelets.el.TagValueExpression.setValue(TagValueExpression.java:117)
at org.primefaces.component.tree.Tree.processUpdates(Tree.java:308)
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1258)
at javax.faces.component.UIViewRoot._processUpdatesDefault(UIViewRoot.java:1237)
at javax.faces.component.UIViewRoot.access$600(UIViewRoot.java:73)
at javax.faces.component.UIViewRoot$UpdateModelPhaseProcessor.process(UIViewRoot.java:1311)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1198)
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:681)
at org.apache.myfaces.lifecycle.UpdateModelValuesExecutor.execute(UpdateModelValuesExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
... 21 more

this is from the xhtml:

<p:tree value="#{treeBean.root}"
var="node"
dynamic="true"
cache="false"
toggleMode="async"
nodeExpandListener="#{treeBean.onNodeExpand}"
nodeCollapseListener="#{treeBean.onNodeCollapse}"
nodeSelectListener="#{treeBean.onNodeSelect}"
selectionMode="single"
selection="#{treeBean.selectedNodes}">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>

this is my TreeBean.java

package com.example.web.beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ManagedBean
@SessionScoped
public class TreeBean implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private static final Log LOGGER = LogFactory.getLog(TreeBean.class);

private TreeNode root;
private TreeNode selectedNode;
private TreeNode[] selectedNodes;

public TreeBean() {

root = new DefaultTreeNode("Root", null);

TreeNode node0 = new DefaultTreeNode("Search", root);
TreeNode node1 = new DefaultTreeNode("New", root);
TreeNode node2 = new DefaultTreeNode("Reports", root);

TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);
TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);

root.setExpanded(true);
}

public void onNodeSelect(NodeSelectEvent event) {
String node = event.getTreeNode().getData().toString();
LOGGER.info("Selected:" + node);
}

public void onNodeExpand(NodeExpandEvent event) {
String node = event.getTreeNode().getData().toString();
LOGGER.info("Expanded:" + node);
}

public void onNodeCollapse(NodeCollapseEvent event) {
String node = event.getTreeNode().getData().toString();
LOGGER.info("Collapsed:" + node);
}

// Getters and Setters
...
}


The onNodeCollapse and onNodeExpand events work fine and they are triggered accordingly when I expand/collapse the tree. However, when I made a selection on one of the nodes of the tree, I'll get that exception...

Anyone know why?

callahan
Posts: 768
Joined: 27 May 2010, 22:52

03 Sep 2010, 19:39

Does it work if you use

selection="#{treeBean.selectedNode}" instead of
selection="#{treeBean.selectedNodes}"

yhrchan
Posts: 11
Joined: 03 Sep 2010, 17:40

03 Sep 2010, 19:58

That worked! Thanks! that was a good catch...=)

callahan
Posts: 768
Joined: 27 May 2010, 22:52

03 Sep 2010, 20:04

No problem ;) .

I see that you're new in this neck of the woods. Happy PrimeFacing.

yhrchan
Posts: 11
Joined: 03 Sep 2010, 17:40

03 Sep 2010, 20:38

Yes I am new to primefaces and JSF2...

I am onto another problem right now...is there anyway to put commandLinks in the nodes of p:tree? I need to get this piece of navigation to work but so far no luck...

callahan
Posts: 768
Joined: 27 May 2010, 22:52

03 Sep 2010, 21:30

I've never tried it myself, but this post might help http://primefaces.prime.com.tr/forum/vi ... ?f=3&t=967. I hope the info there still applies today.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 53 guests