Home arrow Java arrow Page 3 - JSF Development Process Overview
JAVA

JSF Development Process Overview


This article outlines the steps involved in creating an application using JavaServer Faces. It is excerpted from chapter two of the book JavaServer Faces, written by  by Hans Bergsten (O'Reilly, 2004; ISBN: 0596005393).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 8
October 13, 2005
TABLE OF CONTENTS:
  1. · JSF Development Process Overview
  2. · Developing an Application with a JSF-Based User Interface
  3. · Developing Components and Integration Code
  4. · Developing the User Interface Pages

print this article
SEARCH DEVARTICLES

JSF Development Process Overview - Developing Components and Integration Code
(Page 3 of 4 )

The component writer develops all application-specific Java code needed for the user interface, ranging from simple classes that tie the user interface to the application backend code developed by the application developer to custom user interface components when the ones provided by JSF arenít sufficient.

Figure 2-2 shows the main classes and interfaces used by the newsletter application.

 

Figure 2-2.   JSF component-related classes and the  
                         Subscriber application class

You probably recognize theSubscriberclass from the previous section. The component writer develops theSubscriberHandlerclass, as shown in this section. All the other classes in Figure 2-2 are provided by the JSF implementation.

TheUIComponentBaseclass is the base class for all JSF UI components. Subclasses represent specific interface elements, such as text fields, links and buttons, labels, menus and selection lists. JSF includes a set of component classes that can be used right out of the box, such as the ones shown in Figure 2-2, but a component writer can also implement custom components if needed. TheUIInputclass represents an input field and lets you bind the component to an application model through a value binding. When a component is rendered, it pulls its value from the application model object based on this value binding. Similarly, when an input component processes a request, it updates the model itís bound to with the value received with the request. In this example, value bindings for theUIInputand theUISelectMany(the checkbox group) components bind the components to the corresponding properties in theSubscriberapplication class.

The components fire events in response to user actions (such as clicking a button) and event listeners attached to the components handle the events (for example, by updating a database). Instead of implementing and registering listeners for each component, most JSF applications take advantage of shortcuts in the form of method bindings. A method binding is similar to a value binding, but it binds a component to an application method instead of an application property value. For instance, theUICommandcomponent has a property that takes a method binding value. When the component fires anActionEvent, a defaultActionListenerprovided by JSF and automatically attached to the component invokes the method that the method binding points to. All the component writer needs to do is implement the method.

For the subscription example application, letís say that the component writer implements the action event processing method in a new class called   SubscriberHandler:

package com.mycompany.newsservice.handlers;
import com.mycompany.newsservice.models.Subscriber;
public class SubscriberHandler{
    private Subscriber subscriber;
    public void setSubscriber(Subscriber subscriber) {
        this.subscriber = subscriber;
    }
   
public String saveSubscriber() {
       
subscriber.save();
       
return "success";
   
}
}

TheSubscriberHandler class has two methods: a setter method for associating it with an instance of theSubscriberclass and a method for handling the Save buttonActionEvent. ThesaveSubscriber()method simply calls thesave()method on theSubscriberinstance and returnssuccess. In a real application, it would return one or more other values if things didnít go as planned, e.g., systemFailure if the database wasnít available.

It may seem like overkill to create a new class with the only purpose of calling a method in the application backend class, and in this simple example, it probably is. As youíll see when we start implementing a more complex application, thereís a lot more that typically goes into this type of class. Its main purpose is to bridge the gap between the pure application objects and JSF components. For instance, it would be a bit odd for thesave( )method inSubscriberto return aString, but it serves a purpose for a JSF action event processing method to do so, because JSF can use the returned value to decide what to do next. But letís save that for later and move on to the next component writer task.

JSF creates and configures instances of application classes, such as theSubscriber andSubscriberHandlerclasses, based on information specified in a configuration file named faces-config.xml. It makes the instances available through variables that the page author uses in value and method binding expressions, as youíll see in the next section. Creating the configuration file is a task that also falls on the component writerís shoulders because itís part of tying the user interface and the backend together. Hereís a snippet of the configuration file with the declarations for the example application classes:

<faces-config>
 
...
 
<managed-bean>
   
<managed-bean-name>subscr</managed-bean-name>
   
<managed-bean-class>
 
com.mycompany.newsservice.models.Subscriber
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
  <managed-bean>
   
<managed-bean-name>subscrHandler</managed-bean-name>
    <managed-bean-class>
     
com.mycompany.newsservice.handlers.
SubscriberHandler
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
     
<property-name>subscriber</property-name>
     
<value>#{subscr}</value>
   
</managed-property>
 
</managed-bean>
  ...
</faces-config>

As is customary these days, the configuration file is an XML document. The application objects are declared by<managed-bean>elements. The first<managed-bean>element declares that an instance of theSubscriber class should be made available as a session scope variable namedsubscr. Iíll get back to the scope in Chapter 4, but declaring the scope as session (as in this example) means that a unique instance is created for each user and remains available as long as the user actively uses the application.

The second<managed-bean>element contains a similar declaration of a variable namedsubscrHandler as an instance of theSubscriberHandler, but it also contains a<managed-property>element, saying that itssubscriber property must be initialized with the value of thesubscrvariable. This is how theSubscriberHandlerinstance is linked to theSubscriberinstance.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials