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

Using an Application Configuration File - 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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

You can easily configure a JSF application via an application configuration file. In this file, you can register JavaBeans used in the application, define the program-control flow by specifying page-navigation rules, register custom components, and perform other configuration tasks.

The application configuration file is an XML file and can be declared in several places. The easiest way to use this file is to put it in the WEB-INF directory and call it faces-config.xml.

The root element of an application configuration file is faces-config. Here is the skeleton of an application configuration file:


<?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>
</faces-config>

There are a number of aspects of a JSF application that can be configured in the application configuration file. Here, we will focus on how to register JavaBeans (as used in the examples later in this chapter). The application configuration file is explained in detail in Chapter 15.

In a JSP page, you can use the jsp:useBean action to tell the JSP container that you are using the JavaBean class specified in the class attribute of the jsp:useBean action, such as the following:


<jsp:useBean id="numberBean" class="ch02.NumberBean"
scope
="session" />



This tells the Web container to load the JavaBean class and create an instance of it when the JSP page is called. The jsp:useBean action needs to be declared in only one page, and it will be usable in all the JSP pages in the same application. A JSF application allows you to do this instead of registering a JavaBean in the application configuration file.

However, there is a drawback when using <jsp:useBean>. If a page other than the one containing the jsp:useBean action is called before the page that does use the element is called, the Web container will throw an exception. This is because the other page is trying to use a JavaBean that has not been created. If you register the JavaBean in the application configuration file, you will not have this problem.

For each JavaBean you want to register in the application configuration file, you use the managed-bean tag inside the faces-config element. Inside the managed-bean element, you have the following subelements:

  • The managed-bean-name tag defines a name to refer to the JavaBean from a JSP page.
  • The managed-bean-class element specifies the JavaBean class.
  • The managed-bean-scope element defines the scope of the JavaBean.

Here is an example of a managed-bean element:


<managed-bean>
  
<managed-bean-name>myBean</managed-bean-name>
  
<managed-bean-class>myPackage.MyBean</managed-bean-class>
  
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>



The managed-bean element in this example is of type myPackage.MyBean and can be referred to as MyBean from any JSP page in the JSF application. The scope of the bean is session, meaning that an instance of this bean is created at the beginning of a user session. The managed-bean element will be explained further in Chapter 3.

Later in this chapter, in the “Creating the Page Navigation Example” section, you will see how to use an application configuration file to define page-navigation rules in a JSF application with many pages.

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 (JSF)," 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
   

JAVA & J2EE ARTICLES

- 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: