在视图中使用复合组件两次在JSF中复制组件ID [英] Duplicate component ID in JSF using composite component twice in view

查看:118
本文介绍了在视图中使用复合组件两次在JSF中复制组件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承"了我公司的JSF 2(JSF 2.2.7)应用程序,并且遇到了java.lang.IllegalStateException,因为两个组件似乎具有相同的ID.

I "inherited" a JSF 2 (JSF 2.2.7) application in my company and facing a java.lang.IllegalStateException because two component seem to have the same ID.

视图的结构如下(出于说明目的,我提取了相关代码,当我更改某些名称时,它可能包含一些拼写错误/无效的语法):

The structure of the view is as follows (I extracted the relevant code for illustration purpose, it may contain some typos/invalid syntax as I changed some names):

<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如下所示:

mycomponent.xhtml looks as follows:

<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循环中使用

Important: The mycomponent component is also used inside editItemDlg (in the same manner as in "anotherDlg"), i.e. within an dialog and forEach-loop)

如果单击editButton,则会得到:

If I click the editButton, I get:

java.lang.IllegalArgumentException: Component ID anotherForm:j_idt192:argValue  
has already been found in the view.

这很奇怪,因为"anotherDlg"在这种情况下不是openend,而是已经呈现.

Its rather strange because the "anotherDlg" is not openend in this case, but apparently already rendered.

我在StackTrace中获得以下信息(仅显示相关部分):

I get the following info in the StackTrace (only relevant parts shown):

         +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 somehow these component get rendered twice, but I cannot figure out why.

我已经走出低谷答案,但是我真的无法确定列出的原因中的哪一个是我的问题.我不使用任何绑定.

I've gone trough SO answer but I cant really determine which of the listed causes is the issue in my case. I don't use any bindings.

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

What I tried so far: played around with setting id excplicitly, i.e. surrounding mycomonent with , passing loop-counters as ID to the component etc.. with no success. I think the problem cannot be solved within mycomponent . The only workaround I found was to make a physical copy of mycomponent and refer to that copy in my anotherForm (such that editItemDlg and anotherDlg do not use the same components).

感谢您的帮助

推荐答案

您现在拥有的是具有相同ID的多个JSF组件,这些组件将无法正常工作.

What you have right now is multiple JSF components with the same ID, which is not going to work..

动态生成组件时,您必须在相应的id后面附加某种迭代索引.

When dynamically generating components you have to append some kind of an iteration index to the respective ids.

<p:inputText id="argValue_#{bean.counter}" value="#{cc.attrs.arg}" />
    <p:message id="argValueMessage_#{bean.counter}" for="argValue" />

您最好的办法是完全删除id标记,然后让JSF自动生成它们.

The best thing you can do though is remove the id tag completely and let JSF auto-generate them.

如果您当然不从其他地方引用这些ID.

If, of course you do not refence those ids from somewhere else.

这篇关于在视图中使用复合组件两次在JSF中复制组件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