我在 Glassfish 3 上运行 Primefaces 3.2 和 JSF 2.0。
我已经尝试了很多,以编程方式从托管 bean 设置选定的节点。这包括像这样设置选定的节点:
public void setSelectedTreeNode(String name) {
TreeNode root = treeBean.getRoot();
List<TreeNode> tree = root.getChildren();
for(TreeNode node:tree) {
if(node.getData().toString().contains(name)) {
System.out.println("found the node to select");
treeBean.setSelectedNode(node);
break;
}
}
RequestContext context = RequestContext.getCurrentInstance();
context.update(":navForm:treeSingle");
}
“找到要选择的节点”打印在终端中,但未在网页的树中选择该节点..
树是这样的:
<h:form id="navForm">
<p:tree id="treeSingle" value="#{treeBean.root}" var="node"
selectionMode="single" styleClass="treeStyle"
selection="#{treeBean.selectedNode}"
cache="false"
>
<p:ajax event="select" listener="#{treeBean.onNodeSelect}" update=":mainForm" />
<p:treeNode>
<h:outputText value="#{node}" escape="false" />
</p:treeNode>
编辑:TreeBean 是这样构建的:
@ManagedBean
@SessionScoped
public class TreeBean implements Serializable {
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("Node 0.0", node0);
TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);
TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);
TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);
TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);
TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);
TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);
TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);
}
public TreeNode getRoot() {
return root;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
}
有谁知道如何做到这一点?