我正在编写一个Facelets复合组件,它根据参数在inputText和inputSecret之间切换:
<composite:interface>
<composite:attribute name="myId" required="true"/>
<composite:attribute name="secret" required="false" default="false" />
</composite:interface>
<composite:implementation>
<h:inputSecret rendered="#{cc.attrs.secret}" id="#{cc.attrs.myId}" />
<h:inputText rendered="#{!cc.attrs.secret}" id="#{cc.attrs.myId}" />
</composite:implementation>
问题是我收到以下错误:
在视图中已找到组件ID [JSF mangled id]。
答案 0 :(得分:4)
使用视图构建时标记,例如JSTL <c:if>
或<c:choose>
,而不是JSF组件的rendered
属性。在构建JSF组件树期间评估视图构建时间标记,而仅在基于JSF组件树生成HTML期间评估呈现的属性(因此您仍然最终得到具有相同ID的两个组件在JSF组件树中!)。
E.g。
<c:if test="#{not cc.attrs.secret}">
<h:inputText id="input" />
</c:if>
<c:if test="#{cc.attrs.secret}">
<h:inputSecret id="input" />
</c:if>
无关具体问题,myId
没有意义。只需给那些固定的ID。如果原因是无法通过ajax从外部引用它们,请前往Referring composite component ID in f:ajax render。
答案 1 :(得分:0)
组件是否实际呈现无关紧要。视图的内部组件树中仍然存在两个组件,并且需要唯一的ID。我们也遇到了这个问题。
我们用_1和_2为id加上后缀,如果我们需要在javaScript中获取id,我们使用JQuery的部分匹配器。
在你的情况下,你能不能让你的bean的getMyId()方法根据secret属性的值返回一个不同的id?