如何创建一个在inputText和inputSecret之间切换

如何创建一个在inputText和inputSecret之间切换

本文介绍了如何创建一个在inputText和inputSecret之间切换的复合组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Facelets复合组件,该组件根据一个参数在inputText和inputSecret之间切换:

I'm writing a Facelets composite component that switches between using inputText and inputSecret based on a parameter:

<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>

问题是出现以下错误:

推荐答案

使用视图构建时间标记(如JSTL <c:if><c:choose>)代替JSF组件的rendered属性.在构建JSF组件树期间评估视图构建时间标签,而仅在基于JSF组件树生成HTML期间评估渲染属性(因此,您最终仍然拥有具有相同ID的两个组件)在JSF组件树中!).

Use a view build time tag like JSTL <c:if> or <c:choose> instead of the JSF component's rendered attribute. View build time tags are evaluated during constructing the JSF component tree, while the rendered attribute is only evaluated during generating HTML based on the JSF component tree (and thus you still end up with both components with the same ID in the JSF component tree!).

例如

<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>

另请参见:

  • JSF2 Facelets中的JSTL ...有意义吗?
  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • 无关与具体问题无关,myId没有任何意义.只要给那些固定的ID.如果原因是无法通过Ajax从外部引用它们,请转至在f:ajax渲染中引用复合组件ID .

      Unrelated to the concrete problem, the myId doesn't make sense. Just give those a fixed ID. In case the reason was the inability to reference them from outside by ajax, head to Referring composite component ID in f:ajax render.

      这篇关于如何创建一个在inputText和inputSecret之间切换的复合组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:52