Home arrow Java & J2EE arrow Introduction to JavaServer Faces Part 2

Introduction to JavaServer Faces Part 2

We continue with part two of Chapter 2 of Introduction JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004). This section deals with the ActionListener and Component Tree, as well as validation and navigation for your JSP pages.  This chapter prepares you for the next chapters by introducing the JSF Application Programming Interface (API) and the Application Configuration file.

  1. Introduction to JavaServer Faces Part 2
  2. The Action Listener
  3. Continuing With the navigateComponent Tree
  4. Console Message
  5. And the Message
  6. Authoring the JSP Page for the Validator Example
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 23
March 15, 2004

print this article



Intro to JavaServer FacesWriting the ActionListener for the Listener and Component Tree Example

The ActionListener for the command button is the most interesting part of this JSF application. It demonstrates how an event causes a listener to be executed. The listener simply prints messages to the console. However, it shows important information such as the hierarchy of the component tree of the JSP page from which the event was fired and the component that triggered the event. The ActionListener is shown in Listing 5.


Listing 5 The ActionListener for the Command Button (MyActionListener.java)

package ch02a
import java
import javax
import javax
import javax
import javax
import javax
import javax

public class MyActionListener implements ActionListener 

public PhaseId getPhaseId() { 
.out.println("getPhaseId called");  

void processAction(ActionEvent event) {   
.out.println("processAction called");  
// the component that triggered the action event 
   UIComponent component = event.getComponent();
    "The id of the component that fired the action event: "   
    + component.getComponentId());   
   // the action command   
   String actionCommand = event.getActionCommand() 
   System.out.println("Action command: " + actionCommand); 

FacesContext facesContext FacesContext.getCurrentInstance();
      Tree tree 
      UIComponent root 
.out.println("----------- Component Tree ------------");

private void navigateComponentTree
      UIComponent component
int level){ 
// indent 
      for (int i=0; i<level; i++) 
        System.out.print(" "); 
      // print component id 
      Iterator children = component.getChildren(); 
      // navigate children 
      while (children.hasNext()) { 
         UIComponent child = (UIComponent) children.next();
         navigateComponentTree(child, level + 1); 

Buy this book now!Remember: This is part one of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more helpful chapters from McGraw-Hill/Osborne.
Buy this book!

>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: