jsf - 在 View 中使用复合组件两次在 JSF 中重复组件 ID

标签 jsf facelets composite-component

我在我的公司“继承”了一个 JSF 2 (JSF 2.2.7) 应用程序并面临 java.lang.IllegalStateException,因为两个组件似乎具有相同的 ID。

View 的结构如下(我提取了相关代码以进行说明,由于我更改了一些名称,它可能包含一些拼写错误/无效语法):

<p:commandButton id="editButton"
   action="#{controller.prepareItem()}"
   update=":itemEditDlg" oncomplete="PF('itemtEditDlg').show()" />


<comp:editItemDlg id="itemEditDlg"  />

<p:dialog id="anotherDlg" >
   <h:form id="anotherForm">
      <c:forEach items="#{controller.allArgs}" var="arg" >
         <!-- next line is the problem -->
         <comp:mycomponent arg="#{arg}"  />
      </c:forEach>
   </h:form>
</p:dialog>

mycomponent.xhtml 如下所示:
<cc:interface>
    <cc:attribute name="arg" required="true" />
</cc:interface>
<cc:implementation>
    <p:inputText id="argValue" value="#{cc.attrs.arg}" />
    <p:message id="argValueMessage" for="argValue" />
</cc:implementation>

重要提示:mycomponent 组件也在 editItemDlg 中使用(与“anotherDlg”中的方式相同),即在对话框和 forEach 循环中)

如果我单击editButton,我会得到:
java.lang.IllegalArgumentException: Component ID anotherForm:j_idt192:argValue  
has already been found in the view.

这很奇怪,因为在这种情况下“anotherDlg”不是开放式的,但显然已经渲染了。

我在 StackTrace 中获得以下信息(仅显示相关部分):
         +id: j_idt192
             type: javax.faces.component.UINamingContainer@399bd0dc
              +id: j_id2
               type: javax.faces.component.UIPanel@24ad3910
                +id: argValue  <===============
                 type: org.primefaces.component.inputtext.InputText@687d5c3f
                +id: argValueMessage
                 type: org.primefaces.component.message.Message@7e3361b0
                +id: argValue  <===============
                 type: org.primefaces.component.inputtext.InputText@5f52aa8a
                +id: argValueMessage
                 type: org.primefaces.component.message.Message@2c3a7aea

所以不知何故这些组件被渲染了两次,但我不知道为什么。

我经历了SO answer但我无法真正确定列出的哪些原因是我的问题。我不使用任何绑定(bind)。

到目前为止我尝试过的是:明确设置 id ,即用 包围 mycomonent ,将循环计数器作为 ID 传递给组件等。但没有成功。我认为问题无法在 mycomponent 内解决。我发现的唯一解决方法是制作 mycomponent 的物理副本并在我的 anotherForm 中引用该副本(这样 editItemDlg 和 anotherDlg 不使用相同的组件)。

任何帮助表示赞赏

最佳答案

您不应为组件上的输入文本分配 id。

如果您正在遍历表单上的数组,则可以肯定您的输入文本元素将被创建多次。

The specified identifier must be unique among all the components (including facets) that are descendents of the nearest ancestor UIComponent that is a NamingContainer, or within the scope of the entire component tree if there is no such ancestor that is a NamingContainer.



UINamingContainer 中所述.

让我们考虑一下您的 controller.allArgs list 上有两个项目。

您生成的输出:
<form id="anotherForm">
   <input type="text" id="**argValue**">
   <p:message>
   <input type="text" id="**argValue**">
   <p:message>
</form>

这将导致重复的 id 异常。

您可以创建一个新的命名容器来保存您的 input id唯一或在您的添加上附加索引信息。

关于jsf - 在 View 中使用复合组件两次在 JSF 中重复组件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33258296/

相关文章:

JSF/Facelets : why is it not a good idea to mix JSF/Facelets with HTML tags?

jsf-2 - 嵌套在 ui 内时,具有自定义支持组件的复合组件会奇怪地损坏 :repeat

jsf - 查看范围: java. io.NotSerializedException : javax. faces.component.html.HtmlInputText

jsf - 在 session 超时时删除 JSF 托管 bean

java - JSF bean 中的动态字段

JSF Composite 组件的性能

使用 AJAX 的 JQuery DateRangePicker 到 JSF 复合组件

java.lang.NoClassDefFoundError : javax/faces/context/FacesContextFactory 错误

java - 是否可以在 JSF 中实现动态组件树?

css - 识别当前选定列表项的 Facelets 策略