Reputation: 334
I'm trying to create a custom JSF component and add to it a method expression. This is the code of my custom component:
@FacesComponent(AjaxCommand2.COMPONENT_TYPE)
public class AjaxCommand2 extends UIComponentBase {
public static final String COMPONENT_TYPE = "local.test.component.AjaxCommand2";
public static final String COMPONENT_FAMILY = "local.test.component.AjaxCommand2";
private MethodExpression listener;
public MethodExpression getListener() {
return listener;
}
public void setListener(MethodExpression listener) {
this.listener = listener;
}
@Override
public String getRendererType() {
return null;
}
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
}
This is my tag lib file:
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib id="test"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd" version="2.0">
<namespace>http://local.test/ui</namespace>
<tag>
<tag-name>ajaxCommand2</tag-name>
<component>
<component-type>local.test.component.AjaxCommand2</component-type>
</component>
<attribute>
<name>listener</name>
<required>false</required>
<type>javax.el.MethodExpression</type>
</attribute>
</tag>
</facelet-taglib>
And this is the relevant code in the JSF page:
<test:ajaxCommand2 listener="#{testSessionBean.testActionAjax}" />
My problem is that the setter for listener never is called in my custom component and always I'm getting null in listener property.
I cannot see whrere is the problem. Any idea?, I would like set the listener property to point to a specific method of one backed bean.
Upvotes: 4
Views: 1705
Reputation: 1602
Write a Handler for the Component like this:
public class MoveHandler extends ComponentHandler {
public MoveHandler(ComponentConfig config) {
super(config);
}
@Override
protected MetaRuleset createMetaRuleset(Class type) {
MetaRuleset metaRuleset = super.createMetaRuleset(type);
MetaRule metaRule = new MethodRule("listener", void.class, new Class[] {MoveEvent.class});
metaRuleset.addRule(metaRule);
return metaRuleset;
}
}
Upvotes: 1