I want to use multiple action listener to set state of two backing beans before further processing
1st way:
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>
The binding
attribute value needs to point to an object implementing ActionListener
interface, not a method.
From the documentation of f:actionListener
's bindig
attribute :
Value binding expression that evaluates to an object that implements javax.faces.event.ActionListener.
A similar problem was discussed here.