views:

74

answers:

2

What is the difference between action and actionListener. When sould I use action and/or actionListener? My question is related JSF 2.0.

+1  A: 

ActionListener gets fired first, with an option to modify the response, before Action gets called and determines the location of the next page.

If you have multiple buttons on the same page which should go to the same place but do slightly different things, you can use the same Action for each button, but use a different ActionListener to handle slightly different functionality.

Here is a link that describes the relationship:

http://www.java-samples.com/showtutorial.php?tutorialid=605

Erick Robertson
+1  A: 

Use actionListener if you want have a hook before a business action get executed, e.g. to log it and/or have access to the component which invoked the action (which is available by ActionEvent argument). Use action if you want to execute a business action and handle navigation. The action method can return a String which will be used as navigation case outcome (the target view).

Another subtle difference is that the actionListener swallows any exceptions and won't propagate it up (i.e. you won't see an error/exception page, JSF will log them however). This is done so because the business logic, exception handling and navigation handling job is up to the action method.

BalusC