By: Charles Printer Friendly Format
Action listeners are provided by JSF to make it easier to handle action events. JSF already provides
some event handling. For example, clicking a button on the search page (an event) causes the search() method of the FlightSearch class to be called. However, that event handler is limited in what it can do, because it has no access to the state of the user interface. Action listeners do receive information about the user interface, and thus can be used for more robust event handling.
Action listeners are attached to the two JSF command elements: command buttons and command links. Action events are handled in a manner very similar to value change events. You attach a listener to a command element with the actionListener attribute. For example, the searchForm.jsp page has a command button. You can attach an action listener to it by using this syntax:
When the button is clicked, the JSF implementation calls the action listener during the Invoke Application phase. The action listener method then has a chance to perform any processing related to the command element selected by the user. You can perform any processing you need to inside the method. The method can have any name, must be public, return void, and accept an ActionEvent as its only parameter.
After the action listener method is called, the method bound by the action attribute will be called, and the JSF implementation will determine where to navigate next. Because the action listener method is called before the action method, the action listener method is able to modify the response that the action method returns. For example, the Flight Search application could have some links that direct the user to searches for lodging, transportation, or other services. Without action listeners, you would need to write a different action method for each link, because the action method cannot have parameters and thus does not know what part of the user interface was clicked. As the number of links increased, so would the number of action methods. However, the action listener does have access to the user interface through the ActionEvent, and could determine which link was clicked. It could store that information as a bean property that the action method could access. With this technique, a single action listener method and a single action method could handle any number of links.
Most Viewed Articles (in JSF )
Latest Articles (in JSF)
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Subscribe to Tutorials
5. Good piece of information explained wonderfully. !
View Tutorial By: Juzar at 2009-07-23 00:24:40
6. Nice explanation. And a good example to back it up
View Tutorial By: fazil at 2009-07-29 00:32:03
9. Thanks dude its really helpful. it clears my doubt
View Tutorial By: Sriram at 2009-12-28 05:54:37
13. what is the difference b/w actionlistener and acti
View Tutorial By: mahendra at 2011-09-09 06:07:11