错误? - 具有URL参数的PrimeFaces Tree SelectEvent

时间:2017-01-18 14:20:03

标签: java jsf primefaces jsf-2 tree

我有一个页面,可以通过viewParam访问并包含PrimeFaces树组件。

在树上发生的第一个SelectEvent期间,在调用选择侦听器之前会捕获对setId的调用?我认为只应在首页加载时调用setId?

后续选择事件似乎正常,不会调用setId。

这里是重现这种怪异行为的代码:

树test.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<f:view locale="fr">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputScript library="javax.faces" name="jsf.js" target="head"/>
    </h:head>
    <h:body>
        <div class="container">
            <f:metadata>
                <f:viewParam name="id" value="#{treeTestController.id}" />
            </f:metadata>
            <h:form>
                <h3 style="margin-top: 0">Client</h3>
                <p:tree value="#{treeTestController.root}" var="node" selection="#{treeTestController.selectedNode}" selectionMode="single">
                    <p:ajax event="select" update="@this" listener="#{treeTestController.onNodeSelect}" />
                    <p:treeNode>
                        <h:outputText value="#{node}" />
                    </p:treeNode>
                </p:tree>
            </h:form>
        </div>
    </h:body>
</f:view>
</html>

TreeTestController.java

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

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

@ManagedBean
@ViewScoped
public class TreeTestController implements Serializable {
    private static final long serialVersionUID = 1L;
    private TreeNode root;
    private TreeNode selectedNode;
    private Long id;

    @PostConstruct
    public void init() {
        root = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("Node 0", root);
        TreeNode node1 = new DefaultTreeNode("Node 1", 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);

        node1.getChildren().add(new DefaultTreeNode("Node 1.1"));
        node00.getChildren().add(new DefaultTreeNode("Node 0.0.0"));
        node00.getChildren().add(new DefaultTreeNode("Node 0.0.1"));
        node01.getChildren().add(new DefaultTreeNode("Node 0.1.0"));
        node10.getChildren().add(new DefaultTreeNode("Node 1.0.0"));
        root.getChildren().add(new DefaultTreeNode("Node 2"));
    }

    public void onNodeSelect(NodeSelectEvent event) {
        selectedNode = event.getTreeNode();
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

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

}
  1. 访问网址:tree-test.html?id = 1
  2. 选择一个节点(调用setId而不是仅调用onNodeSelect
  3. 我不知道这是一个错误还是缺少某些东西,任何人都可以提供帮助?

0 个答案:

没有答案