【问题标题】:Dynamic targets and id in composite component复合组件中的动态目标和 id
【发布时间】:2018-07-01 03:52:42
【问题描述】:

我有一个复合组件:

<composite:interface componentType="myCC">
    <composite:attribute name="tempId" required="false"/>
    <composite:attribute name="action" targets="??????" required="false"/>
</composite:interface>

<composite:implementation>
        <p:commandButton id="#{cc.attrs.tempId}button" 
            action="#{cc.attrs.action}"/>
</composite:implementation>

此外 - 我有两个使用它的组件 -

<span id="firstSpan">
    <sm:myCC id="fooid1" tempId="inFirstSpan"
      action="#{firstController.method('String')}"/>
</span>

<span id="secondSpan">
    <sm:myCC id="blaid1" tempId="inSecondSpan"
      action="#{secondController.method('String')}"/>
</span>

问题是,我尝试使用 targets 中的 id 但失败了。

在这种情况下我该如何实现?

【问题讨论】:

  • 为什么需要#{cc.attrs.tempId}?只需摆脱它并在复合的上下文中思考(忽略客户端),一切都会自己指出。
  • 嗨@BalusC,首先-感谢您的评论。到现在为止,commandButton id 只是 "button" - 这很好用。但是我们的客户要求我们根据包装它的span 更改commandButton id,而不更改“clientId”。有可能吗?

标签: jsf composite-component


【解决方案1】:

默认情况下,您当前的方法不应该工作,因为当 JSF 输出复合复合的子组件的 HTML 时,它将首先包含复合组件 id,然后是分隔符,然后是子组件 id,如下所示:

假设复合组件ID是“ccId”,子组件是“ccChildId”,输出的ID是这样的:

ccId:ccChildId

因此,如果您想动态引用子组件 ID,则必须合成包含父 ID 在内的调用,以镜像生成的输出 HTML。

在你的情况下,你的目标应该是这样的:

targets="fooid1:inFirstSpanbutton"

【讨论】:

  • 你的前半部分答案是正确的。你答案的后半部分不正确。
  • 嗨 @BalusC 自从我上次与 JSF 互动以来已经有一段时间了,介意请包含不正确的部分以便我修复它吗?
  • @Bonifacio - 你能根据我的例子举个例子吗?你的意思是-&lt;composite:attribute name="action" targets="#{cc.clientId}:#{cc.attrs.tempId}button" required="false"/&gt;
  • 是的,这就是我的意思,但正如@BalusC 所指出的,现在我不确定它是否会按预期工作。
猜你喜欢
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 2013-12-01
  • 2011-11-30
  • 1970-01-01
  • 2013-11-15
  • 2011-06-04
  • 1970-01-01
相关资源
最近更新 更多