Event Based Programming Model With JSF Tag Library


JSF provides an event-based programming model similar to Swing or AWT, Events are used as a mechanism for propagating the state change notification between the source object and one or more target object listeners.
There are three types of events supported by JSF:
  • Value-change events
  • Action events
  • Data model events
When components such as buttons and hyperlinks are activated, action events trigger components such as UIInput, UISelectOne, UISelectMany, and UISelectBoolean components generate the value change events when their values are changed. The UIData component triggers the data model event when a new row of a UIData component is selected.
To enable a component to react to action or value change events, the application can use any one of the following techniques.
Implement an event listener class to process the event and register the event on the component. To implement an event listener, a class has to implement either an ActionListener or ValueChangeListener interface. The class should also override the processAction() method for ActionListener interface or valueChangeListener() method for the ValueChangeListener interface.
The following source code demonstrates the use of both listener interfaces,
  1. //Implementing ActionListener    
  2. Class Employee extends Object implements ActionListener {  
  3.     Private Map empMap = null;  
  4.     Employee()  
  5.     empMap = new Hashmap();  
  6.     empMap.put(“1001”, ”AshishB”);  
  7.     empMap.put(“1002”, ”Ashu”);  
  8.     empMap.put(“1003”, ”Atul”);  
  9.     empMap.put(“1004”, ”Manisha”);  
  10. }  
  11. public void processAction(ActionEvent evt) {  
  12.     throws AbortProcessingException {  
  13.         String name = evt.getComponent().getId();  
  14.         FacesContext contextObj = FacesContext.getCurrentInstance();  
  15.     }  
  16. }  
  17. //Implementing ValueChangeListener    
  18. class Employee extends Object implements ValueChangeListener {  
  19.     public void processValueChange(ValueChangeEvent evt) throws AbortProcessingException {  
  20.             if (name != evt.getnewValue() {  
  21.                     FacesContext contextObj = FaceContext.getCurrentInstance();  
  22.                 }  
  23.             }  
Next, the component should be registered to emit action and value change events The following source code demonstrates this by nesting the event listener tag within the component tag. The value in the type attribute of event listener tag specifies the class name of the event listener implementation.
  1. //registering component with ActionListener    
  3. <h:commandLink id=”number” action=”employee”>  
  4.     <f:actionListener type=”Employee” />  
  5. </h:commandLink>    
  6. //registering component with ValueChangeListener    
  8. <h:inputText id=”name” size=”50” value=”#(employee.name)” required=”true”>  
  9.     <f:valueChangeListener type=”Employee” />  
  10. </h:inputText>   
Implement event processing in a backing bean method and refer to this method in a method binding expression of the component.
The backing bean should implement a method that accepts an ActionEvent object or ValueChangeEvent object and return type set to void. This method should be referred using the actionListener or valueChangeListener attribute of the component tag, as shown in the below source code:
  1. //writing a method to handle action event    
  2. public void displayName(ActionEvent evt) {  
  3.         String name = evt.getComponent().getId();  
  4.         FaceContext contextObj = FaceContext.getCurrentInstance();  
  5.     }  
  6.     //referenced a method that handles action event    
  7.     < h: commandLink id = ”number” action = ”employee” actionListener = ”#(EmployeeBean.displayName)” >  
  8.     //method to handle value change event    
  9.     public void select(ValueChangeEvent vcevt) {  
  10.         //code to handle a value selection event of a list or combobox    
  11.     }  
  12.     //referencing a method that handles action event    
  13.     < h: commandLink id = ”number” action = ”employee” actionListener = ”#(EmployeeBean.select)” >  

Validation Model

JSF also supports a validation model, which validates the component’s local data before it updates the business object’s data. JSF provides a set of standard classes and associated tags to developers for validating the component’s data. The three standard validators provided by JSF are Length Validator, Long Range Validator, and Double Range Validator.
When a component is holding a data type that is not supported by standard JSF validators custom validators are created.

Navigation Model

In a web application, moving from screen to screen based on user action is a common requirement JSF supports a Navigation model, which is the form of declaring a set of rules that defines the next view for the user based on his actions. These rules are specified using XML elements in the application’s configuration resource file, often named as faces-config.xml
The following are the benefits of keeping all navigation rules in one configuration file.
The single faces-config.xml file supports various tools that offer visual design for navigation. For example, in the JDeveloper tool, one can define navigation rules just by linking and dragging pages in a visual environment.
Easy change incorporation
Keeping rules in one file makes it simple to incorporate a change. For example, suppose index.jsp page is to be renamed to “catalog.jsp” then rather than changing all old references to this page, one has to simply modify the navigation rule defined in the application’s configuration resource file.

JSF Tag Library

The JSF supports tags for presentation and other core utilities. A tag library is a collection of such tags. Using these tags saves valuable time in development. JSF supports two types of libraries: Core and HTML.

JSF Core Tag Library

The tags specified in JSF’s core tag library perform the core utilities such as validation and conversion.
Following are a few tags described below,
  • actionListener - It adds an instance of actionListener to a UIComponent specified in the parent tag.
  • attribute - It adds an attribute to a component specified in the parent tag
  • convertor - It generates an instance of the class with a specified ID and associates it with the UIComponent defined in the parent tag.
  • convertDateTime - It adds an instance of DateTimeConverter to a UIComponent specified in the parent tag
  • convertNumber - It adds an instance of NumberConvertor to a UIComponent specified in the parent tag
  • validator - It adds a validator to a component
  • facet - It adds a facet to a component

JSF HTML Tag Library

JSF application uses JSP pages to represent views. JSF provides special useful tags to enhance these views. Each tag gives rise to an associated component. Some of JSF HTML tags are described below, 
  • column - It creates a column in a data table.
  • commandButton - It creates a submit button.
  • commandLink - It creates a link
  • dataTable - It creates a table control
  • form - It creates a form
  • inputText - It creates a text input control(single line)
  • panelGroup - It is used to group other components where the specification requires one child element
  • selectOneMenu - It creates a single select menu
  • selectOneRadio - It creates a set of radio buttons.
For example,
Create an HTML page (index.html) that starts a JSF application that accepts and verifies the user name and password on a click of the Submit button.
  1. <html>  
  2.     <head>  
  3.         <title> A  Simple Java Server Faces Application </title>  
  4.     </head>  
  5.     <body>  
  6.         <font size=’4’> Welcome to a simple JavaServer Faces Application    
  7. </font>  
  8.         <p>  
  9.             <a href=’faces/index.jsp’> Click here to start the application</a>  
  10.         </body>  
  11.     </html>   
In this code, when a user clicks on the link, a JSP page is displayed . The JSP page displays a login form that accepts the user name and password from the user. It also validates the user name using the LengthValidator class.
  1. <html>  
  2.     <head>  
  3.         <title> A Simple JavaServer Faces Log-in Application</title>  
  4.     </head>  
  5.     <body><%@ taglib uri=””/WEB-INF/jsf-html.tld” prefix=”faces” %>  
  6.         <font size=’4’>Please Enter your User Name and Password</font>  
  7.         <faces:usefaces>  
  8.             <faces:form id=”simpleForm” formName=simpleForm”>  
  9.                 <table>  
  10.                     <tr>  
  11.                         <td>Email Id:</td>  
  12.                         <td>  
  13.                             <faces:textentry_Username id=”name”>  
  14.                                 <faces:validator className=’javax.faces.validator.Lengthvalidator’ />  
  15.                                 <faces:attributename=’javax.faces.validator.LengthValidator.Minimum’ value=’3’ />  
  16.                             </faces:textentry_input>  
  17.                         </td>  
  18.                         <td>  
  19.                             <faces:validation_message componentId=’Username’ />  
  20.                         </td>  
  21.                     </tr>  
  22.                     <tr>  
  23.                         <td> Password :</td>  
  24.                         <td>  
  25.                             <faces:textentry_password=”password” />  
  26.                         </td>  
  27.                     </tr>  
  28.                 </table>  
  29.                 <p>  
  30.                     <faces:command_button id=”submit” commandName=Log In” />  
  31.                 </faces:form>  
  32.             </faces:usefaces>  
  33.         </body>  
  34.     </html>   
On a click of the the button, the JSP page is redirected to another page which displays a message "You have successfully logged In", as shown in code below,
  1. <html>  
  2.     <head>  
  3.         <title>    
  4. A simple JavaServer Faces Application</title>  
  5.     </head>  
  6.     <body>    
  7. You have Successfully Logged In!!    
  8. </body>  
  9. </html>   
The following file are also required by the JSF application,
  • faces-config.xml : This is the master configuration file required by all JSF applications. It will contain reference of all included components that form the JSF application.
  • web.xml : This file contains specific settings for JSF which includes Servlet mapping.
Jar files required in the WEB-INF/lib are,
  • WEB-INF/lib/commons-beanutils.jar
  • WEB-INF/lib/commons-collections.jar
  • WEB-INF/lib/commons-disgester.jar
  • WEB-INF/lib/commons-logging-api.jar
  • WEB-INF/lib/jsf-api.jar
  • WEB-INF/lib/standard.jar


JSF supports a Navigational model, which is in the form of declaring a set of rules that define the next view for users based on his actions. JSF also supports a validation model, which validates the component’s local data before it updates the business object’s data. JSF provides useful special tags to enhance the views which are implemented as JSP.

Similar Articles