【问题标题】:Component ID has already been found in the view when using datatable with binding and Session Scoped [duplicate]使用具有绑定和会话范围的数据表时,已在视图中找到组件 ID [重复]
【发布时间】:2012-09-03 12:51:23
【问题描述】:

我有一个 XHTML 页面,它在提交时会返回到自身。 支持 bean 是会话范围的。在重定向到自身时,页面呈现 h:datatable 两次并给了我重复的 id 错误。我可以直观地看到表格被呈现两次,并且彼此相邻。

** 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:f="http://java.sun.com/jsf/core">

                <f:view>
                <h:form  >

                <h:dataTable binding="#{ecole.dataTable}" value="#{ecole.getEcoleList()}" var="c"
                border="0" width="100%" cellpadding="0" cellspacing="0" 
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row"
            >

                        <h:column>
                            <f:facet name="header">
                                ID
                            </f:facet>
                                #{c.idEcole}
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                Nom
                            </f:facet>
                                #{c.nomEcole}
                        </h:column>

                        <h:column>
                            <f:facet name="header">
                                Description
                            </f:facet>
                                #{c.desc_ecl}
                        </h:column>
                        <h:column>
                             <f:facet styleclass="options-width" name="header">
                                        Options
                             </f:facet>
                             <h:commandLink action="#{ecole.editEcoleItem()}" title="Edit" >
                                <h:graphicImage style="border:0" url="/icones/b_edit.png" />
                             </h:commandLink>
                             &#xa0;&#xa0;&#xa0;
                             <h:commandLink title="Delete" 
                             onclick="return confirm('Voulez-vous confirmer la suppression?') ;" 
                                 action="#{ecole.deleteEcole(c)}"
                             >
                              <h:graphicImage style="border:0" url="/icones/b_drop.png" />
                             </h:commandLink>
                        </h:column>

        </h:dataTable>
                <!--  end product-table................................... --> 
                </h:form>
                </f:view>

这是显示的错误消息:

 java.lang.IllegalStateException: Component ID j_id15:j_id16:j_id29 has already been found in the view.  See below for details.
    +id: null
     type: javax.faces.component.UIViewRoot@1abe6f6
      +id: javax_faces_location_HEAD
       type: javax.faces.component.UIPanel@c84a5d
        +id: j_id4
         type: javax.faces.component.UIOutput@18a5776
        +id: j_id22
         type: javax.faces.component.UIOutput@1742dcc
      +id: j_id19
       ...

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    binding 属性应该绑定到请求范围的 bean,或者完全删除并替换为更好的替代方案,具体取决于具体的功能要求。

    如果我获得了能够在editEcoleItem() 方法中检索当前项目的功能要求,那么您可以直接将其作为方法参数传递,就像您在deleteEcole() 中所做的那样。这样您就可以完全删除 binding 属性。这就是 JSF 2.0 / EL 2.2 的方式。也许您过于关注旧的 JSF 1.x 示例。使用 JSF 2.x 开发时不应该这样做。

    另见:

    【讨论】:

    • 我删除了绑定,并且我更改了 editEcoleItem() 正如您在回答中提到的那样,它现在可以工作了。是的,你是对的,我过于关注旧的 JSF 1.x 示例......您不仅拯救了我的一天 Mr.BalusC,而且还拯救了我的项目。现在我的项目完美运行,我不知道如何感谢你!在两个月的时间里,你的教程很有帮助,你的答案解决了我的问题。非常感谢你:)
    • drewdev.blogspot.com/2009/01/jsf-component-binding-stinks.html 正如@BalusC 所说:我们不应该在请求范围之外使用绑定。我刚刚遇到了这个问题,在阅读了这个答案之后,我的疑问消失了,我的程序现在也正在运行。但是,我确实想分享一个我发现的链接,说明为什么它对这种行为不是一个好主意。如果有人需要知道这个答案背后的原因,请转到上面的 URL。
    • @Rash:那个博客很臭。最好参考stackoverflow.com/questions/12506679/…
    • @BalusC 不错,它更清晰、更简短;)谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2013-09-11
    • 2011-12-28
    • 2017-10-23
    • 2013-04-28
    • 2012-07-28
    相关资源
    最近更新 更多