这确实不起作用。之后你不能再传递“额外”的参数。您已声明的method-signature
必须在使用复合组件的一侧实现。例如。
<my:button action="#{bean.remove('Somestring')}" />
复合组件实现应该只是看起来像这样
<h:commandButton value="Remove" action="#{cc.attrs.removeFieldAction}" />
如果这不是你想要什么,你真的要在复合材料部件边通过它,那么我可以请考虑传递额外参数的两种方法:使用<f:attribute>
与动作侦听器将其作为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
属性。
Tnx。 想法是复合组件创建一个字段列表,每个字段链接到bean中的一个对象。一旦从UI中删除一个字段,必须通知bean已删除字段的ID,以便它也将从bean中删除。因此,'someString'参数实际上是被删除字段的UUID。 我在实践中试图用一个参数来实现类似于事件监听器的事情...... 感谢您的解决方案! – Ben 2011-06-15 12:25:01
Hi BalusC。非常感谢你的回复。这适用于Mojarra,但似乎不适用于MyFaces。我在这里发布了一个单独的问题,请你有空看看我有空吗? http://stackoverflow.com/questions/17357593/passed-argument-to-method-inside-composite-component-does-not-work-on-myfaces非常感谢你 – 2013-06-28 05:09:48