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

Introduction to JavaServer Faces, Part 1

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

Intro to JavaServer FacesA JSF application looks like any other servlet/JSP application. It has a deployment descriptor, JSP pages, custom tag libraries, static resources, and so on. The user interface of a JSF application is one or many JSP pages that host JSF components such as forms, input boxes, and buttons. These components are represented by JSF custom tags and can hold data. A component can be nested inside another component, and it is possible to draw a tree of components. In fact, a JSP page in a JSF application is represented by a component tree. Just as in normal servlet/JSP applications, you use JavaBeans to store the data the user entered.

In this chapter, you will get an overview of how JSF works and the steps to writing a simple JSF application. Then you will put this knowledge to use by working through three examples. Finally, you will be introduced to the JSF Application Programming Interface (API).

How Does JSF Work?

A JSF application works by processing events triggered by the JSF components on the pages. These events are caused by user actions. For example, when the user clicks a button,the button triggers an event. You, the JSF programmer, decide what the JSF application will do when a particular event is fired. You do this by writing event listeners. In other words, a JSF application is event-driven. Figure 1 illustrates the processing of a JSF application.

When an event occurs (say, when the user clicks a button), the event notification is sent via HTTP to the server. On the server is a special servlet called the FacesServlet. Each JSF application in the Web container has its own FacesServlet.

In the background, three things happen for each JSF request, as illustrated in Figure 2.

For JSF requests to be processed, they must be directed to a servlet called FacesServlet. The redirection is accomplished by using the following servlet and servlet-mapping tags in the deployment descriptor:


<!-- Faces Servlet -->
<servlet>
  
<servlet-name>Faces Servlet</servlet-name>
  
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
  
<servlet-name>Faces Servlet</servlet-name>
  
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<!-- Faces Servlet --><SERVLET><SERVLET-NAME></SERVLET-NAME><SERVLET-CLASS></SERVLET-CLASS><LOAD-ON-STARTUP></LOAD-ON-STARTUP></SERVLET><!-- Faces Servlet Mapping --><SERVLET-MAPPING><SERVLET-NAME></SERVLET-NAME><URL-PATTERN></URL-PATTERN></SERVLET-MAPPING>

This means that the URL of every request must contain the /faces/ pattern, as specified in the url-pattern element under the servlet-mapping element. 

JSF Applications

Figure 1 JSF applications are event-driven


NOTE You can specify a context parameter saveStateInClient with a value of true to force JSF to save state in the client as opposed to saving it in the server. If you choose to do so, you must add the following context-param element before the servlet element in your deployment descriptor.


<context-param>
  
<param-name>saveStateInClient</param-name>
  
<param-value>false</param-value>
</context-param>

FacesServlet creates an object called FacesContext, which contains information necessary for request processing. To be more precise, FacesContext contains the ServletContext, ServletRequest, and ServletResponse objects that are passed to the service method of FacesServlet by the Web container. During processing, FacesContext is the object that is modified. 

JSF

Figure 2 How JSF works in a nutshell 

Next is the processing. The processor is an object called Lifecycle. The FacesServlet servlet hands over control to the Lifecycle object. The Lifecycle object processes the FacesContext object in six phases, which we will look at next.

NOTE The series of actions necessary for JSF request processing by the Lifecycle object is referred to as the request processing lifecycle. You will encounter this term throughout this book.

JSF also allows you to configure a JSF application via an application configuration file. After discussing the Lifecycle object phases, we will discuss how to use this configuration file to register JavaBeans.

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
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: