primefaces树选择始终返回null

时间:2014-03-03 10:52:28

标签: java jsf-2 primefaces

我正在尝试使用primefaces示例中的一个非常简单的树示例。我有一个按钮,可以显示当前选择的内容。但getselection函数的结果始终为null。 (我在xhtml中调用了加载bean - 我不知道这可能是问题) xhtml和bean代码如下:

<!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>

和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 ! 
    }  

} 

}

1 个答案:

答案 0 :(得分:0)

在xhtml中,我更改了“actionListener”的“action”按钮并退出了事件标记。 在java代码中,我使用构造函数而不是“init”方法。 试试这个

Java代码

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);  
        }  
    }  
}  

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>