将方法表达式传递给自定义组件

时间:2013-09-05 10:45:29

标签: jsf custom-component methodexpression

我正在寻找一个关于如何将方法表达式传递给自定义组件的最小示例。我尝试了以下方法,但是我的组件的负责的setter永远不会被调用。

我的观点:

public String bar(){
    //do stuff
    return "";
}


我的观点:

<my:comp foo="#{bean.bar}" />


Bean为我的组件:

private static final String FOO = "foo";
public void setFoo(MethodExpression me){
    //never called
    getStateHelper().put(FOO, me);
}

public MethodExpression getFoo(){
    //actually gets called
    return (MethodExpression) getStateHelper().get(FOO);
}


在我的组件渲染器中,我调用component.getFoo()并获得NPE。

1 个答案:

答案 0 :(得分:1)

您需要实现ActionSource2界面:

@FacesComponent(MyComponent.COMPONENT_TYPE)
public class Mycomponent extends UIComponentBase implements ActionSource2 {
    // ...
}

或者,更容易,从UICommand类(也被<h:commandXxx>组件使用)扩展,其中所有这些ActionSource2方法已经以正确的方式实现,因此您不能我需要重复这份工作:

@FacesComponent(MyComponent.COMPONENT_TYPE)
public class Mycomponent extends UICommand {
    // ...
}

无论哪种方式,您都可以使用action="#{some method expression}"甚至在其上附加actionlisteners。请注意,您无法重命名属性名称action。它必须是action

此类自定义组件的开源示例是OmniFaces <o:commandScript>。源代码可用here

相关问题