You are previewing JavaServer Faces 2.0.
O'Reilly logo
JavaServer Faces 2.0

Book Description

The Definitive Guide to JavaServer Faces 2.0

Fully revised and updated for all of the changes in JavaServer Faces (JSF) 2.0, this comprehensive volume covers every aspect of the official standard Web development architecture for JavaEE. Inside this authoritative resource, the co-spec lead for JSF at Sun Microsystems shows you how to create dynamic, cross-browser Web applications that deliver a world-class user experience while preserving a high level of code quality and maintainability.

JavaServer Faces 2.0: The Complete Reference features an integrated sample application to use as a model for your own JSF applications, with code available online. The book explains all JSF features, including the request processing lifecycle, managed beans, page navigation, component development, Ajax, validation, internationalization, and security. Expert Group Insights throughout the book offer insider information on the design of JSF.

• Set up a development environment and build a JSF application

• Understand the JSF request processing lifecycle

• Use the Facelets View Declaration Language, managed beans, and the JSF expression language (EL)

• Define page flow with the JSF Navigation Model, including the new "Implicit Navigation" feature

• Work with the user interface component model and the JSF event model, including support for bookmarkable pages and the POST, REDIRECT, GET pattern

• Use the new JSR-303 Bean Validation standard for model data validation

• Build Ajax-enabled custom UI components

• Extend JSF with custom non-UI components

• Manage security, accessibility, internationalization, and localization

• Learn how to work with JSF and Portlets from the JSF Team Leader at Liferay, the leading Java Portal vendor

Ed Burns is a senior staff engineer at Sun Microsystems and is the co-specification lead for JavaServer Faces. He is the co-author of JavaServer Faces: The Complete Reference and author of Secrets of the Rock Star Programmers.

Chris Schalk is a developer advocate and works to promote Google's APIs and technologies. He is currently engaging the international Web development community with the new Google App Engine and OpenSocial APIs.

Neil Griffin is committer and JSF Team Lead for Liferay Portal and the co-founder of The PortletFaces Project.

Ready-to-use code at

