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.

Available free for university faculty: The original PowerPoint versions of the tutorials are available free for university faculty in certain cases. Please see instructor usage rules for details.


Section 1: Introducing JSF

View PDF of Introducing JSF section.

Source Code:

  • jsf-blank-myfaces-app.zip
    Blank Web app to use as a starting point for JSF applications. Apache MyFaces version, bundled as a normal Web application (as opposed to an Eclipse project as below). 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.
  • jsf-blank-myfaces-eclipse.zip
    Same as above, but bundled as an Eclipse project.
  • jsf-blank-myfaces-with-extensions-app.zip
    Blank Web app to use as a starting point for JSF applications. Apache MyFaces version, bundled as a normal Web application (as opposed to an Eclipse project as below). In addition to the standard JSF features, this Web app includes JAR files and settings for the standard Apache MyFaces extensions: Tomahawk (MyFaces custom components and validators), Tiles, file upload, and more.
  • jsf-blank-myfaces-with-extensions-eclipse.zip
    Same as above, but bundled as an Eclipse project.
  • jsf-blank-Sun-RI.zip
    Blank Web app to use as a starting point for JSF applications. Sun Reference Implementation (RI) version. 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. Except for the sections on Facelets, Tiles, and the MyFaces components, the examples in this tutorial run equally well on top of either the MyFaces or Sun RI base.

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

Section 2: Controlling Page Navigation

View PDF of Controlling Page Navigation section.

Source Code:

Topics:

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

Section 3: Handling Request Parameters with Managed Beans

View PDF of Handling Request Parameters with Managed Beans section.

Source Code:

Topics:

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

Section 4: Using the JSF Expression Language

View PDF of Using the JSF Expression Language section.

Source Code:

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

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:

  • NorthwindTest.java
    Standalone application to test the Northwind database. Only works if you have set up the Northwind database as described in the JDBC notes.
  • NorthwindServlet2.java
    Servlet to test the Northwind database. Uses the driver and connection explicitly. Only works if you have set up the Northwind database as described in the JDBC notes.
  • server.xml
    Tomcat configuration file to register the Northwind database as a data source. Only works if you have set up the Northwind database as described in the JDBC notes. Specific to Tomcat version!
  • NorthwindServlet3.java
    Servlet to test the Northwind database. Uses JNDI to get the connection. Only works if you have registered the data source and set up the Northwind database as described in the JDBC notes.

Topics:

  • Overview of JDBC technology
  • JDBC drivers
  • Seven basic steps in using JDBC
  • Using JNDI with JDBC
  • Retrieving data from a ResultSet
  • Using prepared and callable statements
  • Handling SQL exceptions
  • Submitting multiple statements as a transaction

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