table of contents

JSF Tutorial

An Introduction to JavaServer Faces
Plus Apache MyFaces Extensions

This tutorial is derived from Marty Hall's world-renowned live JSF training course. Note that the course (and this tutorial) now covers Apache MyFaces 1.1.5. The course is usually taught on-site at customer locations, but servlet, JSP, Ajax, Jakarta Struts, and JSF training courses at public venues are periodically scheduled for people with too few developers for an onsite course. For descriptions of the various other courses that are available, please see the training course page. To inquire about a customized training course at your location, please contact Marty at hall@coreservlets.com. Courses on servlets, JSP, Ajax, Struts, Hibernate, Java 5, Java 6, and Ruby/Rails also available.

If you find these free tutorials helpful, we would appreciate it if you would link to us.

Source Code

Source code for all examples in this tutorial can be downloaded from the JSF sample code repository.

PowerPoint Files for University Faculty

The PDF files in this tutorial contain the complete text of the original PowerPoint files, so if your goal is learning Ajax, just stick with this tutorial. However, as a service to instructors teaching full-semester courses at accredited universities, coreservlets.com will release the original PowerPoint files for free. Please see the instructor materials page for details.

Servlet and JSP Review

This section provides a quick review of the server-side programming topics that will be used throughout this tutorial. There are no Ajax-specific topics in this first section. For more details on server-side programming, see the servlet and JSP tutorials.

Tutorial section:

Topics:

  • What servlets are all about
  • Servlet basics
  • Creating and deploying projects in Eclipse. Also see the Tomcat and Eclipse tutorial.
  • Creating forms and reading form data
  • JSP scripting
  • Using XML syntax for JSP pages
  • JSP file inclusion
  • MVC

Source code:


Section 1: Introducing JSF

Tutorial section:

Topics:

  • Understanding JSF
    • Different views of JSF
    • Comparing JSF to standard servlet/JSP technology (Pros/Cons)
    • Comparing JSF to Apache Struts (Pros/Cons)
  • Setting Up JSF
    • Downloading and configuring JSF
      • Apache MyFaces
      • Sun Reference Implementation
    • Testing JSF
    • Setting up JSF applications
    • Accessing JSF documentation
      • Generic
      • Specific to Apache MyFaces

Source Code:

  • jsf-blank-myfaces.zip
    Blank Web app to use as a starting point for JSF applications. Apache MyFaces version, bundled as an Eclipse project Has all necessary JAR files in WEB-INF/lib, all required settings in WEB-INF/web.xml, and a blank WEB-INF/faces-config.xml file.

Section 2: Controlling Page Navigation

Tutorial section:

Topics:

  • JSF flow of control
  • The basic steps in using JSF
  • Static navigation
    • One result mapping
  • Dynamic navigation
    • Multiple result mappings

Source code:


Section 3: Handling Request Parameters with Managed Beans

Tutorial section:

Topics:

  • Using beans to represent request parameters
  • Declaring beans in faces-config.xml
  • Outputting bean properties
    • Standard JSF approach
    • JSP 2.0 expression language

Source code:


Section 4: Using the JSF Expression Language

Tutorial section:

Topics:

  • Motivating use of the expression language
    • Comparing to the JSP 2.0 EL
  • Accessing bean properties
    • Direct
    • Nested
  • Submitting bean properties
    • Expressions in output values
    • Expressions in submission values
    • Expressions for action controllers
  • Accessing collection elements
  • Using implicit objects and operators

Source code:


Section 5: Using Properties Files (Resource Bundles)

View PDF of Using Properties Files section.

Source Code:

Topics:

  • Loading properties files
  • Simple messages
  • Parameterized messages
  • Internationalized messages

Section 6: Handling Events

View PDF of Handling Events section.

Source Code:

Topics:

  • Comparing action controllers to event listeners
  • Action listeners
  • Value change listeners
  • Using JavaScript to submit form
    • Browser incompatibilities
  • Combining action listeners and action controllers in the same GUI element

Section 7: Building Input Forms with the h: Library

View PDF of Building Input Forms with the h: Library section.

Source Code:

Topics:

  • The most common HTML elements
  • Shared attributes
  • Implied attributes
  • Elements that can invoke action controllers/listeners
  • Elements that can invoke value change listeners
  • Elements that display lists of items

Section 8: Validating User Input and Redisplaying Incomplete Forms

View PDF of Validating User Input and Redisplaying Incomplete Forms section.

Source Code:

Topics:

  • Manual validation
  • Implicit automatic validation
  • Explicit validation
  • Defining your own validation methods
  • Creating custom validators

Section 9: Accessing Databases with JDBC

View PDF of Accessing Databases with JDBC section.

Source Code:

Topics:

  • Overview of JDBC technology
  • JDBC design strategies
  • Using Apache Derby (Java DB)
  • Seven basic steps in using JDBC
  • Using JDBC from desktop Java apps
  • Using JDBC from Web apps
  • Prepared statements (parameterized commands)
  • Meta data
  • Transaction control

Section 10: Displaying Data Tables

View PDF of Introducing JSF section.

Source Code:

The database-related examples assume you have set up the Northwind database as described in the JDBC notes.

Topics:

  • Motivation
  • Basic syntax
  • Defining table headings
  • Formatting tables with style sheets
  • Displaying database tables

Section 11: Using the Apache MyFaces Components (Tomahawk)

View PDF of Using the Apache MyFaces Components section.

Source Code:

  • myfaces-components.zip
    Web application used for the examples in this section. This is the complete version, including all JAR files and web.xml settings. Specific to Apache MyFaces. If you want to start with a blank Web app and add in MyFaces extended components (Tomahawk), be sure to start with the full (not minimal) version of the Apache MyFaces version of jsf-blank.

Topics:

  • Popular Apache component libraries
    • Tomahawk
    • Tobago
    • Trinidad
  • Getting the Tomahawk components
  • Configuring MyFaces to use Tomahawk
  • Sample components
    • Date input
    • Tabbed panes
    • Popups
    • Data lists
    • Tables with column flow
  • Sample validators
    • Regular expressions
    • Email addresses
    • Credit cards
    • Equality
  • Overview of other components

Section 11B: Using the Ajax4jsf Library

View PDF of Ajax4jsf section.

Source Code:

Topics:

  • Ajax motivation
  • Installation
  • Main Ajax4jsf Elements
    • a4j:commandButton
    • a4j:commandLink
    • a4j:poll
    • a4j:support
  • Limitations on use of h:outputText with Ajax4jsf
  • Other Ajax Toolkits
Note: for more details on Ajax, please see the Ajax and GWT (Google Web Toolkit) tutorial.

Section 12: Developing Custom Tag Libraries: The Basics

View PDF of Developing Custom Tag Libraries: The Basics section.

Source Code:

Download tags code

Topics:

  • Java-based tags
    • Components of a tag library
    • Basic tags
    • Tags that use attributes
    • Tags that use body content
    • Tags that optionally use body content
  • JSP-based tags (tag files)
    • Components of a tag library
    • Basic tags
    • Tags that use attributes
    • Tags that use body content

Section 13: Developing Custom Tag Libraries: Advanced Topics

View PDF of Developing Custom Tag Libraries: Advanced Topics section.

Source Code:

Download tags code

Topics:

  • Manipulating the tag body
  • Tags with dynamic attribute values
  • Tags with complex objects for attributes
  • Looping tags
  • Nested tags
  • Using SAX and TagLibraryValidator to validate tag library syntax

Section 14: Developing Custom Components

View PDF of Developing Custom Components section.

Source Code:

Topics:

  • Simple output-only components
  • Components that accept attributes
  • Components that accept input

Section 15: Page Templating with Facelets

View PDF of Page Templating with Facelets section.

Source Code:

Topics:

  • Simple output-only components
  • Components that accept attributes
  • Components that accept input

Section 16: Using (Struts) Tiles with JSF

View PDF of Using (Struts) Tiles with JSF section.

Source Code:

  • e-boats.zip
    First Web application used for the examples in this section. This is the complete version, including all JAR files and web.xml settings. The deployed JAR files are for Apache MyFaces 1.1, but the basic code can also run in the Sun Reference Implementation if you include struts.jar.
  • e-boats2.zip
    Second Web application used for the examples in this section. This is the complete version, including all JAR files and web.xml settings. Specific to Apache MyFaces.

Topics:

  • Tiles motivations
  • Setting up JSF (any implementation) for simple Tiles
  • Tiles basics
    • Sketch out desired layout
    • Make template file that represents layout
    • Create JSP pages that define layout pieces
    • Create JSP pages that populate layout
  • Setting up MyFaces for Tiles definitions
  • Using Tiles definitions file

Section 17: Using the JSP Standard Tag Library (JSTL) with JSF

View PDF of Using the JSP Standard Tag Library (JSTL) with JSF section.

Source Code:

Topics:

  • Obtaining JSTL documentation and code
  • The JSTL Expression Language
  • Looping Tags
    • Looping a certain number of times
    • Looping over data structures
  • Conditional Evaluation Tags
    • Single choice
    • Multiple choices
  • Database Access Tags
  • Other Tags

More Information about JavaServer Faces and Related Technologies



More Information

Java

Servlets & JSP
JSF
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts