Question

I am trying to use a very simple tree example from the primefaces sample. I have a button that is supposed to show what is currently selected. But the result of the getselection function is always null. (I have a call in the xhtml that loads the bean - I don't know if maybe this is the problem) xhtml and beans code follows:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
   <f:event type="preRenderView" listener="#{masterpage_bean.init()}" />


<h:body>
      <h:form>     
<p:layout style="min-width:650px;min-height:400px;" id="head">  
    <p:layoutUnit position="center"  size="275"  resizable="true">

    <p:growl id="messages" showDetail="true" />      
    <p:panel header="Work">  
         <h:panelGrid columns="2" cellpadding="5">  


       <p:tree id="treeSingle" value="#{masterpage_bean.root}" var="node" style="font-size: 12px"
        selectionMode="single"  

        selection="#{masterpage_bean.selectedNode}">  


    <p:treeNode>     
       <h:outputText value="#{node}" />  

    </p:treeNode>  


</p:tree>  
        <p:commandButton value="Display Selected" action="#{masterpage_bean.displaySelectedSingle}" id="btnDisplay"/>          



         </h:panelGrid>     
  </p:panel>

</p:layoutUnit>
    </p:layout>       
                </h:form>
</h:body>

and the bean:

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ViewScoped
@ManagedBean 
public class Masterpage_bean  {


TreeNode root;
TreeNode selectedNode;  


   public void init(){
   selectedNode=new DefaultTreeNode("root",null);

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

    TreeNode node0 = new DefaultTreeNode("Node 0", root);  
    TreeNode node1 = new DefaultTreeNode("Node 1", root);  
    TreeNode node2 = new DefaultTreeNode("Node 2", root);  

    TreeNode node00 = new DefaultTreeNode("Some guy G.J", node0);  
    TreeNode node01 = new DefaultTreeNode("Another the third", node0);  


   }     


  public TreeNode getRoot() {
    return root;
}



   public TreeNode getSelectedNode() {  
    return selectedNode;  
}  

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



    public void displaySelectedSingle() {  
      if(selectedNode != null) {  
          // this never happens ! 
    }  

} 

}
Was it helpful?

Solution

In xhtml, i changed the "action" button for "actionListener" and quit event tag. In the java code, i used constructor instead of "init" method. Try this

the Java code

import java.io.Serializable;  
import javax.faces.application.FacesMessage;  
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;  
import javax.faces.event.ActionEvent;  

import org.primefaces.model.DefaultTreeNode;  
import org.primefaces.model.TreeNode;  

@ViewScoped
@ManagedBean (name="masterpage_bean")
public class TreeBean implements Serializable {  

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private TreeNode root;  

    private TreeNode selectedNode;  

    public TreeBean() {  
        root = new DefaultTreeNode("Root", null);  
        TreeNode node0 = new DefaultTreeNode("Node 0", root);  
        TreeNode node1 = new DefaultTreeNode("Node 1", root);  
        TreeNode node2 = new DefaultTreeNode("Node 2", root);  

        TreeNode node00 = new DefaultTreeNode("Some guy G.J", node0);  
        TreeNode node01 = new DefaultTreeNode("Another the third", node0);
    }  

    public TreeNode getRoot() {  
        return root;  
    }  

    public TreeNode getSelectedNode() {  
        return selectedNode;  
    }  

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

    public void displaySelectedSingle(ActionEvent event) {  
        if(selectedNode != null) {  
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());  

            FacesContext.getCurrentInstance().addMessage(null, message);  
        }  
    }  
}  

the XHTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/facelets"
      >
<h:head>
    <title>Facelet Title</title>
</h:head>

<h:body>
   <h:form>     
<p:layout style="min-width:650px;min-height:400px;" id="head">  
    <p:layoutUnit position="center"  size="275"  resizable="true">

    <p:growl id="messages" showDetail="true" />      
    <p:panel header="Work">  
         <h:panelGrid columns="2" cellpadding="5">  


       <p:tree id="treeSingle" value="#{masterpage_bean.root}" var="node" style="font-size: 12px"
        selectionMode="single"  

        selection="#{masterpage_bean.selectedNode}">  


    <p:treeNode>     
       <h:outputText value="#{node}" />  

    </p:treeNode>  


</p:tree>  
        <p:commandButton value="Display Selected" actionListener="#{masterpage_bean.displaySelectedSingle}" update="messages" id="btnDisplay"/>          

         </h:panelGrid>     
  </p:panel>

</p:layoutUnit>
    </p:layout>       
                </h:form>

</h:body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top