Home arrow Java & J2EE arrow Page 8 - Introduction to JavaServer Faces, Part 1

Writing the Object Model for the Listener and Component Tree Example - Java

This chapter gently introduces the JavaServer Faces technology. More importantly, it teaches you how to write your first JSF application to get a feel for how this great technology works.  In addition to the sample chapters, this chapter prepares you for the next chapters by introducing the JSF Application Programming Interface (API) and the Application Configuration file. This excerpt comes from chapter two of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

  1. Introduction to JavaServer Faces, Part 1
  2. Understanding the Request Processing Lifecycle Phases
  3. Using an Application Configuration File
  4. Writing a JSF Application
  5. Writing JavaBeans and Event Listeners
  6. Creating the Event Listener and Component Tree Example
  7. Creating the Directory Structure
  8. Writing the Object Model for the Listener and Component Tree Example
  9. Defining Taglib Directives
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 74
March 08, 2004

print this article



For this application, you need a JavaBean to store the two numbers to add and the result of the addition. Listing 2 presents the JavaBean called NumberBean.

Listing 2 The NumberBean JavaBean

package ch02a
class NumberBean 
  int firstNumber 
  int secondNumber 
public NumberBean () {  
.out.println("Creating NumberBean"); 

public void setFirstNumber(int number) { 
.out.println("setFirstNumber: " number); 

public int getFirstNumber() { 
.out.println("getFirstNumber: " firstNumber); 
return firstNumber

public void setSecondNumber(int number) { 
.out.println("setSecondNumber: " number);

public int getSecondNumber() {
.out.println("getSecondNumber: " secondNumber); 
return secondNumber;

public int getResult() {
.out.println("getResult " + (firstNumber secondNumber));
return firstNumber secondNumber;

Writing the Application Configuration File

As explained earlier in the chapter, the best way to make the JavaBean available to the JSF application is to register it in the application configuration file. Listing 3 shows the application configuration file (faces-config.xml) needed by the application. 

Listing 3 The Application Configuration File (faces-config.xml) for the Listener and Component Tree Example

<?xml version="1.0"? >
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"

Authoring the JSP Page for the Listener and Component Tree Example

For the user interface, you need a JSP page called adder.jsp, which is shown in Listing 4.

Listing 4 The adder.jsp Page

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %><%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %><f:use_faces><h:form formName="addForm">
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<title>Add 2 numbers</title>
<h:form formName="addForm" >
<br/>First Number:
<h:input_number id="firstNumber"
<br/>Second Number:
<h:input_number id="secondNumber"
<h:output_number id="output" valueRef="NumberBean.result"/>
<h:command_button id="submitButton" label="Add"
="submit" >
<f:action_listener type="ch02a.MyActionListener" />

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 part 2 of "Introduction to JavaServer Faces," where we learn about JSP, JavaBeans, and Model 2. 
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: