Introduction to JavaServer Faces, Part 3

This chapter has been leading us gently through the JavaServer Faces technology. More importantly, in it, Budi teaches ushow to write an online survey application to get a feel for how this great technology works. 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).


Creating the Page Navigation Example
JavaServer FacesThis section presents another sample application, which features an online survey application with two JSP pages. What makes it different from the previous two examples is that this application has multiple pages. Its main purpose is to demonstrate how to navigate from one JSP page to another.

Like the first two applications, this application is simple. The first page displays a form that takes two input boxes, where you enter your favorite singer and favorite band. After you submit the form, the second JSP page is displayed. The second JSP page displays the values you entered into the form in the first page. It also allows you to go back to the first page by clicking a button. A JavaBean called SurveyBean is used to store the user’s data.

This application requires a directory structure, deployment descriptor (the same as the ones for the previous examples, shown in Listing 1), a JavaBean, an application configuration file, and two JSP pages.

Creating the Directory Structure for the Page Navigation Example

You need to create a directory called JSPCh02c for this application. In Tomcat, you create the directory under the webapps directory. The directory structure for this application is shown in Figure 10. Notice that it has two JSP pages: page1.jsp and page2.jsp. Also, the SurveyBean class resides under the WEB-INF/classes directory. As usual, you must copy all required libraries to the WEB-INF/lib directory.

Creating the SurveyBean
Your application needs a JavaBean to store data. This bean is called SurveyBean, and it can be found in the SurveyBean.java file in the ch02c package. It is shown in Listing 7

JSF directory structure

Figure 10 The directory structure of the page navigation example 

Listing 7 The SurveyBean
[code]
package ch02c;
public class SurveyBean {
   String favSinger = null;
   String favBand = null;
   public void setFavSinger(String favSinger) {
      this.favSinger = favSinger;
   } 
   public String getFavSinger() {
      return favSinger;
   }
   public void setFavBand(String favBand) {
      this.favBand = favBand;
   } public String getFavBand() {
      return favBand; 
   }
}
[/code]

There is nothing special in the SurveyBean class. It has two properties, favSinger and favBand, and accessors and mutators (get and set methods) for the properties.

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

{mospagebreak title=Wrtiting the JSP Files for the Page Navigation Example}

This application has two JSP pages (page1.jsp and page2.jsp) as the user interface. The page1.jsp page is shown in Listing 8, and the page2.jsp page is shown in Listing 9.

Listing 8 The page1.jsp File
[code]
<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h” %>
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f” %>
<html>
<head>
<title>Online Survey</title>
</head>
<body>
<h2>Online Survey</h2>
<f:use_faces>
<h:form formName=”surveyForm” >
  <br/>Favorite Singer:
  <h:input_text id=”favSinger” valueRef=”surveyBean.favSinger”/>
  <br/>Favorite Band:
  <h:input_text id=”favBand” valueRef=”surveyBean.favBand”/>
  <br/>
  <h:command_button id=”submit” label=”Submit”
  commandName=”submit” />
</h:form>
</f:use_faces>
</body>
</html>
[/code]

Listing 9 The page2.jsp File
[code]
<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h” %>
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f” %>
<html>
<head>
<title>Thank You</title>
</head>
<body>
<h2>Thank you for your feedback.</h2>
<f:use_faces>
<h:form formName=”thankYouForm” >
Your favorite singer is
<h:output_text id=”favSinger” valueRef=”surveyBean.favSinger”/>
and your favorite band is
<h:output_text id=”favBand” valueRef=”surveyBean.favBand”/>
<br/>Now, click the button to go back to the previous page.
<br/>
<h:command_button id=”goBack” label=”Go Back”
commandName=”goBack” />
</h:form>
</f:use_faces>
</body>
</html>
[/code]

In both pages, you use the same UI components you used in the previous examples. In the page1.jsp file, you use a form containing two UIInput components for the user’s favorite singer and favorite band. Both UIInput components are bound to the SurveyBean‘s properties.

