Wednesday, November 27, 2013

Understanding JSF in few Minutes..


What is JSF

  • UI component based Java Web application framework
  • UI components and their state are represented on the server with a defined life-cycle
  • JSF Application have UI Component / Configuration File  ( faces-config.xml  / web.xml )

 Configuration Files :
faces-config.xml Manages :
Managed Bean
Navigation
validators
data converters
( Once you define the object ("Person") in faces-config.xml you can use the attributes of Person in your JSF UI components, e.g. by binding the value "firstName" of this object to an JSF input field. )

web.xml
You must specify in web.xml that a "FacesServlet" is responsible for handling JSF applications. "FacesServlet" is the central controller for the JSF application. "FacesServlet" receives all requests for the JSF application and initializes the JSF components before the JSP is displayed.


Value and Method Binding
In JSF you do not need to specify the get() or set() method but just the variable name.
JSF universal Expression Language (EL) uses  #{...}. These are only evaluated when needed opposite to JSP EL which are always executed .

JSF Main features


  • based on the Model-View-Controller concept
  • stateful UI component model, e.g. each component is aware of its data
  • separates the functionality of a component from the display of the component
  • support listeners on UI components
  • support data validation, data binding and data conversion between the UI and the model


JSP  vs Facelets

It namely provides great templating capabilities such as composite components .
As being a MVC (Model-View-Controller) framework, JSF provides the FacesServlet as the sole request-response Controller. It takes all the standard and tedious HTTP request/response work from your hands, such as gathering user input, validating/converting them, putting them in model objects, invoking actions and rendering the response.

JSF 2.0

Unlike JSF 1.x, almost everything is declared in faces-config.xml, with JSF 2.0, you are allowed to use annotation to declare navigating, managed bean . It uses Facelets .


Bean Management :

There are 2 ways to manage beans in JSF
  1. Entry in faces-config.xml
  1. Using Annotation

In  faces-config.xml

version="1.0" encoding="UTF-8"?>

    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
   
          helloworldBean

          com.manish.common.HelloWorldBean
          session
    


Using Annotation :

package com.manish.common;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;

@ManagedBean
@SessionScoped
public class HelloWorldBeanimplements Serializable {

        private static final long serialVersionUID = 1L;

        private String name;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
}


Quick Start

Managed Bean

More Example on Refer