我可以在复合组件中调用bean上的方法吗?

时间:2012-08-16 19:44:01

标签: jsf-2 composite-component

我想编写一个或多或少的gemeric组件,在那里我交出一个控制器bean,组件应该显示一些CRUD按钮。

遵循复合组件:

<composite:interface>
  <composite:attribute name="controller" />
  <composite:attribute name="object" />
</composite:interface>

<composite:implementation>

  <h:panelGrid columns="3" columnClasses="celltop">
    <h:commandButton id="save" value="#{msg.saveButtonLabel}"
      action="#{cc.attrs.controller.save}" />
    <h:commandButton id="delete" value="#{msg.deleteButtonLabel}"
      action="#{cc.attrs.controller.delete(cc.attrs.object)}" />
    <h:commandButton id="cancel" value="#{msg.backButtonLabel}"
      action="#{cc.attrs.controller.cancel}" immediate="true" />
  </h:panelGrid>

</composite:implementation>

<viewController:buttons controller="customerController" object="#{customerController.customer}"/>
@Named
@ConversationScoped
public class CustomerController implements Serializable {

    public String cancel() {
    customer = null;
    if (!conversation.isTransient()) {
        conversation.end();
    }
    return "cancelled";
}
单击“取消”按钮时,

导致以下异常:

javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /resources/components/viewController/buttons.xhtml @25,65 action="#{cc.attrs.controller.cancel}": Method not found: customerController.cancel()
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:92)

是否无法调用提供给CC的bean的方法?

1 个答案:

答案 0 :(得分:3)

是的,你可以。您的错误就是您只传递了一个普通的vanilla String,表示托管bean名称为属性值

controller="customerController"

虽然您实际上应该从EL范围传递具体的托管bean实例

controller="#{customerController}"

此外,异常消息有些误导,但它基本上只是显示属性值的Object#toString()。如果它是一个具体的托管bean实例,你宁愿看到像

这样的东西
  

找不到方法:com.example.CustomerController@12345678.cancel()

或其toString()实现返回的任何内容,如果被覆盖。