You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在目标中使用复合组件属性?动态ID绑定及action报错求助

Alright, let's tackle this problem head-on. The error you're seeing happens because the targets attribute in your composite component's <composite:attribute> doesn't handle dynamic EL expressions (like #{cc.attrs.componentName}) the way you might expect. Here's why and how to fix it:

Why the Error Occurs

When JSF initializes your composite component, it tries to resolve the targets value immediately. At that point, #{cc.attrs.componentName} hasn't been populated with the value you pass when using the component (like Foo2101), so JSF looks for a component with the literal ID #{cc.attrs.componentName}button—which doesn't exist, hence the error. This also blocks your action method from binding correctly.

The Fix: Ditch targets and Bind Directly

Instead of relying on targets to route the action attribute, we'll bind it directly to the command button in the composite implementation. This lets the EL expression resolve properly when the component is used, not during initialization.

Step 1: Update the Composite Interface

Remove the targets attribute and add a method-signature to ensure the action attribute accepts method expressions correctly:

<composite:interface componentType="nestedGridCellEditor">
    <composite:attribute name="componentName" required="false" shortDescription="Name of the attribute to display in the id"/>
    <composite:attribute name="action" required="false" method-signature="java.lang.String action()" shortDescription="A method expression or a string outcome to process when command is executed"/>
</composite:interface>

Step 2: Adjust the Composite Implementation

Bind the action attribute directly to the <p:commandButton>. This ensures the dynamic ID and action expression work as intended:

<composite:implementation>
    <span id="#{cc.clientId}" style="display: flex;">
        <p:commandButton id="#{cc.attrs.componentName}button" action="#{cc.attrs.action}"/>
    </span>
</composite:implementation>

Step 3: Keep Using the Component As Before

Your existing usage code works perfectly with this change—no modifications needed here:

<sm:nestedGridCellEditor id="DateNGCE" componentName="Foo2101" action="#{contractTermsBudgetController.updateRow('Date', budget)}"/>

Why This Works

Now when you use the component:

  • #{cc.attrs.componentName} resolves to Foo2101, so the command button's full ID becomes DateNGCE:Foo2101button (combining the composite's client ID with your custom name, exactly what you wanted for identification).
  • The action attribute binds directly to your method expression, so it triggers correctly when the button is clicked.

Bonus Tip

If you need to pass other attributes to the command button (like ajax, icon, or style), you can add them to the composite interface and bind them directly in the implementation the same way we did with action. This avoids the limitations of targets for dynamic values.

内容的提问来源于stack exchange,提问作者Dvir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:40:17