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).
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:
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:
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.
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.
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!