我想在我的复合组件中实现“回调”功能。必要时,将从后备组件调用此方法。
XHTML部分是:
<cc:interface componentType="partnerSelComp">
<cc:attribute name="value" type="com.app.data.Partner"/>
<cc:attribute name="callback" method-signature="void notify(java.lang.Long)"/>
</cc:interface>
<cc:implementation>
<span id="#{cc.clientId}">
<p:inputText id="code" binding="#{cc.partnerCode}">
<p:ajax event="blur" update="code name msg" listener="#{cc.validate}" />
</p:inputText>
<p:outputLabel id ="name" binding="#{cc.partnerName}"/>
<p:message id="msg" for="code"/>
</span>
</cc:implementation>
支持组件的相关部分是:
@FacesComponent("partnerSelComp")
public class PartnerSelComp extends UIInput implements NamingContainer {
private InputText partnerCode;
private OutputLabel partnerName;
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
public void validate() {
...
MethodExpression exp = (MethodExpression) getAttributes().get("callback");
...
}
...
}
exp
以上获取空值。如何获取callback
属性,以及如何执行它?
答案 0 :(得分:0)
一种无参数调用函数的解决方案(Java 1.8)。
组件:
<composite:interface>
<composite:attribute name="buttonCallback" type="java.util.function.Consumer"/>
</composite:interface>
<composite:implementation>
<p:commandButton value="Submit" action="#{cc.attrs.buttonCallback.accept(null)}"/>
<composite:implementation>
使用范围:
<mycomp:myCC buttonCallback="#{myBackingBean.myCallback}"/>
在备用bean中的用法:
private Consumer<Void> myCallback;
@PostConstruct
public void init() {
myCallback= this::myFunction;
}
public void myFunction(Void v) { //... }