What is JSF?

  • JSF is a component based and event driven framework for web applications.
  • It makes easier to create complex UI for an application using JSF tags.
  • JSF is much better than Servlets, JSP and struts technologies.
  • JSF is MVC based framework.
  • JSF provides standard, reusable components for creating user interfaces for web applications .
  • Goal of JSF is to create web applications faster and easier. It hides complexities to enable developers to focus on their own business logic.
  • JSF 1.2 is the latest release and it works with servlet 2.5 and JSP 2.1.
  • Built on top of Servlet API.

JSF Architecture
  • The architecture of JSF is based on MVC design pattern
  • The MVC design pattern splits an application design into three separate parts
    • Model:   handles data and logic.
    • View:    handles output (presentation)
    • Controller:  handles processing of an application.
  • The purpose of MVC is to separate model and view to enable developers to set focus on their core skills and collaborate more clearly.
  • Typically if you want to change the code for model then you does not need to change view layer and vice versa.
  • As controllers are used to process user actions. In this process model and views may be changed.

JSF Life Cycle

  • The life cycle of a JSF is similar to that of a JSP: The client makes an HTTP request for the page, and the server responds with the page translated to HTML. However, because of the extra features that JSF technology offers, the life cycle provides some additional services to process a 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
  • When the client submits a page, the JSF implementation must perform several tasks, such as validating the data input of components in the view and converting input data to types specified on the server side.
  • Which steps in the life cycle are executed depends on whether or not the request originated from a JSF application and whether or not the response is generated with the rendering phase of the JSF life cycle.
  • JSF Request Processing Life Cycle Scenarios
  • A JSF application supports two kinds of requests and two kinds of responses:
    • Faces request
    • Non-Faces request
    • Faces response
    • Non-Faces response:
  • These different requests and responses result in three possible life cycle scenarios that can exist for a JSF application:
    • Scenario 1: Non-Faces Request Generates Faces Response
    • Scenario 2: Faces Request Generates Non-Faces Response
    • Scenario 3: Faces Request Generates Faces Response
  • Standard Request Processing Life Cycle
    • The standard request processing life cycle represents scenario 3, which is the most common scenario for the life cycle of a JSF
    • Fig. shows the steps in the JSF life cycle
JSF Life Cycle

JSF life Cycle phases
  • Restore View
    • A request comes through the FacesServlet controller. The controller examines the request and extracts the view ID, which is determined by the name of the JSP page.
  •   Apply request values
    • The purpose of the apply request values phase is for each component to retrieve its current state. The components must first be retrieved or created from the FacesContext object, followed by their values.
  •   Process validations
    • In this phase, each component will have its values validated against the application's validation rules.
  •   Update model values
    • In this phase JSF updates the actual values of the server-side model, by updating the properties of your backing beans.
  •   Invoke application
    • In this phase the JSF controller invokes the application to handle Form submissions.
  •   Render response
    • In this phase JSF displays the view with all of its components in their current state. 

JSF Components

  • Components in JSF are elements like text box, button, table etc. that are used to create UI of JSF Applications.
  • It has two types:
    • Standard UI Components
      • JSF contains its basic set of UI components like text fields, check boxes, list boxes, panel, label, radio button etc. These are called standard components.
    • Custom UI Components
      • These types of components are not standard JSF components.  JSF provides this additional facility to let you create and use your own set of reusable components.
      • These components are called custom components or third party components

JSF Tags

  • JSF application typically uses JSP pages to represent views
  • JSF provides 43 tags in two standard JSF tag libraries:
    • JSF Core Tags  Library
    • JSF Html Tags Library
  • Even a very simple page uses tags from both libraries.
             <%@ taglib uri=”http://java.sun.com/jsf/core “ prefix=”f” %>
            <%@ taglib uri=”http://java.sun.com/jsf/html “ prefix=”h” %>

JSF Html Tags

  • These tags represent html components like text fields, buttons, form etc.
  • There are 25 tags in JSF Html Tag Library.

  • column :   creates column in a dataTable 
  • commandButton : creates button
  • commandLink:   creates link that acts like a pushbutton
  • dataTable :  creates a  table control
  • Form:   creates a form
  • graphicImage : displays an image
  • inputHidden:  creates hidden field
  • inputSecret :  creates input control for password
  • inputText : creates  text input control (single line)
  • inputTextarea:  creates  text input control (multiline)
  • message:   displays the most recent message for a component 
  • messages : displays all messages 
  •  outputFormat : creates  outputText, but formats compound messages
  • outputLabel : creates label 
  • outputLink:  creates anchor
  • outputText : creates single line text output
  • panelGrid: creates html table with specified number of columns
  • panelGroup: used to group other components where the specification requires one child element
  • selectBooleanCheckbox: creates checkbox
  • selectManyCheckbox:   creates set of checkboxes
  • selectManyListbox:   creates multiselect listbox
  • selectManyMenu : creates multiselect menu
  • selectOneListbox : creates single select listbox
  • selectOneMenu:  creates single select menu
  • selectOneRadio:  creates set of radio buttons

JSF Core Tags
  • These tags are used for validation, conversion , event handling, views and sub-views etc.
  • There are 18 tags in JSF Core Tag Library.
    • f :view:     Creates the top-level view
    • f:subview:  Creates a subview of a view
    • f:attribute :  Adds an attribute  to a component
    • f:param:    Constructs a parameter component
    • f:converter:      Adds an arbitrary converter to a component
    • f:converterDateTime:    Adds a datetime converter to a component
    • f:converterNumber :  Adds a number converter to a component
    • f:actionListener :     Adds an action listener to a component
    • f:valueChangeListener:     Adds a valuechange listener to a component
    • f:validator :   Adds a validator to a component
    • f:validateDoubleRange:    Validates a double range for a component value
    • f:validateLength:   Validates the length of a component value
    • f:validateLongRange:    Validates a long range for a component value
    • f:facet:     Adds a facet to a component
    • f:loadBundle:    Loads a resource bundle, stores properties as a Map
    • f:selectitems:    Specifies items for a select one or select many component
    • f:selectitem:    Specifies an item for a select one or select many component
    • f:verbatim:    Adds markup to a JSF page
JSF Managed Beans

  • JavaBeans objects managed by a JSF implementation are called managed beans.
  •  A managed bean describes how a bean is created and managed. It has nothing to do with the bean's functionalities.
  • Syntax (configured in the faces-config.xml file)
              <managed-bean-name> login </managed-bean-name>
              <managed-bean-class> LoginBean </managed-bean-class>
              <managed-bean-scope> request </managed-bean-scope>
  • Remember it is possible to have number of faces-config.xml file. However the developer should need to register it in web.xml.
JSF Backing Beans
  • Backing beans are JavaBeans components associated with UI components used in a page
  • Backing Beans should be defined only in the request scope.

Bean Scope and its Types

  •  Bean Scope typically holds beans and other objects that need to be available in the different components of a web application.
  • JSF supports three Bean Scopes
  • Request Scope
    • The request scope is short-lived. It starts when an HTTP request is submitted and ends when the response is sent back to the client.
  • Session Scope
    • The session scope persists from the time that a session is established until session termination.
  • Application Scope
    • The application scope persists for the entire duration of the web application. This scope is shared among all the requests and sessions.

No comments:

Post a Comment