Table of Contents

  1. Cover Page
  2. JavaServer Faces 2.0
  3. Copyright Page
  4. Dedication
  5. Contents
  6. Part I The JavaServer Faces Framework
    1. 1 Introduction to JavaServer Faces
      1. What Is JavaServer Faces?
      2. The History of JavaServer Faces
        1. The Common Gateway Interface
        2. The Servlet API
        3. JavaServer Pages
        4. Apache Struts
        5. The Spring Framework and Spring MVC
        6. The Birth of JavaServer Faces
      3. The JavaServer Faces Design Goals
      4. JSF Application Architecture
        1. The JSF Request Processing Lifecycle
        2. The JSF Navigation Model
    2. 2 Building a Simple JavaServer Faces Application
      1. Application Overview
        1. The JSFReg Application Files
        2. The JSF Software Stack
        3. Assembling the JSFReg Application
        4. The Configuration File
        5. The Facelets Pages
      2. Building and Running the Application
        1. Deploying and Running the Application
      3. Reviewing the Key Portions of the Application
    3. 3 The JavaServer Faces Request Processing Lifecycle
      1. A High-Level Overview of the JSF Request Processing Lifecycle
        1. What Exactly Does the Request Processing Lifecycle Do?
        2. How Does It Differ from Other Web Technologies?
        3. Automatic Server-Side View Management and Synchronization
        4. The Request Processing Lifecycle Phases
      2. Observing the Request Processing Lifecycle in Action
      3. Advanced Topics Related to the Request Processing Lifecycle
        1. Using the immediate Attribute
        2. Phase Listeners
        3. Exception Handler
      4. Lifecycle Concepts to Remember
    4. 4 The Facelets View Declaration Language
      1. The Power of Templating in Faces
      2. Similarities and Differences Between JSP and Facelets
      3. Templating with Facelets
      4. Guide to Facelets Templating Tags
        1. ui:composition
        2. ui::decorate
        3. ui::define
        4. ui::insert
        5. ui::include
        6. ui::param
      5. Guide to Nontemplating Facelets Tags
        1. ui::component
        2. ui::fragment
        3. ui::remove
        4. ui::debug
    5. 5 Managed Beans and the JSF Expression Language
      1. What Are Managed Beans?
        1. A Simple Managed Bean Example
        2. Initializing Managed Bean Properties
        3. Declaring Lists and Maps Directly as Managed Beans
        4. Managed Bean Interdependence
        5. Setting Managed Properties Using EL
      2. Controlling Managed Bean Life Spans
      3. The JSF Expression Language
        1. Important Expression Languages Changes Between JSF 1.1 and JSF 1.2
        2. Unified EL Concepts
        3. Value Expressions
        4. Expression Operators
        5. Method Expressions
      4. Web Application Development Details on Managed Beans
        1. How to Access Managed Beans Programmatically
        2. Using Managed Beans as Backing Beans for JSF Pages
    6. 6 The Navigation Model
      1. Using Implicit Navigation
      2. Overview of the JSF Navigation System
        1. Recalling MVC—The Controller
        2. The NavigationHandler—Behin d the Scenes
        3. A Note on Faces Action Methods
      3. Building Navigation Rules
        1. A Static Navigation Example
        2. A Dynamic Navigation Example
      4. More Sophisticated Navigation Examples
        1. Using Wildcards
        2. Using Conditional Navigation
        3. Using Redirects
        4. XML Configuration for View Parameters
        5. Using JSF Components on a Servlet Error Page
    7. 7 The User Interface Component Model
      1. What Are UI Components?
        1. The Rise of Component-Based Web Development
        2. The Goal of JavaServer Faces UI Components
      2. Introducing the JSF UI Component Architecture
        1. The UI Component Tree (View)
        2. The UI Component and Its Associated “Moving Parts”
      3. Component Resources
      4. UI Components and Facelets
        1. Accessing UI Components Programmatically
        2. Helpful Advice for Binding UI Components in JSF Views
    8. 8 Converting and Validating Data
      1. Some Validation and Conversion Examples
      2. Conversion and Validation Under the Covers
      3. The Faces Converter System
        1. DateTimeConverter
        2. NumberConverter
        3. Associating a Converter with a UIComponent Instance
        4. The Lifetime of a Converter
        5. Custom Converters
      4. The Faces Validation System
        1. LongRangeValidator
        2. DoubleRangeValidator
        3. LengthValidator
        4. The “required” Facility and the RequiredValidator
        5. RegExValidator
        6. BeanValidator
      5. How to Associate a Validator with a UIComponent Instance
        1. Using Markup to Associate a Validator with a UIComponent Instance
        2. Using Markup and the validator Attribute to Associate a Validator with a UIComponent Instance
        3. Programmatically Associating a Validator with a UIComponent Instance
        4. The Lifetime of a Validator
        5. Custom Validators
      6. Using Bean Validation from JSF
        1. What Is Bean Validation?
        2. Differences Between JSF Validation and Bean Validation
        3. Details on Using Bean Validation from JSF
        4. Validating JSF Managed Bean Properties with Bean Validation
      7. Tie It All Together: Messages in a View
        1. FacesMessage-Related Methods on FacesContext
        2. The UIViewRoot and Its Locale Property
        3. When and How FacesMessage Instances Are Created and Added to the FacesContext
        4. How FacesMessages Are Rendered
        5. Messages and Bean Validation
    9. 9 The JSF Event Model
      1. A High-Level Overview of the JSF Event Model
        1. Some Event Examples
      2. How JSF Events Work
        1. The Faces Event Listener Interfaces and Event Classes
        2. When Are Faces Events Processed?
        3. Application Events
        4. The Anatomy of a Value Change Event
      3. Writing Custom Action and Value Change Listeners
      4. More Faces Event Examples
        1. Using a Value Change Event to Auto-Fill Fields
        2. Extending the Value Change Example
      5. Working with Phase Events and Listeners
        1. Using a PhaseListener to Observe the Faces Lifecycle in Action
      6. Working with SystemEvents and Listeners
        1. How SystemEvents Are Published
        2. How to Subscribe to SystemEvents
      7. General Rules for Creating Custom Application Events and Listeners.
        1. Creating Custom SystemEvent and ComponentSystemEvent Classes
  7. Part II Extending JavaServer Faces
    1. 10 Applying JSF: Introducing the Virtual Trainer Application
      1. A Quick Tour of the Virtual Trainer Application
        1. Registering and Logging In to the Virtual Trainer Application
        2. Creating a New Training Event Workout Plan
        3. Choosing Your Trainer
        4. Actions Available Only to Trainers
      2. The Virtual Trainer Application Requirements
      3. The Virtual Trainer Application Architecture
      4. Facelet Pages
        1. Pages in the Web Application Root
        2. Pages in the /user Directory
        3. Pages in the /trainer Directory
        4. Pages in the /resources Directory
      5. Backing Beans
      6. Persistence and Transaction Architecture
      7. Navigation Concerns
      8. Creating a Simple Authentication System
      9. Revisiting JSFReg: Building the Registration System
      10. Building the Core Pages of the Virtual Trainer Application
        1. Creating the allEvents.xhtml Page
        2. The Trainer Version of editTrainingEvent.xhtml
        3. Creating the viewTrainees.xhtml and viewTrainingSessionsForUser.xhtml Pages
      11. The UserRegistry and EventRegistry
        1. Accessing and Initializing the UserRegistry Instance
        2. Reading and Writing User Instances
        3. Reading and Writing TrainingSession Instances
      12. Accessing and Initializing the EventRegistry Instance
      13. Reading and Writing Event Instances
      14. JPA and the Entity Classes in Virtual Trainer
        1. JPA XML Configuration File: persistence.xml
        2. A Custom ExceptionHandler Example
      15. Internationalizing the Virtual Trainer Application
        1. Localization
      16. Final Comments on Virtual Trainer
    2. 11 Building Custom UI Components
      1. Deciding When to Build a Custom UI Component
      2. What Are UI Components?
      3. A Simple JSF UI Component
      4. The Code Behind the Markup of This Simple JSF UI Component
        1. Introduction to Resource Libraries
      5. Adding Behavior to a Composite Component
        1. A First Look into the Details of Composite Components
        2. The #{cc} Implicit Object
      6. The Parts of a UI Component
      7. A Noncomposite JSF Custom UIComponent
        1. Building the HtmlHelloWorld Example
        2. A HelloWorld UI Component That Accepts Form Input
        3. A JSF Stock Quote Component
      8. State Management with Custom Components
        1. Writing to a StateHelper
        2. Reading from a StateHelper
        3. Removing Values from a StateHelper
      9. Extracting Rendering Code into a Renderer
        1. Creating a Custom Facelet Tag Library TagHandler
        2. Using a RenderKit to Support Multiple Client Device Types
      10. Advanced Composite Component Development
        1. Creating a Backing Class for <vt:loginPanel>
        2. Composite Component Feature Review
      11. Packaging JSF Components into a Self-Contained Jar
      12. Packaging a Composite Component into a Jar
        1. Packaging a Composite Component Facelet File
        2. Declaring a Custom Namespace URL for a Composite Component Library
        3. Packaging Backing Classes for a Composite Component
      13. Packaging a Noncomposite Component into a Jar
        1. How the JSF Runtime Loads faces-config.xml Files
        2. How the JSP Runtime Loads TLD Files
    3. 12 JSF and Ajax
      1. Ajax Overview
      2. A Simple Ajax Example from JSFReg
        1. Ajax JSFReg Behind the Scenes
      3. The <f:ajax> Tag and Its Attributes
      4. The Standard JavaScript Resource
        1. Advanced Topics on JSF and Ajax
    4. 13 Building Non-UI Custom Components
      1. Non-UI Custom Components and Decoration in JSF
        1. Wrapper Classes
      2. Non-View Custom Components Explained
      3. PhaseListener
        1. Interface to Implement
        2. Registering a PhaseListener
      4. Converter and Validator
        1. Interfaces to Implement
        2. Registering a Converter or Validator
      5. ViewHandler
        1. Abstract Class to Extend
        2. Registering a ViewHandler
      6. VariableResolver and PropertyResolver
        1. Abstract Classes to Extend
        2. Registering a VariableResolver or PropertyResolver
      7. ELResolver (JSF 1.2)
        1. The ELResolver Chain of Responsibility
        2. Abstract Class to Extend
        3. Registering an ELResolver
      8. NavigationHandler
        1. Abstract Class to Extend
        2. Registering a NavigationHandler or ConfigurableNavigationHandler
      9. ActionListener
        1. Interface to Implement
        2. Registering an ActionListener
      10. StateManager
        1. Abstract Class to Extend in JSF 1.1 and 1.0
        2. Abstract Class to Extend in JS F 1.2
        3. Registering a StateManager
      11. RenderKit
      12. Factories in JSF
        1. Registering a Factory
        2. RenderKitFactory
        3. ExceptionHandlerFactory
        4. FacesContextFactory
        5. ExternalContextFactory
        6. LifecycleFactory
        7. PartialViewContextFactory
        8. ViewDeclarationLanguageFactory
        9. VisitContextFactory
        10. Application Factory
      13. Additional Non-UIComponent Topics
        1. Telling the JSF Runtime Where You Are in the Software Development Lifecycle: ProjectStage
        2. Custom Scopes
    5. 14 Securing JavaServer Faces Applications
      1. Aspects and Implementation of Web Application Security
      2. Container-Managed Security
        1. Container-Managed Authentication
        2. Basic Authentication and the Concept of a “Realm”
        3. Form-Based Authentication
        4. Certificate Authentication
        5. Container-Managed Authorization and the Concept of Roles
        6. Container-Managed Data Security
        7. A Small Security Improvement in the Virtual Trainer
      3. Application-Managed Security with JavaServer Faces
        1. Leveraging JAAS from a JSF Application
        2. To Learn More about Security
  8. Part III JavaServer Faces Tools and Libraries
    1. 15 Configuring JavaServer Faces Applications
      1. Understanding XML Schemas
      2. Understanding How Configuration Files Are Processed
      3. Ordering of faces-config.xml Files
      4. How the JSF Runtime Scans Classes for Configuration Annotations
      5. The Faces Configuration Elements
        1. The absolute-ordering Element
        2. The action-listener Element
        3. The after Element
        4. The application Element
        5. The application-factory Element
        6. The attribute Element
        7. The attribute-class Element
        8. The attribute-name Element
        9. The base-name Element
        10. The before Element
        11. The behavior Element
        12. The behavior-class Element
        13. The behavior-id Element
        14. The client-behavior-renderer Element
        15. The client-behavior-renderer-class Element
        16. The client-behavior-renderer-type Element
        17. The component Element
        18. The component-class Element
        19. The component-family Element
        20. The component-type Element
        21. The converter Element
        22. The converter-class Element
        23. The converter-for-class Element
        24. The converter-id Element
        25. The default-locale Element
        26. The default-render-kit-id Element
        27. The default-validators Element
        28. The default-value Element
        29. The el-resolver Element
        30. The exception-handler-factory Element
        31. The external-context-factory Element
        32. The faces-config Element
        33. The faces-context-factory Element
        34. The facet Element
        35. The facet-name Element
        36. The factory Element
        37. The from-action Element
        38. The from-outcome Element
        39. The from-view-id Element
        40. The if Element
        41. The key Element
        42. The key-class Element
        43. The lifecycle Element
        44. The lifecycle-factory Element
        45. The list-entries Element
        46. The locale-config Element
        47. The managed-bean Element
        48. The managed-bean-class Element
        49. The managed-bean-name Element
        50. The managed-bean-scope Element
        51. The managed-property Element
        52. The map-entries Element
        53. The map-entry Element
        54. The message-bundle Element
        55. The name Element
        56. The navigation-case Element
        57. The navigation-handler Element
        58. The navigation-rule Element
        59. The null-value Element
        60. The ordering Element
        61. The others Element
        62. The partial-view-context-factory Element
        63. The phase-listener Element
        64. The property Element
        65. The property-class Element
        66. The property-name Element
        67. The property-resolver Element
        68. The redirect Element
        69. The referenced-bean Element
        70. The referenced-bean-class Element
        71. The referenced-bean-name Element
        72. The render-kit Element
        73. The render-kit-class Element
        74. The render-kit-factory Element
        75. The render-kit-id Element
        76. The renderer Element
        77. The renderer-class Element
        78. The renderer-type Element
        79. The resource-bundle Element
        80. The resource-handler Element
        81. The source-class Element
        82. The state-manager Element
        83. The suggested-value Element
        84. The supported-locale Element
        85. The system-event-class Element
        86. The system-event-listener Element
        87. The system-event-listener-class Element
        88. The tag-handler-delegate-factory Element
        89. The to-view-id Element
        90. The validator Element
        91. The validator-class Element
        92. The validator-id Element
        93. The value Element
        94. The value-class Element
        95. The var Element
        96. The variable-resolver Element
        97. The view-declaration-language-factory Element
        98. The view-handler Element
        99. The view-param Element
        100. The visit-context-factory Element
      6. Extension Elements
        1. Metadata Elements
    2. 16 The Standard JSF Component Library
      1. A Brief Review of JSF, Facelets, and JSP Tag Nomenclature
      2. Acquiring and Installing the Standard Libraries
        1. What You Get (Binary)
        2. What You Get (Source)
        3. The Tag Library Reference
        4. The Standard Core Library
        5. The f:actionListener Tag
        6. The f:ajax Tag (2.0)
        7. The f:attribute Tag
        8. The f:convertDateTime Tag
        9. The f:convertNumber Tag
        10. The f:converter Tag
        11. The f:event Tag (2.0)
        12. The f:facet Tag
        13. The f:loadBundle Tag
        14. The f:metadata Tag (2.0)
        15. The f:param Tag
        16. The f:phaseListener Tag (1.2, 2.0)
        17. The f:selectItem Tag
        18. The f:selectItems Tag
        19. The f:setPropertyActionListener Tag (1.2, 2.0 Only)
        20. The f:subview Tag
        21. The f:validateBean Tag (2.0)
        22. The f:validateDoubleRange Tag
        23. The f:validateLength Tag
        24. The f:validateLongRange Tag
        25. The f:validateRegex Tag (2.0)
        26. The f:validateRequired Tag (2.0)
        27. The f:validator Tag
        28. The f:valueChangeListener Tag
        29. The f:verbatim Tag
        30. The f:view Tag
        31. The f:viewParam Tag (2.0)
      3. The Standard HTML Library
        1. The h:body Tag (2.0)
        2. The h:button Tag (2.0)
        3. The h:column Tag
        4. The h:commandButton Tag
        5. The h:commandLink Tag
        6. The h:dataTable Tag
        7. The h:form Tag
        8. The h:graphicImage Tag
        9. The h:head Tag (2.0)
        10. The h:inputHidden Tag
        11. The h:inputSecret Tag
        12. The h:inputText Tag
        13. The h:inputTextarea Tag
        14. The h:link Tag (2.0)
        15. The h:message Tag
        16. The messages Component
        17. The h:outputFormat Tag
        18. The h:outputLabel Tag
        19. The h:outputLink Tag
        20. The h:outputScript Tag (2.0)
        21. The h:outputStylesheet Tag (2.0)
        22. The h:outputText Tag
        23. The h:panelGrid Tag
        24. The h:panelGroup Tag
        25. The h:selectBooleanCheckbox Tag
        26. The h:selectManyCheckbox Tag
        27. The h:selectManyListbox Tag
        28. The h:selectManyMenu Tag
        29. The h:selectOneListbox Tag
        30. The h:selectOneMenu Tag
        31. The h:selectOneRadio Tag
      4. The Standard Facelets Templating Library (2.0)
        1. Example Website Project
        2. The ui:component Tag (2.0)
        3. The ui:composition Tag (2.0)
        4. The ui:debug Tag (2.0)
        5. The ui:decorate Tag (2.0)
        6. The ui:define Tag (2.0)
        7. The ui:fragment Tag (2.0)
        8. The ui:include Tag (2.0)
        9. The ui:insert Tag (2.0)
        10. The ui:param Tag (2.0)
        11. The ui:remove Tag (2.0)
        12. The ui:repeat Tag (2.0)
      5. The Standard Facelets Composite Component Library (2.0)
        1. Example Project
        2. The cc:actionSource Tag (2.0)
        3. The cc:attribute Tag (2.0)
        4. The cc:editableValueHolder Tag (2.0)
        5. The cc:extension Tag (2.0)
        6. The cc:facet Tag (2.0)
        7. The cc:implementation Tag (2.0)
        8. The cc:insertChildren Tag (2.0)
        9. The cc:insertFacet Tag (2.0)
        10. The cc:interface Tag (2.0)
        11. The cc:renderFacet Tag (2.0)
        12. The cc:valueHolder Tag (2.0)
  9. A JSF Portlets
    1. Overview of Portlet 1.0 and 2.0
      1. Portlet Lifecycle
      2. Portlet Modes
      3. Portlet Window States
      4. Portlet Preferences
      5. Inter-Portlet Communication
    2. JSF Portlet Development
      1. JSF Portlet Bridges
      2. JSF Portlet View Handlers
      3. JSF ExternalContext and the Portlet API
      4. JSF and Portlet Preferences
      5. JSF and Inter-Portlet Communication
    3. ICEfaces Portlet Development
      1. ICEfaces Ajax with Partial Submit
      2. ICEfaces Direct-to-DOM RenderKit
      3. The ice:portlet Tag
      4. ICEfaces 1.x Portlet Bridge
      5. ICEfaces 1.x D2DFaceletViewHandler
      6. ICEfaces 1.x and Portlet Window States
      7. ICEfaces Portlets and Concurrent DOM Views
      8. ICEfaces 1.x Extended Request Scope
      9. ICEfaces Ajax Push and Inter-Portlet Communication
      10. ICEfaces Themes and Portal Themes
      11. ICEfaces Themes and Liferay Themes
      12. ICEfaces Ajax Bridge and Liferay Portal
      13. ICEfaces Portlets and Liferay Request Attributes
    4. PortletFaces
      1. Downloading PortletFaces
      2. PortletFacesContext
      3. PortletFaces Tags
      4. PortletFaces and Portlet Preferences
      5. PortletFaces Expression Language Additions
      6. PortletFaces and Localization
      7. Liferay Language Portlet Integration
      8. Improved Integration Between Liferay and ICEfaces 1.x
  10. Index
  11. Footnote
    1. ch08fn1
    2. ch12fn1