07_Creating the Web Tier Using Java Server Faces

Embed Size (px)

Citation preview

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    1/28

    7Copyright 2007, Oracle. All rights reserved.

    Creating the Web Tier Using

    JavaServer Faces

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    2/28

    7-2 Copyright 2007, Oracle. All rights reserved.

    Objectives

    After completing this lesson, you should be able to do

    the following:

    Describe the purpose of JavaServer Faces

    Use JSF components Explain the use of managed beans

    Describe the life cycle of a JSF application

    Explain the role of the JSF tag libraries

    Describe how JDeveloper supports JSF Create a JSF-based JSP in JDeveloper

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    3/28

    7-3 Copyright 2007, Oracle. All rights reserved.

    JSF: Overview

    JavaServer Faces (JSF) is a server-side component

    framework for Web applications.

    JSF:

    Is an implementation of the MVC design pattern Enables separation of business and presentation

    logic

    Enables separation of navigational and data flow

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    4/28

    7-4 Copyright 2007, Oracle. All rights reserved.

    JSF: Benefits

    Simplifies Java EE development

    Intended for RAD style development tools

    Component-based architecture

    Java EE framework functionality State management

    Error handling

    Input validation

    Type conversion Event handling

    Page navigation

    Portable across JSF implementations

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    5/28

    7-5 Copyright 2007, Oracle. All rights reserved.

    Key Terms

    UI component: The components that manage data

    to be displayed on a page

    Managed bean: Objects that maintain data and

    methods that are used across multiple pages Expression Language: Shortcut to access

    properties and methods from a JSF page

    Navigation model: The rules that govern page flow

    Life cycle: The processes that are executed from

    the time a page is loaded until it is complete

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    6/28

    7-6 Copyright 2007, Oracle. All rights reserved.

    JSF Architecture

    JavaServer Faces has:

    A set of prefabricated UI components

    An event-driven programming model

    A component model that enables third-partydevelopers to build additional components

    It can be thought of as Swing for server-side

    applications.

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    7/287-7 Copyright 2007, Oracle. All rights reserved.

    JSF Architecture

    Page

    HTML

    render kit

    Frontcontroller

    Page

    Backend

    code

    HTML

    browser

    Phone/PDA

    WML

    render kit

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    8/287-8 Copyright 2007, Oracle. All rights reserved.

    JSF Components

    JSF Components consists of three parts:

    UI components: Functionality, attributes, or

    behavior

    Renderers: Converts components to and from aspecific markup language

    Render kits: Library of renderers (The Basic

    HTML RenderKit is part of the specification.)

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    9/287-9 Copyright 2007, Oracle. All rights reserved.

    JSF UI Components

    Are the basic building blocks of a JSF application

    Are stateful server objects

    Do not define rendering

    Can represent simple tocomplex user interface

    components ranging from

    a button or input field to a complete page

    Can be associated to model data objects through

    value binding

    Can use helper objects such as validators,

    converters, listeners, and events

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    10/287-10 Copyright 2007, Oracle. All rights reserved.

    JSF Component Architecture

    Expr. Language

    Managed beanJSF Page

    J2EE Persistence Layer / JDBC

    Markup

    RDBMS

    UI Component

    Device Renderer

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    11/287-11 Copyright 2007, Oracle. All rights reserved.

    Tag Handlers and Component Trees

    Each tag has a corresponding tag handler class.

    JSF tag handlers collaborate with each other to

    build a component tree.

    f:form, h:inputText, h:inputSecret,h:commandButton

    UIForm

    UIInput UIOutput UICommand

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    12/287-12 Copyright 2007, Oracle. All rights reserved.

    Tag Libraries

    Include HTML tag library

    h: tag for HTML

    Component tags for all UI components and HTML

    rendering kit

    Example: h:form, h:inputText, h:messages

    Core tag library

    f: tags for faces

    Custom actions independent of any rendering kit

    Example: f:validator, f:converter,

    f:selectItem

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    13/287-13 Copyright 2007, Oracle. All rights reserved.

    Configuration Files

    faces-config.xml

    Defines managed beans

    Specifices navigation rules

    Must exist withWEB-INF Similar to struts-config.xml

    Servlet mapping in web.xml uses a faces

    extension.

    URL pattern *.faces maps to

    java.faces.webapp.FacesServlet.

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    14/287-14 Copyright 2007, Oracle. All rights reserved.

    JSF Renderers

    Two rendering models:

    Direct implementation

    Delegated implementation

    Delegated implementation enables separation ofUI from how they appear to the user.

    Each JSF implementation must provide a default

    RenderKit instance.

    JSF specification includes the Standard HTML

    RenderKit Specification.

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    15/287-15 Copyright 2007, Oracle. All rights reserved.

    Managed Beans

    Java objects (empty constructor), maps, lists

    Defined in faces-config.xml

    Defined with various scopes:

    application session

    request

    none

    Lazy initialization by JSF as needed

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    16/287-16 Copyright 2007, Oracle. All rights reserved.

    Expression Language

    Dot notation for attributes (JavaBean model):

    #{userbean.name} same as

    instance.getName();

    Map notation:#{foo["baa"]} same as

    instance.get("baa");

    Expressions can be of any depth:

    #{foo["baa"].person.name}

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    17/287-17 Copyright 2007, Oracle. All rights reserved.

    Life Cycle of a JSF Page

    A JSF page is represented by a tree of UI

    components, called a view.

    When a client makes a request for the page, the

    life cycle starts. During the life cycle, JSF implementation must

    build the view while considering the state saved

    from the previous postback.

    When the client performs a postback of the page,

    JSF implementation must perform life-cycle steps: Validation

    Conversion

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    18/287-18 Copyright 2007, Oracle. All rights reserved.

    JSF Life Cycle: Initial Request

    Restore/createview

    Client/Browser

    Renderresponse

    Applyrequest

    values

    UpdatemodelInvokeapplication

    Processvalidation

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    19/287-19 Copyright 2007, Oracle. All rights reserved.

    JSF Life Cycle: Postback

    Restore/createview

    Client/Browser

    Renderresponse

    Applyrequest

    values

    UpdatemodelInvokeapplication

    Processvalidation

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    20/287-20 Copyright 2007, Oracle. All rights reserved.

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    21/287-21 Copyright 2007, Oracle. All rights reserved.

    Formal Phases: JSP

    JSP life cycle

    ServletURL parsing

    Client/Browser

    RenderJSP response

    Product code

    (state management,

    events, and so on)

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    22/287-22 Copyright 2007, Oracle. All rights reserved.

    Using JSF Tag Libraries

    JSF UI components are encapsulated in JSP

    tag libraries:

    Core: For application tasks,

    such as validation, and data

    type conversion

    HTML: For rendering basic

    HTML, such as input fields,

    menus, tables, and buttons

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    23/287-23 Copyright 2007, Oracle. All rights reserved.

    JSF Applications

    A typical JSF application consists of:

    One or more JSPs containing JSF components

    A navigation model specified in the

    faces-config.xml file A set of managed beans that facilitate the UI logic

    of the application

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    24/287-24 Copyright 2007, Oracle. All rights reserved.

    JSF and JDeveloper 10.1.3

    JDeveloper provides many features for creating JSF

    components:

    JSF visual editing

    JSF UI componentvisual editing

    Provides backend

    code generation

    (double-click)

    JSF Configuration Editorfor productive editing offaces-config.xml

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    25/287-25 Copyright 2007, Oracle. All rights reserved.

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    26/28

    7-26 Copyright 2007, Oracle. All rights reserved.

    JSF Navigation Diagram

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    27/28

    7-27 Copyright 2007, Oracle. All rights reserved.

    Summary

    In this lesson, you should have learned how to:

    Describe the purpose of JavaServer Faces

    Use JSF components

    Explain the use of managed beans Describe the life cycle of a JSF application

    Explain the role of the JSF tag libraries

    Describe how JDeveloper supports JSF

    Create a JSF-based JSP in JDeveloper

  • 8/14/2019 07_Creating the Web Tier Using Java Server Faces

    28/28

    Practice Overview:

    Developing JSF Pages

    This practice covers the following topics:

    Creating a basic JSF page

    Developing a JSF page that uses stored values

    Building a display JSF page Creating a simple navigation diagram