In the page2.jsp page, you use two UIOutput components to display the values entered by the user. The UIOutput components are also bound to the SurveyBean‘s properties, from which the components get their values. <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

{mospagebreak title=Creating the Application Configuration File for the Page Navigation Example}

In JSF, page navigation is controlled using one or more navigation rules in the application configuration file. Each rule specifies a page of origin and one or more possible targets. A page is represented by its tree identifier, and each possible target is represented by a navigation case. You need to specify a navigation rule for each page from which the user can navigate to another page. You also need to register the SurveyBean in the application configuration file. The application configuration file is shown in Listing 10.

Listing 10 The Application Configuration File for the Page Navigation Example
[code]
<?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>surveyBean</managed-bean-name>
  <managed-bean-class>ch02c.SurveyBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-tree-id>/page1.jsp</from-tree-id>
  <navigation-case>
  <to-tree-id>/page2.jsp</to-tree-id>
 </navigation-case>
</navigation-rule>
<navigation-rule>
 <from-tree-id>/page2.jsp</from-tree-id>
  <navigation-case>
   <to-tree-id>/page1.jsp</to-tree-id>
  </navigation-case>
 </navigation-rule>
</faces-config>
[/code]

A navigation rule is defined by the navigation-rule element in the application configuration file. For this application, each navigation-rule element needs to contain the from-tree-id element and navigation-case subelements. The from-tree-id 5 3 subelement is the tree identifier of the page of origin. The navigation-case subelement represents a possible target. A navigation-rule element can have zero or several navigation-case subelements. Each navigation-case element specifies the target page for a particular outcome of the from-tree-id processing. For this application, the navigation-case element simply contains a to-tree-id subelement that specifies the destination page.

To specify which page to navigate to from the page1.jsp page, you use the following navigation-rule element:

[code]
<navigation-rule>
 <from-tree-id>/page1.jsp</from-tree-id>
 <navigation-case>
  <to-tree-id>/page2.jsp</to-tree-id>
 </navigation-case>
</navigation-rule>
[/code]

To specify which page to navigate to from the page2.jsp page, you use the following navigation-rule element:

[code]
<navigation-rule>
 <from-tree-id>/page2.jsp</from-tree-id>
 <navigation-case>
  <to-tree-id>/page1.jsp</to-tree-id>
 </navigation-case>
</navigation-rule>
[/code] 

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

{mospagebreak title=Compiling and Running the Page Navigation Example}

To compile the application, change to the JSFCh02c/WEB-INF/classes directory. If you are using Windows, type the following command:

[code]
javac -classpath ../lib/jsf-api.jar;../lib/jsfri. jar;../../../../common/lib/servlet-api.jar ch02c/*.java
[/code]

Note that you need two library files in the lib directory and the servlet-api.jar file. In Tomcat 5, the servlet-api.jar file can be found in the common/lib directory of Tomcat’s home directory.

If you are using Linux/Unix, replace the semicolons that separate library files with colons:

[code]
javac -classpath ../lib/jsf-api.jar:../lib/jsfri. jar:../../../../common/lib/servlet-api.jar ch02c/*.java
[/code]

To run the application, direct your browser to the following URL:

[code]
http://localhost:8080/JSFCh02c/faces/page1.jsp
[/code]

JSF survey page
Figure 11 The survey page

You will see something similar to Figure 11.

Type something in both input boxes and click Submit. You will see a result similar to Figure 12 in your browser.

If you click the Go Back button in the second page (page2.jsp), you will go back to the first page. 

JSF response
Figure 12 The response page

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

{mospagebreak title=Introducing the JSF Application Programming Interface (API)}

The JSF implementation consists of various interfaces and classes that you can use to write event listeners, validators, custom components, and so on. These interfaces and classes are grouped into the following packages:

  • javax.faces
  • javax.faces.application
  • javax.faces.component
  • javax.faces.context
  • javax.faces.convert
  • javax.faces.el
  • javax.faces.event
  • javax.faces.lifecycle
  • javax.faces.render
  • javax.faces.tree
  • javax.faces.validator
  • javax.faces.webapp
Each package is briefly described in the following sections. Bear in mind that this is only a general introduction to the classes and interfaces in the packages. You will get more details about using these packages in the upcoming chapters, which discuss specific topics related to the classes and interfaces.

The javax.faces Package
The javax.faces package contains the FactoryFinder and FacesException classes. The FactoryFinder is responsible for discovering all factory objects specified in the JSF API. An example of a factory object is the javax.faces.context.FacesContextFactory object, which is responsible for creating FacesContext instances. You probably will never need to use the FactoryFinder class during your career as a JSF programmer.

On the other hand, the FacesException class, which is a subclass of java.lang. RuntimeException, encapsulates general JSF exceptions. You will often need to throw an instance of this class in your JSF classes.

The javax.faces.application Package
The javax.faces.application package contains important classes such as Action, NavigationHandler, and Application, as well as the Message interface. The Action class represents an object that performs a task and can return a String. The NavigationHandler class handles page navigation.

The Application class represents a per-Web-application singleton object to which JSF can register objects that provide functionality required by JSF. As an example, the default NavigationHandler object that manages page navigation is registered with the Application object.

The Message interface represents a validation or other message. For example, a validator will add a Message object to the FacesContext if the validation of the local value of a component fails. The javax.faces.application.MessageImpl class is the concrete implementation of the Message interface.

The javax.faces.component Package
The javax.faces.component package provides interfaces and classes that represent the standard UI components in JSF. The most notable member of this package is the UIComponent interface, which every UI component must implement. A convenience base class that implements this interface is UIComponentBase. Then there are other classes that represent UI components, such as UIForm, UICommand, UIGraphic, UIInput, UIOutput, UIPanel, and UISelectItem. These components are discussed in Chapters 4 and 5.

The javax.faces.context Package
The javax.faces.context package contains classes and interfaces that define per-request state information. The FacesContext class represents all of the per-request state information that is needed by the Lifecycle object during the request processing lifecycle. A FacesContext contains many other important objects, such as the component tree and the ExternalContext class.

The ExternalContext class, also a member of this package, is there so that JSF does not need to depend on any particular container. Thanks to this class, JSF can run in either a servlet or a portlet. In a servlet environment, you can get information contained in the ServletContext, ServletRequest, and ServletResponse objects through the instance of ExternalContext.

Other important members of this package are FacesContextFactory and MessageResources. FacesContextFactory is responsible for the creation of FacesContext objects. The MessageResources class represents a collection of message templates that help in constructing localized messages. Chapter 11 discusses how you can achieve internationalization and localization of your JSF applications.

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

{mospagebreak title= javax Packages}

The javax.faces.convert Package
Data conversion happens naturally during the request processing lifecycle. User input, always in String form, is converted into corresponding formats such as numeric and date formats. The javax.faces.convert package contains one interface (Converter) and one Exception class (ConverterException). You use the Converter interface if you want to write a custom converter, and the ConverterException is thrown for each failed data conversion. Converters are discussed in detail in Chapter 10.

The javax.faces.el Package
The javax.faces.el package contains classes that are useful in evaluating and processing expressions encountered in JSP pages in a JSF application. The el stands for expression language. One of the classes you may want to get familiar with is ValueBinding. This class is used for accessing the property represented by an action or value reference expression.

The javax.faces.event Package
The javax.faces.event package contains classes and interfaces useful for event processing. The FacesEvent class, for example, extends the java.util.EventObject class and is the base class for any event that can be emitted in a JSF application. This class has two subclasses that are also members of the javax.faces.event package: ActionEvent and ValueChangedEvent. These are the only events that can be fired in a JSF application. ActionEvent represents the activation of a UI component, such as the clicking of a button. ValueChangedEvent is trigged when the local value of a component has been changed.

The javax.faces.event package also provides three interfaces: FacesListener, ActionListener, and ValueChangedListener. The FacesListener interface is the base interface that must be implemented by any event listener in JSF. The FacesListener interface is also the superinterface of the ActionListener and ValueChangedListener. You implement the ActionListener interface to write a listener that can receive ActionEvents. Implement the ValueChangedListener interface if you want to receive ValueChangedEvents.

Another member of the javax.faces.event package is the PhaseId class. This class encapsulates type-safe enumeration of possible values used to indicate after which phase in the request processing lifecycle an event listener will be executed. The FacesListener interface has the getPhaseId method, which returns a PhaseId. The request processing lifecycle calls the getPhaseId method of all registered listeners to find out when to execute the listener. If a listener’s getPhaseId returns PhaseId.APPLY_REQUEST_VALUES, for example, the listener will be executed in the process event step after the Apply Request Values phase.

The AbortProcessingException class in the javax.faces.event package may be thrown by event listeners to terminate the processing of the current event.

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

{mospagebreak title=More javax Packages}

The javax.faces.lifecycle Package
The javax.faces.lifecycle package contains classes and an interface that have something to do with the request processing lifecycle. There are two classes in this package: Lifecycle and LifecycleFactory. As explained earlier in this chapter, the Lifecycle object is the object that processes a JSF request. The Lifecycle object is represented by the javax.faces.lifecycle.Lifecycle class. The LifecycleFactory class is responsible for creating a Lifecycle instance.

The only interface in this package is ViewHandler. This interface defines a mechanism by which the Render Response phase of the request processing lifecycle can support different response-generation technologies. 

The javax.faces.render Package
The javax.faces.render package contains three classes: Renderer, RenderKit, and RenderKitFactory. A Renderer is responsible for converting the internal representation of a UI component into something that is understood by the JSF client. When JSF is used in a Web application, a Renderer sends HTML tags to the browser as the representation of a UI component’s state. A Renderer normally can render one UI component, so there are always many Renderer objects for the various standard UI components used in a JSF application.

A RenderKit represents a collection of Renderer instances. The JSF implementation comes with the default RenderKit.The RenderKitFactory is a factory object that returns a RenderKit instance.

Renderers are discussed in Chapter 12.

The javax.faces.tree Package
The javax.faces.tree package contains the Tree and TreeFactory classes. The Tree class represents a component tree. The TreeFactory is responsible for creating Tree instances.

The javax.faces.validator Package
The javax.faces.validator package contains the Validator interface that you implement to write your custom validator. It also contains a number of standard validator classes: DoubleRangeValidator, LengthValidator, LongRangeValidator, RequiredValidator, and StringRangeValidator. Validators and input validation are discussed in Chapter 9.

The javax.faces.webapp Package
The members of the javax.faces.webapp package are classes that are required for integrating JSF into Web applications. The most important class in this package is the FacesServlet class. An instance of this class is required by each JSF application to manage the request processing lifecycle. Its job description includes obtaining a FacesContext instance and passing it to the Lifecycle object for processing.

Another member, UIComponentTag, is a base class that you need to extend to create a tag handler that handles a JSP custom action that is related to a UI component. You can see this class used in Chapters 12 and 13.

Summary
This chapter provided a gentle introduction to JSF. Its main objective was to give you the experience of running your first JSF applications. You got an idea of how JSF works, without going into all of the details yet. You will learn more about JSF processing in later chapters.

In this chapter, you learned the most important characteristic of JSF applications that makes them different from other servlet/JSP applications: their event-driven nature. You also built three simple JSF applications that illustrate the component tree, event processing, input validation, and page navigation. The last section of this chapter offered a general overview of the JSF API, which you will be using to develop JSF components in the next chapters.

Buy this book now!Remember: This is part three of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). If you’ve enjoyed what you’ve seen here, click on the “Buy it now!” banner to pick up a copy today!
Buy this book!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye