Friday, 25 January 2013

1. Introduction to JavaServer Faces (JSF)

For a web application limited to servlet or JSPs, our approach is something like this:

On a page, user gives input parameter, we retrieve those inside a servlet or a JSP page and then we come to know what user had done.
Consider this example having some JSP pages. Here pressing one button generate one action, pressing another button generate another action and based upon what button is pressed we decide what to do further. Moreover, we distinguish these buttons using strings to know what user wants to do.

If we relate all this to swing (which is also a GUI), what there happens is when we press a button, a event will generate and we will handle that event. But here in web apps, we are pressing button, then inside some servlet we are doing request.getParameter, so there is nothing like an event model here as this is a request response model.

However, it is much easier to use an event model. Life is much simpler there as we can simply access GUI components by using things like textfield1.getvalue or setvalue. Now as new technologies and frameworks have evolved in some past years, such event models are available in web also. These things are getting automated and we have to write very less code as we do in swing.
JavaServer Faces (JSF) is one such example.

Evolution of JSF
Prior to JSF, Structs framework was there which provided a structured programming model (MVC) but doesn't support UI tags.

To fulfil all the requirements, several vendors like Sun, Oracle, IBM etc.. met through the Java Community Process (JCP) in 2001 and voted to proceed with a comprehensive and detailed specification for building Java EE thin-client Web applications whose primary goal was to provide a standard and much simpler way to build user interfaces for Java Web applications.
This resulted in JSR #127, and which resulted in JSF. JSF combines an MVC approach with component-based UI framework that greatly simplifies Java EE Web development as compared with servlets or JSP.
So, we can say that if we combine Desktop Java & Struts, we get JSF.

What is JSF ?
A framework which provides solution for:
  • representing UI components
  • managing their states
  • handling events
  • input validations
  • data bindings
  • automatic conversion
  • defining page navigation
  • Supporting internationalization and accessibility.
JSF provide RAD facility at page level and provide event model by which development time is reduced.

Now again coming to the basics - Browser can only generate/understand HTML, so how these components are written for us in java ?
So the answer is that these JSF components are written like JSP custom tags. We can create custom tag and thus can generate HTML from those custom tags. So these components are in the form of custom tags and we are given with already lot of worked done in the form of components.
A web page in JSF is represented by server-side java objects and UI components.

Typically if we don't use any IDE and code these tags in notepad, it might look like this:

JSF code for button - 

    <h:commandButton value="Login"
    action="#{customer.login}" />

and we would create a class Customer like:

      class Customer {
           public void loginActionListener (ActionEvent e) {
           public String login () {
              return "OK" ;

However, these things are very easy to implement if we use any IDE which can provide simply drag and drop functionality.

No comments:

Post a Comment