Monday, June 29, 2009

Optional actionListener for h:commandButton

Here is an implementation of the needed tag handler:

public final class ActionListenerHandler extends TagHandler {

private final static Class[] ACTION_LISTENER_SIG = new Class[]{ActionEvent.class};

private final TagAttribute value;

public ActionListenerHandler(TagConfig config) {
super(config);
this.value = this.getRequiredAttribute("value");
}

/*
* (non-Javadoc)
*
* @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
* javax.faces.component.UIComponent)
*/
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException {
if (parent instanceof ActionSource2) {
if (ComponentSupport.isNew(parent)) {
((ActionSource2) parent).addActionListener(new MethodExpressionActionListener(
value.getMethodExpression(ctx, null, ACTION_LISTENER_SIG)));
}
} else {
throw new TagException(this.tag,
"Parent is not of type ActionSource2, type is: "
+ parent);
}
}
}

Usage:

h:commandButton value="#{value}"
c:if test="#{not empty actionListener and actionListener}"
x:actionlistener value="#{bean.actionEvent}"
/c:if
/h:selectonemenu

No comments:

Post a Comment