0

我有一个带有数据表的 JSF 应用程序。每行都有一个“commandLink”。单击行的 commandLink 时,必须在控制台上显示行数据。

点击commandLink时出现错误,错误如下:

component with duplicate id "dataTable1:col1" found

viewId=/UserHome.xhtml
location=E:\workspaces_eclipse\webService\.metadata\.plugins \org.eclipse.wst.server.core\tmp2\wtpwebapps\JSF_Demo\UserHome.xhtml
phaseId=RENDER_RESPONSE(6)

Caused by:
java.lang.IllegalStateException - component with duplicate id "dataTable1:col1" found
at  org.apache.myfaces.view.facelets.compiler.CheckDuplicateIdFaceletUtils.checkIds(CheckDuplic    ateIdFaceletUtils.java:100)

该错误表明组件具有相同的 ID,但是我尝试为数据表的每个元素提供不同的“id”。

JSF 文件“UserHome.xhtml”的源代码如下:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!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:f="http://java.sun.com/jsf/core"   xmlns:h="http://java.sun.com/jsf/html">
     <h:head>
    <title>Resource Net 1.0</title>
    <h:outputStylesheet library="css" name="table-style.css"></h:outputStylesheet>
</h:head>
<h:body>
   <div align="center">
   <table width="90%" height="100%" border="0" style="cellspacing:0; border-radius:10px ;box-shadow: 0px 0px 15px 10px #888888">
    <tr>
        <td>
            <h2><div align="center">Resource Net 1.0</div></h2>
        </td>
    </tr>
    <tr>
        <td>                
            2. Generated by Map :
            <h:selectOneMenu value="#{userHomeListener.favCoffee2}">
                <f:selectItems value="#{userHomeListener.allDomains}" />
            </h:selectOneMenu>
        </td>
    </tr>       
    <tr>
        <td>
            <h:dataTable value="#{userHomeListener.documents}" var="doc"
                         binding="#{userHomeListener.documentsTable}"
                          id="dataTable1"                            
                         styleClass="order-table"
                         headerClass="order-table-header"
                         rowClasses="order-table-odd-row,order-table-even-row"
                         border="1">

                <h:column id="col1">
                    <f:facet name="header1">Document ID</f:facet>
                    #{doc.docID}
                </h:column>
                <h:column id="col2">
                    <f:facet name="header2">Document Name</f:facet>
                    #{doc.docName}
                </h:column>
                <h:column  id="col3">
                    <f:facet name="header3">Document Link</f:facet>
                     <h:form id="form1">
                     <h:commandLink id="link" value="#{doc.docLink}" action="#{userHomeListener.getRowData}"></h:commandLink>
                     </h:form>
                </h:column>
                <h:column id="col4">
                    <f:facet name="header4">Upload Date</f:facet>
                    #{doc.uploadDate}
                </h:column>

            </h:dataTable>
            <h:commandButton value="get row data" ></h:commandButton>
        </td>
    </tr>
    </table>

    </div>
</h:body>

我的代码有问题吗?请提出解决此问题的方法。

提前致谢。

4

1 回答 1

2

binding属性<h:dataTable>是这里的嫌疑犯。当 bean 的范围太广和/或您在该属性的 getter/setter 中做“错误的事情”时,可能会导致此类问题。

将 bean 放入请求范围和/或寻找替代方法以便您可以完全摆脱它binding应该可以解决这个问题。

binding属性和命令链接操作方法的名称的组合getRowData表明您只是使用它来获取当前表行。在使用旧的 JSF 1.x 时确实是这样,但在使用新的 JSF 2.x 时就不是这样了。当您运行支持 Servlet 3.0 / EL 2.2 的容器(Tomcat 7、Glassfish 3 等)时,这可以做得更好、更简单。

<h:dataTable value="#{userHomeListener.documents}" var="doc">
    <h:column>
        <h:form>
            <h:commandLink value="#{doc.docLink}" action="#{userHomeListener.getRowData(doc)}" />
        </h:form>
    </h:column>
</h:dataTable>

public void getRowData(Document doc) {
    // ...
}

你看,你可以直接传递#{doc}作为方法参数。

也可以看看:

于 2012-08-31T11:51:13.390 回答