【问题标题】:Is this Primefaces bug or Mojarra/MyFaces bug这是 Primefaces 错误还是 Mojarra/MyFaces 错误
【发布时间】:2012-01-29 10:37:48
【问题描述】:

当我在column 的dataTable 内时,我似乎无法触发事件。这是我的简单演示

<h:form id="form">
    <!--This section of p:tree here seems to be the reason causing the event not fired when click the command button-->
    <p:tree value="#{viewBean.root}" var="node" dynamic="true" cache="false"  
                selectionMode="checkbox"  selection="#{treeBean.selectedNode}">  

        <p:ajax event="expand" update=":form:messages" listener="#{viewBean.onNodeExpand}" />  
        <p:ajax event="collapse" update=":form:messages" listener="#{viewBean.onNodeCollapse}" />  
        <p:ajax event="select" update=":form:messages" listener="#{viewBean.onNodeSelect}" />  
        <p:ajax event="unselect" update=":form:messages" listener="#{viewBean.onNodeUnselect}" />  

        <p:treeNode>  
            <h:outputText value="#{node}" />  
        </p:treeNode>  
    </p:tree>
    <h:panelGroup id="mygroup">
            <p:dataTable id="mytable" value="#{viewBean.foodList}" var="item">
                <p:column>
                    #{item}
                </p:column>
                <p:column>
                    <p:commandButton value="delete" 
                                     action="#{viewBean.delete}"
                                     update=":form:mygroup">
                        <f:setPropertyActionListener target="#{viewBean.selectedFood}"
                                                     value="#{item}"/>
                    </p:commandButton>
                </p:column>
            </p:dataTable>
    </h:panelGroup>
</h:form>

这是我的托管 bean

@ManagedBean
@ViewScoped
public class ViewBean {

    private TreeNode root;  

    private TreeNode selectedNode;  
    private List<String> foodList;

    private String selectedFood;

    @PostConstruct
    public void init(){

        foodList = new ArrayList<String>();
        foodList.add("Pizza");
        foodList.add("Pasta");
        foodList.add("Hamburger");

        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 void delete(){

        foodList.remove(selectedFood);

    }
    //setter and getter

    public void onNodeExpand(NodeExpandEvent event) {  
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", event.getTreeNode().toString());  

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

    public void onNodeCollapse(NodeCollapseEvent event) {  
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", event.getTreeNode().toString());  

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

    public void onNodeSelect(NodeSelectEvent event) {  
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().toString());  

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

    public void onNodeUnselect(NodeUnselectEvent event) {  
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", event.getTreeNode().toString());  

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

我的问题似乎是事件没有被触发,这意味着 delete 永远不会被调用。

我环顾四周,知道 Mojarra 在嵌套 UIData 组件方面存在大量问题,所以我尝试了 Mojarra 2.1.4,但仍然没有解决问题。我试过Myfaces 2.1.5,还是不行!!!这是 Primefaces 的错误吗?我什至使用h:panelGroup 作为update 的包装器,但它仍然不起作用。

编辑:原来dataTable上方的p:tree是点击commandButton时未触发事件的根本原因。如果我删除p:tree,那么它会立即工作。请注意,p:tree 本身工作得很好,当我点击结帐时,我的托管 bean 上触发了事件。

【问题讨论】:

  • 以后,请在“不起作用”方面更具体一些。例如。 “selectedFood 在 delete() 方法中是 null”。
  • @BalusC:对不起,我已经编辑了我的帖子以澄清我说does not work时的意思。
  • 很好,为了避免红鲱鱼,请不要忘记修复我最初发布答案的您自己的错误。您仍然不清楚“不起作用”。 “事件未触发”到底是什么意思?例如,根据 Firebug/Chrome 或其他什么,根本没有发送任何 ajax 请求?
  • #{treeBean} 不应该是#{viewBean}吗?
  • 您确定&lt;p:tree&gt; 有效吗?此异常表明它需要 TreeNode[] 而不是 TreeNode 的属性,这反过来表明即使删除整个 &lt;p:dataTable&gt; 也会遇到完全相同的问题。

标签: jsf primefaces myfaces mojarra


【解决方案1】:

这是您自己的代码中的错误。

<partial-response>
  <error>
    <error-name>class javax.el.ELException</error-name>
    <error-message><![CDATA[/index.xhtml @15,84 selection="#{viewBean.selectedNode}": Cannot convert [Lorg.primefaces.model.TreeNode;@fd9b4d of type class [Lorg.primefaces.model.TreeNode; to interface org.primefaces.model.TreeNode]]></error-message>
  </error>
</partial-response>

具体的异常消息表明&lt;p:tree selection&gt; 应该引用TreeNode[] 类型的属性,而不是TreeNode(注意消息中类标识符上的[ 前缀表示数组类型)。所以,相应地修复它:

<p:tree selection="#{treeBean.selectedNodes}">

与

private TreeNode[] selectedNodes;

无论如何,&lt;p:tree&gt; 和 &lt;p:dataTable&gt; 都在同一个“上帝”形式中。因此,它们中的任何一个中的任何操作都将提交另一个组件上的所有内容。如果树和表没有任何关系,那么它们应该各自放在自己的&lt;h:form&gt; 中。或者,您必须将process 属性添加到命令链接/按钮以仅处理包含感兴趣的组件,例如process="mytable" 在表格内的按钮上。

【讨论】:

  • 谢谢 BalusC。我先有selectionMode="single",然后我切换到selectionMode="checkbox",但我没有意识到我需要使用TreeNode[]。非常感谢。最后一个问题,Lorg.primefaces.model.TreeNode 是指 TreeNode[] 吗?
  • 不,它标识TreeNode 类型的对象。 [Lorg.primefaces.model.TreeNode 标识TreeNode 类型的对象数组。
  • 我明白了。谢谢你。今天学习一些调试技巧:)
猜你喜欢
  • 2012-03-31
  • 1970-01-01
  • 2015-01-05
  • 2012-03-09
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
相关资源
最近更新 更多