我可以在复合组件中的bean上调用方法吗? [英] Can I call methods on beans in composite components?
问题描述
我想编写一个或多或少的gemeric组件,在该组件中,我将提交一个控制器bean,并且组件应显示一些CRUD按钮.
I want to write a more or less gemeric component, where i hand in a controller bean and the componend should display some CRUD buttons.
以下复合组件:
<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";
}
当我单击取消"按钮时,
导致以下异常:
leads to the following exception when I click on the Cancel button:
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上调用方法吗?
Is it not possible to call methods on beans given to CC?
推荐答案
可以.您的错误只是您仅传递了一个普通香草String
,它代表托管Bean名称为属性值
Yes you can. Your mistake is just that you passed only a plain vanilla String
representing the managed bean name as attribute value
controller="customerController"
您实际上应该已经从EL范围传递了具体的托管bean实例
while you should actually have passed the concrete managed bean instance from the EL scope
controller="#{customerController}"
诚然,异常消息在某种程度上具有误导性,但它基本上只是显示属性值的Object#toString()
.如果它是一个具体的托管bean实例,您宁愿看到类似
The exception message is admittedly somewhat misleading, but it's basically just showing the Object#toString()
of the attribute value. If it were a concrete managed bean instance, you'd rather have seen something like
未找到方法:com.example.CustomerController@12345678.cancel()
Method not found: com.example.CustomerController@12345678.cancel()
或它的toString()
实现返回的任何内容(如果被覆盖).
or whatever is been returned by its toString()
implementation, if overridden.
这篇关于我可以在复合组件中的bean上调用方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!