Friday, April 13, 2007

All About JSF

Java Server Faces (JSF)

What is JSF??


• A set of Web-based GUI controls and associated handlers

– JSF provides many pre-built HTML-oriented GUI controls, along
with code to handle their events.

• A device-independent GUI control framework

– JSF can be used to generate graphics in formats other than HTML,
using protocols other than HTTP.

• A better Struts
– Like Apache Struts, JSF can be viewed as an MVC framework for
building HTML forms, validating their values, invoking business
logic, and displaying results.


JSF V/S Struts


• Custom components

– JSF makes it relatively easy to combine complex GUIs
into a single manageable component; Struts does not

• Support for other display technologies
– JSF is not limited to HTML and HTTP; Struts is

• Access to beans by name
– JSF lets you assign names to beans, then you refer to
them by name in the forms. Struts has a complex process
with several levels of indirection where you have to
remember which form is the input for which action.

• Expression language
– The JSF expression language is more concise and
powerful than the Struts bean:write tag.


• This is less advantageous if using JSP 2.0 anyhow.

Simpler controller and bean definitions
– JSF does not require your controller and bean classes to extend any particular parent class (e.g., Action) or use any particular method (e.g., execute). Struts does.

• Simpler config file and overall structure
– The faces-config.xml file is much easier to use than is the struts-config.xml file. In general, JSF is simpler.

• More powerful potential tool support
– The orientation around GUI controls and their handlers
opens possibility of simple to use, drag-and-drop IDEs

• Established base and industry momentum
– Struts has a large core of existing developers and momentum among both developers and IT managers; JSF does not.

• Support for other display technologies
– JSF is not limited to HTML and HTTP; Struts is

• Confusion vs. file names
– The actual pages used in JSF end in .jsp. But the URLs used end in
.faces or .jsf. This causes many problems; in particular, in JSF:

• You cannot browse directories and click on links

• It is hard to protect raw JSP pages from access

• It is hard to refer to non-faces pages in faces-config.xml

• Self-submit approach
– With Struts, the form (blah.jsp) and the handler (blah.do) have
different URLs; with JSF they are the same.




The JSF Expression Language


Access bean property or method

#{myBean.myProperty}

#{myBean.myMethod}

Access for array, list element or map entry

#{myBean.myList[5]}

#{myBean.myMap['key']}



Boolean Expression ==, !=, <=, ... #{myBean.myValue != 100 } #{myBean.myValue <= 200 } The Request Processing Life Cycle



Restore View

Extract the view ID -> find current

view

Look up the components for the

current view, create them if necessary

Restore or create component values,

event listeners, validators and
converters

If no (new) user input -> skip to




Render Response Phase



Application Request Values

Decoding: extract user input from

input components

Create Action Events and add them to

Faces Context for later processing

if immediate = "true"

-Validation of submitted values

-Action Sources fire action events


Process Validators
Convert submitted values

Validate submitted values

Conversion or validation errors ->

Render Response (6), else ...

Set local value to the converted and

validated submitted values

Fire value change events

Update Modal Values

Find appropriate bean in the scope

(request, session, application)

Set the bean property to the new value


Invoke Application

Handle all fired events

-Execute default action listener method

-Call the event handler methods of all

registered event listeners

Decide navigation

-depending on the outcome of the fired

action method

Render Response

Display the next view

-Render tree of UIComponents

-with the updated values from the backing

beans

Invoke converters

Save the state of the new view

-for later use in the Restore View Phase

JSF Application Configuration


Setting Up JSF

Download jsf jar files

Put jar files in class path

Download jsf-blank-myfaces war file

Directory Structure




Deployment Descriptor






The Standard JSF Components


Shared Attributes

Id-Identifier for a component

rendered- Boolean value false suppresses rendering

Value-The components value,typically a value binding

Required- Boolean value true requires a value to be entered into the input field

styleClass-CSS class name

Converters and Validators

Type Conversion

and Input Validation


Automatic Conversion

All basic Java types are automatically

converted between string values and

objects.

BigDecimal

BigInteger

Boolean, boolean

Byte, byte

Character, char

Integer, int

Short, short

Double, double

Float, float

Long, long

No comments: