The order of Action and ActionListener in JSF

Hi, I always bump into this question when doing consultancies on clients about actions and action listeners and the order they are called.

Let’s assume that we have a p:commandButton which has an action, an actionlistener, some setPropertyActionListeners and a f:actionListener. The code will be something like:



<p:commandButton actionListener="#{actionListenerBean.actionListener}" action="#{actionListenerBean.action}" value="click me">
        <f:setPropertyActionListener target="#{actionListenerBean.property}" value="some value"/>
        <f:actionListener type="tr.com.prime.ActionListenerBean" />
        <f:setPropertyActionListener target="#{actionListenerBean.property2}" value="some other value"/>
</p:commandButton>


and our Java code is as follows ( a bit crappy for demo ) :


@ViewScoped
@ManagedBean(name = "actionListenerBean")
public class ActionListenerBean implements ActionListener {

	public void actionListener(ActionEvent actionEvent){
		System.out.println("actionListener of the button is called");
	}
	public String action(){
		System.out.println("action of the button is called");
		return null;
	}

	public void setProperty(String property) {
		System.out.println("f:setPropertyActionListener for property is called");
	}

	public void setProperty2(String property2) {
		System.out.println("f:setPropertyActionListener for property2 is called");
	}
	
	@Override
	public void processAction(ActionEvent ae) throws AbortProcessingException {
		System.out.println("f:actionlistener is called");
	}
}

when we click on the button, the output is as follows:

actionListener of the button is called
f:setPropertyActionListener for property is called
f:actionlistener is called
f:setPropertyActionListener for property2 is called
action of the button is called

As you can see first the actionListener of the button is called, then the actionListeners that are inside the button are called in the order they are added to the button and finally action of the button is called.

If you are trying to set some data with f:setPropertyActionListener but cannot see it in your actionListener method of your button, the order of the actionListeners is the reason.

Advertisements

5 thoughts on “The order of Action and ActionListener in JSF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s