【问题标题】:How to create a composite component which switches between inputText and inputSecret?如何创建在 inputText 和 inputSecret 之间切换的复合组件?
【发布时间】:2012-08-10 20:50:22
【问题描述】:

我正在编写一个基于参数在使用 inputText 和 inputSecret 之间切换的 Facelets 复合组件:

<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] 已在视图中找到。

【问题讨论】:

    标签: jsf facelets composite-component


    【解决方案1】:

    使用 JSTL &lt;c:if&gt;&lt;c:choose&gt; 之类的视图构建时间标记,而不是 JSF 组件的 rendered 属性。在构建 JSF 组件树期间评估视图构建时间标记,而仅在基于 JSF 组件树生成 HTML 期间评估渲染属性(因此您仍然会得到具有相同 ID 的 both 组件在 JSF 组件树中!)。

    例如

    <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

    【讨论】:

    • 太棒了!我只是不想在 facelets 中使用 jstl 标签。似乎它们仍然有它们的用途。谢谢,这对我们当前的项目也很有用。
    • 这会有很大帮助 - 谢谢!
    【解决方案2】:

    组件是否实际渲染并不重要。这两个组件仍将存在于视图的内部组件树中,并且需要唯一的 id。我们也遇到了这个问题。

    我们使用 _1 和 _2 作为 id 后缀,如果我们需要在 javaScript 中获取 id,我们使用 JQuery 的部分匹配器。

    在你的情况下,你能不能让你的 bean 的 getMyId() 方法根据 secret 属性的值返回不同的 id?

    【讨论】:

      猜你喜欢
      • 2010-11-08
      • 2021-04-20
      • 2012-08-17
      • 2017-08-14
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      相关资源
      最近更新 更多