Action listeners in JSF
By: Charles in JSF Tutorials on 2007-10-06
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:
<h:commandButton value="Search"
actionListener="#{flight.confirm}"
action="#{flight.search}"/>
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Struts Vs JSF (A comparison of Struts against JSF)
Calling Multiple Listeners in JSF
<convertNumber> and <convertDateTime> in JSF
faces-config.xml to DirectTraffic in the JSF Application
JSF - TreeNode.setID gets IllegalArgument Exception
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
How to open a new browser window from my JSF page?
Accessing Context Data in Beans using JSF
Install and Deploy JBoss Application Server
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Comments