我终于把这件事做好了。我所讨论的JSF组件应该有三个类:组件、呈现器和组件处理程序。
应将子UICommandLink添加到HintBubbleComponent的ComponentHandler中创建的组件的方法中:
public class HintBubbleHandler extends ComponentHandler {
public HintBubbleHandler(ComponentConfig config) {
super(config);
}
@Override
public void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent) {
HintBubbleComponent hintBubbleComponent = (HintBubbleComponent) c;
UICommandLink commandLink = new UICommandLink();
hintBubbleComponent.getChildren().add(commandLink);
commandLink.setParent(hintBubbleComponent);
commandLink.setId("okButton");
}
}
正因为如此,当回调访问树以查找执行操作的组件时,UICommandLink将出现在组件树中。
在渲染器的decode()方法中,actionListener应设置为UICommandLink:
@Override
public void decode(FacesContext context, UIComponent component) {
super.decode(context, component);
UICommandLink commandLink = (UICommandLink) component.getChildren().get(0);
MethodExpression actionListener = ((HintBubbleComponent) component).getActionListener();
if ( actionListener != null ) {
commandLink.setActionExpression(actionListener);
}
}
现在一切都在工作,动作被调用了!