【问题标题】:PrimeFaces tree selection is nullPrimeFaces 树选择为空
【发布时间】:2011-09-15 10:30:14
【问题描述】:

我在 Glassfish 3.1 上使用 PrimeFaces 2.2.1 Tree 组件

我正在尝试将 Tree 上的选定节点设置为我的支持 bean 中的 TreeNode 对象,但它始终为空。

我在 PrimeFaces 论坛上寻求支持,但很遗憾没有得到回复。

<p:tree id="contextTree" value="#{contextTreeBean.contextRoot}" var="node" selectionMode="single" selection="#{contextTreeBean.selectedNode}">
        <p:treeNode>
            <h:outputText value="#{node.name}"/>
        </p:treeNode>
    </p:tree>
    <h:outputText id="output" value="#{contextTreeBean.output}"/>
    <p:commandButton id ="createButton" value="+" actionListener="#{contextTreeBean.createContext()}" update="contextTree, output"/>


@ManagedBean
@RequestScoped
public class contextTreeBean {

    @EJB
    private ContextFacadeLocal contextFacade;
    private Context context = new Context();
    private TreeNode contextRoot;
    private TreeNode selectedNode;
    private String output;

    /** Creates a new instance of contextTreeBean */
    public contextTreeBean() {
    }

    public void createContext() {
        output = selectedNode.getData().toString();
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

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

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public ContextFacadeLocal getContextFacade() {
        return contextFacade;
    }

    public void setContextFacade(ContextFacadeLocal contextFacade) {
        this.contextFacade = contextFacade;
    }

    public TreeNode getContextRoot() {
        return contextRoot;
    }

    public void setContextRoot(TreeNode contextRoot) {
        this.contextRoot = contextRoot;
    }

    @PostConstruct
    private void postConstruct() {
        populateContextTree();
    }

    private void populateContextTree() {
        buildContextTree(new DefaultTreeNode("Root", null), contextFacade.findRootContexts());
    }

    private void buildContextTree(TreeNode parentNode, List<Context> children) {
        for (Context currentContextNode : children) {
            TreeNode tempNode = new DefaultTreeNode(currentContextNode, parentNode);
            buildContextTree(tempNode, currentContextNode.getChildren());
        }
        contextRoot = parentNode;
    }
}

【问题讨论】:

  • 把bean改成查看作用域或者会话作用域是否有效?

标签: java jsf jsf-2 primefaces


【解决方案1】:

您是否尝试通过记录验证selectedNode 是否为空?也许它正在设置,但您的&lt;p:commandButton&gt;update 属性设置不正确。请记住,默认情况下,&lt;h:form&gt; 会将其 id 添加到子元素中。

同时验证&lt;h:form&gt;中的其他元素没有引发验证错误

此外,我不相信 Primefaces 树组件在由 @RequestScoped 托管 bean 支持时能够正常工作。尝试将托管 bean 更改为 @ViewScoped,以便托管 bean 的生命周期跨越各个请求。

【讨论】:

  • 我在 JSF 模板客户端中有 Tree 组件,模板中有表单。将表单移动到客户端页面有效。我能够保留支持 bean RequestScoped。模板客户端中是否应该始终包含表单组件?
  • @Laurence,是的,您会发现通常每个客户页面都应该至少有一个表单。此外,您可能会遇到一些 Primefaces 错误,您需要多个表格。一个例子是,如果您在一个页面上有多个对话框组件,最好将每个对话框的内容放在一个单独的表单中。
【解决方案2】:

我在 JSF 模板客户端中使用了 Tree 组件,其中包含模板中的表单。将表单移动到客户端页面有效。我能够保留支持 bean RequestScoped。

【讨论】:

  • 那么,你基本上是在嵌套表单?
  • 我在模板中有一个表单,但没有模板客户端。我以为表单会暴露给客户端页面,但显然不会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多