【发布时间】:2016-01-20 09:21:57
【问题描述】:
我在公司“继承”了一个 JSF 2 (JSF 2.2.7) 应用程序并面临 java.lang.IllegalStateException,因为两个组件似乎具有相同的 ID。
视图的结构如下(我提取了相关代码以进行说明,它可能包含一些拼写错误/无效的语法,因为我更改了一些名称):
<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 循环中)
如果我点击编辑按钮,我会得到:
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,但我无法确定列出的原因中的哪一个是我的问题。我不使用任何绑定。
到目前为止,我尝试了什么:明确设置 id,即用 包围 mycomonent,将循环计数器作为 ID 传递给组件等。但没有成功。我认为问题无法在 mycomponent 内解决。我发现的唯一解决方法是制作 mycomponent 的物理副本并在我的 anotherForm 中引用该副本(这样 editItemDlg 和 anotherDlg 不使用相同的组件)。
感谢任何帮助
【问题讨论】:
-
经过仔细检查,堆栈跟踪中的树确实有问题。复合组件的实现被复制回同一个复合实例。这个不对。该问题的原因在目前提供的信息中不可见。请以 MCVE 格式 (stackoverflow.com/tags/jsf/info) 发布有问题的代码,并尝试将 Mojarra 升级到最新版本(当前为 2.2.12),以排除已经修复的错误。
-
@BalusC 与 2.2.12 相同的问题。我想我需要一些时间来创建一个实际运行的最小项目。
-
我在使用 Mojarra 2.2.7 时遇到了同样的问题。这个问题有什么进展吗?
-
对我来说似乎很奇怪的是生成的 id 序列被破坏了。注释过于严格,无法在此处粘贴组件树转储,但简而言之 - 看到生成的组件 ID 从 j_idt1 到 j_idt65 的顺序完美,然后它们突然中断到 j_idt311,然后继续使用 j_idt66。并在这个地方显示了重复的 id found 标记。
-
@BalusC 我试图在一个简单的 MCVE 项目中重现该错误,但我没有这样做......
标签: jsf facelets composite-component