Java
  Home arrow Java arrow Page 8 - Introduction to JavaServer Faces, Part...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introduction to JavaServer Faces, Part 1
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 52
    2004-03-08

    Table of Contents:
  • Introduction to JavaServer Faces, Part 1
  • Understanding the Request Processing Lifecycle Phases
  • Using an Application Configuration File
  • Writing a JSF Application
  • Writing JavaBeans and Event Listeners
  • Creating the Event Listener and Component Tree Example
  • Creating the Directory Structure
  • Writing the Object Model for the Listener and Component Tree Example
  • Defining Taglib Directives

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introduction to JavaServer Faces, Part 1 - Writing the Object Model for the Listener and Component Tree Example


    (Page 8 of 9 )

    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
    public 
    class NumberBean 
      int firstNumber 
    0
      int secondNumber 
    0
      
    public NumberBean () {  
        System
    .out.println("Creating NumberBean"); 
      

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

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

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

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

      
    public int getResult() {
        System
    .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"
      
    "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
     
    <
    faces-config>
      
    <managed-bean>
        
    <managed-bean-name>numberBean</managed-bean-name>
        
    <managed-bean-class>ch02a.NumberBean</managed-bean-class>
        
    <managed-bean-scope>session</managed-bean-scope>
      
    </managed-bean>
    </faces-config>

    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" %>
    <html>
    <head>
    <title>Add 2 numbers</title>
    </head>
    <body>
    <f:use_faces>
    <h:form formName="addForm" >
      
    <br/>First Number:
      
    <h:input_number id="firstNumber"
        valueRef
    ="numberBean.firstNumber"/>
      
    <br/>Second Number:
      
    <h:input_number id="secondNumber"
        valueRef
    ="numberBean.secondNumber"/>
      
    <br/>Result:
      
    <h:output_number id="output" valueRef="NumberBean.result"/>
      
    <br/>
      
    <h:command_button id="submitButton" label="Add"
        commandName
    ="submit" >
        
    <f:action_listener type="ch02a.MyActionListener" />
      
    </h:command_button>
    </h:form>
    </f:use_faces>
    </body>
    </html>



    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 Articles
    More By McGraw-Hill/Osborne


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway