The Java EE 6 Tutorial, Volume I

Creating a Simple JavaServer Faces Application

JavaServer Faces technology provides an easy and user-friendly process for creating web applications.

Developing a simple JavaServer Faces application typically requires the following tasks:

In this section, the above tasks are described through the process of creating a simple JavaServer Faces Facelets application.

The example is a Hello application which includes a backing bean and a web page. When accessed by a client, the web page prints out a Hello World message. The example application is located in tut-install/examples/web/hello directory.

The tasks involved in developing this application can be examined by looking at the application in detail.

Developing Backing Beans

As mentioned earlier in this chapter, a backing bean (a type of managed bean) is a JavaBean that is managed by JavaServer Faces. Components in a page are associated with backing beans which provide application logic. The example backing bean, helloWorld.java, contains the following code:

package Hello;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class Hello{
final String world = "Hello World!";

public String getWorld() 
{ return world; }

} 

The example backing bean sets the value of the world variable with the string Hello World!. The @ManagedBean annotation registers the backing bean as a resource with the JavaServer Faces implementation. For more information on managed beans and annotations, see Developing With JavaServerTM Faces Technology.

Creating the Web Page

In a typical Facelets application, web pages are created in XHTML. The example web page, beanhello.xhtml, is a simple XHTML page. It contains the following content:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
       <title>JavaServer Faces Hello World Application</title>
    </head>
    <body> 
    		#{hello.world}
    </body>
</html>

A Facelets XHTML web page can also contain several other elements which are covered later in this tutorial.

The web page connects to the backing bean through the Unified Expression Language (EL) value expression #{hello.world}, which retrieves the value of the world property from the backing bean Hello. Note the use of hello to reference the backing bean Hello. If no name is specified in the @ManagedBean annotation, the backing bean is always accessed with the first letter of the class name in lowercase.

For more information on using EL expressions, see Chapter 6, Unified Expression Language. For more information about Facelets technology, see Introduction to Facelets. For more information about JavaServer Faces programming model and building web pages using JavaServer Faces technology, see Chapter 7, Using JavaServerTM Faces Technology in Web Pages.

Mapping the Faces Servlet Instance

The final task requires mapping the Faces Servlet which is done through the web deployment descriptor (web.xml). A typical mapping of Faces Servlet is as follows:

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

The above file segment represents part of a typical JavaServer Faces web deployment descriptor. The web deployment descriptor can also contain other content relevant to a JavaServer Faces application configuration but that information is not covered here.

Mapping the Faces Servlet is automatically done for you when using a Java EE 6server such as Sun GlassFishTM Enterprise Server v3.

The Lifecycle of the helloWorld Application

Every web application has a lifecycle. Common tasks such as handling incoming requests, decoding parameters, modifying and saving state, and rendering web pages to the browser are all performed during a web application lifecycle. Some web application frameworks hide the details of the lifecycle from you while others require you to manage them manually.

By default, JavaServer Faces handles most of the lifecycle actions for you automatically. But it does expose the different parts of the request lifecycle, so that you can modify or perform different actions if your application requirements warrant it.

It is not necessary for the beginning user to understand the lifecycle of a JavaServer Faces application, but the information can be useful for creating more complex applications.

The lifecycle of a JavaServer Faces application starts and ends with the following activity: The client makes a request for the web page, and the server responds with the page. The lifecycle consists of two main phases: execute and render.

During the execute phase, several actions can take place: The application view is built or restored, the request parameter values are applied, conversions and validations are performed for component values, backing beans are updated with component values, and application logic is invoked. For a first (initial) request, only the view is built. For subsequent (postback) requests, some or all of the other actions can take place.

In the render phase, the requested view is rendered as response to the client. Rendering, typically is the process of generating output such as HTML or XHTML that can be read by the client (usually a browser).

The following short description of the example JavaServer Faces application passing through its lifecycle summarizes the activity that takes place behind the scenes.

The helloWorld example application goes through the following stages when it is deployed on the Enterprise Server:

  1. When the helloWorld application is built and deployed on the Enterprise Server, the application is at an uninitiated state.

  2. When a client makes a first (initial) request for the beanhello.xhtml web page, the helloWorld Facelets application is compiled.

  3. The compiled Facelets application is executed and a new component tree (UIViewRoot) is constructed for the helloWorld application and is placed in the Faces Context.

  4. The component tree is populated with the component and the backing bean property associated with it (represented by the EL expression hello.world).

  5. A new view is built based on the component tree.

  6. The view is rendered to the requesting client as a response.

  7. The component tree is destroyed automatically.

  8. On subsequent (postback) requests, the component tree is rebuilt and the saved state is applied.

For more detailed information on the JavaServer Faces lifecycle, see the JavaServer Faces Specification, Version 2.0 document.

ProcedureRunning the Application in NetBeans IDE

To build, package, deploy, and run the JavaServer Faces helloWorld example using NetBeans IDE, follow these steps:

  1. In NetBeans IDE, select File->Open Project.

  2. In the Open Project dialog box, navigate to the example directory:


    tut-install/examples/web
  3. Select the helloWorld folder.

  4. Select the Open as Main Project check box.

  5. Click Open Project.

  6. In the Projects tab, right-click the helloWorld project and select Run.

    This step compiles, assembles, and deploys the application, then brings up a web browser window displaying the following URL:


    http://localhost:8080/helloWorld

Example 4–1 Example Output of the helloWorld Application


Hello World!