Dec 14, 2011

JSF Interview Questions and Answers: event handling

Q. What are the different types of JSF events?
A. JSF is an event driven framework.

  • Action Events: bound to UI Command objects like a Command Button or a Hyper-link. Whenever a user presses a Command Button or clicks a hyperlink these Events get generated.
  • Value Change Events: bound to UI Components like Text Field, Check-Box, List and Radio Buttons. The Value Change Event is fired as soon as the value that is displayed in the view is modified.
  • Phase Events: As you saw earlier in the JSF overview blog, the request processing life-cycle in JSF includes six phases and any JSF implementation will fire Phase events during the start and end of each phase. If we want to capture the Phase Events, then can define a Phase Listener. These are handy for debugging as well.

Q. How are events handled in JSF? What is the difference between these event handling mechanisms?
A. Action handlers and event listeners provide an event driven mechanism. Every time a user does something like clicking a button, selecting an item from a drop down, or submitting a form, an event occurs. Event notification is then sent via HTTP to the server and handled by the FacesServlet. Events can invoke custom business logic or initiate page navigation.

JSF provides two types of methods for handling events; listeners and action handlers, both of these may be defined within a managed bean. A listener takes an FacesEvent as a parameter and a void return type, while an action handler takes no parameters and returns a String.

Example 1: An event handler

<!-- login.xhtml-->
<h:inputText id="useName" />
<h:inputSecret id="password" />
<h:commandButton action="#{login.submit}">

<!-- faces-config.xml-->

//Login managed bean
public String submit(  ) {
      //do some action

       return "success"  //navigate to /home.jsf     

Example 2: An event handler with an event listener

Action listeners are provided by JSF to make it easier to handle action events. An advantage of using a listener is that the FacesEvent object provides additional information, such as the form element that initiated the event. An action handler in contrast has no knowledge of the source of the event, but based upon its return value, can initiate page navigation. The example below shows using both event handlers and event listeners.

<h:commandButton value="Search" actionListener="#{orders.confirm}" action="#{}" />

In the above example, 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.

public void confirm(ActionEvent event) {
       int calculatedAge = calculateAgeFromDOB();
       if (event.getComponent().getId().equals("confirm")) {
            //perform some action
    else if(event.getComponent().getId().equals("validate")){
         //perform some other action

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.

public String search( ) {
    //some action logic
 //navigation logic
 return "success";

Since the action listener method is called before the action handler method, the action listener method can modify the response that the action method returns.

An action listener can also be implemented as shown below.

<h:commandButton id="submitOrderSearch" value="Search" action="#{}" >
     <f:actionListener type="com.MyAppActionListenerImpl" />

The listener class can be implemented as shown below:

package com;


public class MyAppActionListenerImpl implements ActionListener {

    public void processAction(ActionEvent aev) {


A ValueChangeEvent is useful whenever you want to be notified when there is a change in the value of a component, such as text modification in a text field or a check box selection. Most JSF components support the valueChangeListener attribute.

<h:inputText  id="orderStatus"  valueChangeListener="#{orders.onStatusChange}" />

The managed bean method:

public void onStatusChange(ValueChangeEvent vce) {

This can also be implemented as shown below.

<h:inputText  id="orderStatus" >
    <f:valueChangeListener type="com.MyAppValueChangeActionListenerImpl" />

The managed bean method:

package com;


public class MyAppValueChangeActionListenerImpl implements ValueChangeListener {

    public void processValueChange(ValueChangeEvent vce) {


More JSF Interview Questions and Answers



Anonymous Anonymous said...

Excellent! very clear and detailed answers, Thank you very much!

4:48 AM, January 31, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home