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).
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 3The Application Configuration File(faces-config.xml) for the Listener and Component Tree Example
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!