Action listeners in JSF

By: Charles Emailed: 1729 times Printed: 2329 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


JSF Home | All JSF Tutorials | Latest JSF Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(15)


1. View Comment

Thanks for the Information. Its very useful

View Tutorial          By: Husain at 2009-03-04 13:30:38
2. View Comment

thanks man!u r lifesaver!!!!!!!

View Tutorial          By: sneha at 2009-03-17 23:56:40
3. View Comment

just looking for it, useful!

View Tutorial          By: ymfeng at 2009-03-20 00:09:56
4. View Comment

Yeah, great article

View Tutorial          By: smeagol_s at 2009-03-29 02:45:01
5. View Comment

Good piece of information explained wonderfully. !!!

View Tutorial          By: Juzar at 2009-07-23 00:24:40
6. View Comment

Nice explanation. And a good example to back it up

View Tutorial          By: fazil at 2009-07-29 00:32:03
7. View Comment

Hi, I have the following element:

<h:commandButton id="exportPDFButton" actionListener="#{exportPDF.exportPDF}">

This button goes to the server and calls the exportPDF function. This exportPDF function takes a few seconds to be done, so I want to desable the button during that time. How can I achieve that? is there a way to know when the response is send? please, any suggestion will be welcome


View Tutorial          By: gardek at 2009-09-02 14:40:37
8. View Comment

That's a very nice and simple article,
Thank You.


View Tutorial          By: Mohsen at 2009-10-11 03:07:18
9. View Comment

Thanks dude its really helpful. it clears my doubt.

View Tutorial          By: Sriram at 2009-12-28 05:54:37
10. View Comment

exellent, thks!!!

View Tutorial          By: Eduardo at 2010-01-29 19:20:15
11. View Comment

hi,

i have a problem. I have the tipical common JSP page that is include by the rest JSP. This page is a filter page and that is include in the other pages with "jsp:include" tag. The filter page have buttons to search elements with the next date or the previous date, but must call diferent beans (each principal page have its own SearchBean) Each principal page, that includes the filter page, use "jsp:param" tag to indicate the actionListener that must be invoke. But when in the filter page i put "#{param.forwardActionListener}" in the "h:commandButton", i get an error when click.

Exemple:

test.jsp code

<jsp:include page="dateFilterPage.jsp" flush="true">
<jsp:param name="parentView" value="testSubview"/>
<jsp:param name="backAction" value="TestSimpleBean.action"/>
<jsp:param name="forwardAction" value="TestSimpleBean.action"/>
<jsp:param name="backActionListener" value="TestSimpleBean.backDate"/>
<jsp:param name="forwardActionListener" value="TestSimpleBean.forwardDate"/>
</jsp:include>

dateFilterPage.jsp code

<h:commandButton styleClass="esriToolDefault" id="FechaAdelanteBtn" value="forward date" action="#{param.forwardAction}" actionListener="#{param.forwardActionListener}"/>

The error is :

javax.faces.el.MethodNotFoundException: backActionListener: com.sun.faces.context.RequestParameterMap.backActionListener(javax.faces.event.ActionEvent)

How i can force to filter page to invoke the correct bean getter by param?

Thanks


View Tutorial          By: Felipe at 2010-02-18 05:03:51
12. View Comment

Good info.. keep providing..

View Tutorial          By: Shoaeb uddin khaleefah at 2010-07-27 05:28:19
13. View Comment

wow...great MAN

View Tutorial          By: Er Sounderarajan at 2013-03-13 07:29:58
14. View Comment

great, thnak you very much !!!!

View Tutorial          By: geek at 2014-05-11 00:10:32
15. View Comment

Good Example

View Tutorial          By: Naresh at 2015-02-03 07:41:40

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Charles
arraycopy example in Java
Action listeners in JSF
Automatically Including Preludes and Codas in JSP
Using realloc() Function in C
Handling multiple buttons in HTML Form in Struts
Fopen and Getc implementation program in C
Pointers to Structures example program in C
Pointers vs. Multi-dimensional Arrays in C
getch and ungetch in C
Bitwise Operators in C
Variables and Arithmetic Expressions in C
Differences between JavaServer Faces technology and Struts
assert() example program in C++
'double buffering' Sample program in J2ME
Converting Default Function Arguments in C++ to Java

More Tutorials in JSF
Struts Vs JSF (A comparison of Struts against JSF)
faces-config.xml to DirectTraffic in the JSF Application
<convertNumber> and <convertDateTime> in JSF
Action listeners in JSF
Calling Multiple Listeners in JSF
Using Javascript in JSF
How to open a new browser window from my JSF page?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
JSF - TreeNode.setID gets IllegalArgument Exception
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle

More Latest News
Most Viewed Articles (in JSF )
How to open a new browser window from my JSF page?
Calling Multiple Listeners in JSF
Struts Vs JSF (A comparison of Struts against JSF)
Installing JSF
Using Javascript in JSF
Action listeners in JSF
Java Bean Scopes in JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
<convertNumber> and <convertDateTime> in JSF
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle
Most Emailed Articles (in JSF)
Action listeners in JSF
Install and Deploy JBoss Application Server
What is JSF (JavaServer Faces)?
Differences between JavaServer Faces technology and Struts
JSF Basics
The Relationship Between JSF and Other Java EE Technologies
JSF Life Cycle
Installing JSF
Java Bean Scopes in JSF
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Accessing Context Data in Beans using JSF
Using Javascript in JSF
How to open a new browser window from my JSF page?
Servlet error : java.lang.IndexOutOfBoundsException (JSF RI 1.1_01: IndexOutOfBoundsException)
JSF - TreeNode.setID gets IllegalArgument Exception