<f:setPropertyActionListener> Parent 不是 ActionSource 类型,类型是:com.sun.faces.component.PassthroughElement

<f:setPropertyActionListener> Parent is not of type ActionSource, type is: com.sun.faces.component.PassthroughElement

我在我的 JSF 项目中使用 Passthrough 元素,并且需要做类似的事情:

<h:commandLink action="#{myBean.acao()}" value="click me">
        <f:setPropertyActionListener target="#{myBean.object}" value="#{localObject}"/>
</h:commandLink>

但使用 Passthrough 元素可以更好地控制我的前端,如下所示:

<a href="#" jsf:action="#{myBean.acao()}">click me
        <f:setPropertyActionListener target="#{myBean.object}" value="#{localObject}"/>
</a>

但显然这不起作用,我收到以下错误消息:

<f:setPropertyActionListener> Parent is not of type ActionSource, type is: com.sun.faces.component.PassthroughElement

有谁知道我该如何解决这个问题?

看起来只是您的 Mojarra 版本中的一个错误。它适用于当前最新的 2.2.12 版本。

您可以通过使用传递方法参数的 EL 2.2 功能来解决它​​。另见 Invoke direct methods or methods with arguments / variables / parameters in EL。它肯定在您的环境中可用,因为 #{myBean.acao()} 显然没有抛出 EL 异常(EL 2.2 之前不支持此语法)。

<a href="#" jsf:action="#{myBean.acao(localObject)}">click me</a>

如果您绝对需要在动作侦听器事件期间调用 setter,例如因为你想通过抛出 AbortProcessingException 来控制 action 的调用,以防设置值无效,另请参阅 Differences between action and actionListener,然后声明一个 jsf:actionListener.

<a href="#" jsf:actionListener="#{myBean.setObject(localObject)}" jsf:action="#{myBean.acao}">click me</a>