标题确实说明了一切。 我尝试过,但失败并出现以下错误:
非法尝试将参数传递给复合组件查找表达式(即 cc.attrs.[identifier])。
我的尝试如下:
<composite:interface>
<composite:attribute name="removeFieldAction" method-signature="void action(java.lang.String)" />
</composite:interface>
<composite:implementation>
<h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction('SomeString')}"/>
</composite:implementation>
正确的做法是什么?
最佳答案
这确实行不通。之后您不能像这样传递“额外”参数。 method-signature
正如您所声明的那样,必须在使用复合组件的一侧实现。例如
<my:button action="#{bean.remove('Somestring')}" />
复合组件的实现应该像这样
<h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction}" />
<小时/>
如果这不是您想要的,并且您确实想从复合组件端传递它,那么我可以想到两种传递额外参数的方法:使用 <f:attribute>
使用 Action 监听器将其作为 attidional 组件属性传递,或 <f:setPropertyActionListner>
让 JSF 在调用操作之前将其设置为属性。但两者的复合组件都没有发生变化。您需要至少请求整个 bean 作为复合组件的属性。
这是一个带有 <f:setPropertyActionListener>
的示例。这会在调用操作之前设置属性。
<composite:interface>
<composite:attribute name="bean" type="java.lang.Object" />
<composite:attribute name="action" type="java.lang.String" />
<composite:attribute name="property" type="java.lang.String" />
</composite:interface>
<composite:implementation>
<h:commandButton value="Remove" action="#{cc.attrs.bean[cc.attrs.action]}">
<f:setPropertyActionListener target="#{cc.attrs.bean[cc.attrs.property]}" value="Somestring" />
</h:commandButton>
</composite:implementation>
将用作
<my:button bean="#{bean}" action="removeFieldAction" property="someString" />
根据上面的示例,bean 应该如下所示
public class Bean {
private String someString;
public void removeFieldAction() {
System.out.println(someString); // Somestring
// ...
}
// ...
}
如果您遵守特定约定,您甚至可以省略 property
完全属性。
关于jsf - 将方法参数/参数传递给复合组件操作属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6355543/