I want to use multiple action listener to set state of two backing beans before further processing
1st way:
<p:commandButton process="@this" >
<f:attribute name="key" value="#{node.getIdTestGroup()}" />
<f:actionListener binding="#{testController.nodeListener}" />
<f:actionListener binding="#{testDeviceGroupController.prepareCreate}" />
</p:commandButton>
It give an exception:
WARNING: /testGroup/List.xhtml @26,88 binding="#{testController.nodeListener()}": Method nodeListener not found javax.el.ELException: /testGroup/List.xhtml @26,88 binding="#{testController.nodeListener()}": Method nodeListener not found
2nd way:
<p:commandButton process="@this" >
<f:attribute name="key" value="#{node.getIdTestGroup()}" />
<f:actionListener binding="#{testController.nodeListener(event)}" />
<f:actionListener binding="#{testDeviceGroupController.prepareCreate(event)}" />
</p:commandButton>
Event is null on the nodeListener and prepareCreate methods
How to do it correct?
I see you facilitate the traditional approach of guess-how-it-works-using-bare-intuition-and-random-associations-then-act-surprised :-)
f:actionListener
only lets you add a whole object as an observer, not an arbitrary method. You can either use type
attribute to specify the class name (it will be instantiated by JSF) or binding
attribute to give an instance of the object that you created by yourself (not a method!). The object must implement javax.faces.event.ActionListener
.
Your second try (testDeviceGroupController.prepareCreate(event)
) is wrong on many levels, but the crux is that the methods are called not to handle your action, but to create the Actionlistener
instance.
You have a couple of options:
Like this:
public ActionListener createActionListener() {
return new ActionListener() {
@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
System.out.println("here I have both the event object, and access to the enclosing bean");
}
};
}
and use it like this:
<h:commandButton>
<f:actionListener binding="#{whateverBean.createActionListener()}"/>
</h:commandButton>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With