Java
  Home arrow Java arrow Page 3 - Introducing Java Server Faces (JSF)
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introducing Java Server Faces (JSF)
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2006-10-17

    Table of Contents:
  • Introducing Java Server Faces (JSF)
  • JSF: Terminology Continued
  • JSF: Steps for Implementation
  • JSF in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing Java Server Faces (JSF) - JSF: Steps for Implementation


    (Page 3 of 4 )

    Like any other framework, JSF has its own steps that have to be followed by the developer for creating a workable application. The common steps that are the part of JSF based application development process are:

    1. Develop the model objects, which will hold the data.
    2. Add managed bean declarations to the Application Configuration File.
    3. Create the Pages using the UI component and core tags.
    4. Define Page Navigation.

    The sequence of the first and third steps can be swapped. That means, depending on your needs, model objects can be developed once the UI has been created and managed beans have been configured. With large enterprise applications, it is always better to develop model objects first and then work on UI development. With small- or medium-sized applications, rapid prototyping can be done using UI components and, when approved, model objects can be developed.

    Here are the details:

    Develop the model objects, which will hold the data

    Data forms the basis of any application. With web applications, it is always better to develop a model to hold the data. In essence a model object is the object world representation of a relational or non-relational data model such as tables. In JSF a model object is a JavaBean that is modeled after, for example, a table. The model object can be used to represent, process and return the result of the user's input. For instance, the following example holds the data entered in the text field:

    import java.util.Random;

    public class UserNumberBean {

    Integer userNumber = null;

    Integer randomInt = null;

    String response = null;

    public UserNumberBean () {

    Random randomGR = new Random();

    randomInt = new Integer(randomGR.nextInt(10));

    }

    public void setUserNumber(Integer user_number) {

    userNumber = user_number;

    }

    public Integer getUserNumber() {

    return userNumber;

    }

    public String getResponse() {

    if(userNumber.compareTo(randomInt) == 0)

    return "Yay! You got it!";

    else

    return "Sorry, "+userNumber+" is incorrect.";

    }

    The above class takes user input, generates random integers and compares them, then returns either an affirmative or negative String. One thing to keep in mind is that the model object and managed bean can be same Java class. This approach is mostly used in small projects.

    Add managed bean declarations to the Application Configuration File

    The model object and/or the managed bean must be added to the Application Configuration File, which is a technical term for faces-config.xml. It is done by providing the required details to the child elements of the <managed-bean> tag. For example, the Model object that has been shown in the above example can be added thus:

    <managed-bean>

             <managed-bean-name>

                      UserNumberBean

             </managed-bean-name>

             <managed-bean-class>

                    guessNumber.UserNumberBean

            </managed-bean-class>

            <managed-bean-scope>

                   Session

             </managed-bean-scope>

    </managed-bean>

    The <managed-bean-name> takes the name of the managed bean. Next comes the <managed-bean-class> that takes the fully qualified Java class name as its value. The scope of the bean is defined in the <managed-bean-class> element. The framework reads it and does the needful.

    Create the Pages using the UI component and core tags

    Now that the model object has been associated with the framework, next task is to create the page that will present the data to the user. This is done by developing the page using UI Components provided as tags. The two main tags are view and form. The first informs of the start of JSF tags and the second is for form controls in JSF. The following example creates a input box and connects it with the bean:

    <f:view>

    <h:form binding="#{Page1.form1}" id="form1">

                        <h:inputText binding="#{            

                                                                   guessNumber.UserNumberBean

                                                                  }"

                           id="textField1" style="left: 192px; top: 48px; position:    

                           absolute"/>

                        <h:commandButton binding="#{Page1.button1}" id="button1" style="left: 288px; top: 144px; position: absolute" value="Submit"/>

      </h:form>

        </f:view>

    Observe the inputText tag. It is connected with the managed bean using the binding attribute. And it comes under the form tag along with the commandButton tag. The whole set up is placed under the view tag. That's how pages are created using JSF tags and UI Components. Next let's see how to set up the navigation.

    Define Page Navigation

    In JSF, the center of action is faces-config.xml. So the navigation rules are also set up in the faces-config file. There are three main participating elements. Navigation-rule contains a set of rules for navigation within the application. Navigation-case is a child node of navigation-rule, and it defines the per page processing result which navigation will be defined. From-outcome and to-view-id are the child nodes of navigation-case. The former defines the expected result of the processing of a page and the later defines to which page the redirection must be done.

    The following is an example of such a navigation set up:

    <navigation-rule>

    <from-view-id>/login.jsp</from-view-id>

    <navigation-case>

    <from-outcome>success</from-outcome>

    <to-view-id>/mainmenu.jsp</to-view-id>

    </navigation-case>

             <navigation-case>

    <from-outcome>failure</from-outcome>

    <to-view-id>/login.jsp</to-view-id>

    </navigation-case>

    </navigation-rule>

    That covers the steps required in using JSF for development. The next section will focus on implementing these steps for a real world scenario.

    More Java Articles
    More By A.P.Rajshekhar


       · JSF has brought event driven paradigm to the web application development. In this...
       · Hi,On the last page you have mentioned struts-config, I think it should be...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek