A first look at JavaServer Faces, Part
2
Tutorial Details:
A first look at JavaServer Faces, Part 2
A first look at JavaServer Faces, Part 2
By: By David Geary
Explore JavaServer Faces components
ecall from Part 1 that JavaServer Faces (JSF) is conceptually a mixture of Struts (Apache's popular open source JSP framework) and Swing (Java's standard desktop application framework). Like Struts, JSF provides a well-defined lifecycle, shown in Figure 1, for processing requests.
Figure 1. The JavaServer Faces lifecycle. Click on thumbnail to view full-size image.
In Part 1, the JSF lifecycle was discussed in detail and therefore is not discussed here; however, this article refers to the JSF lifecycle, and Figure 1 is repeated for convenience.
Like Swing, JSF specifies a rich component hierarchy for implementing server-side user interface (UI) components. That hierarchy lets developers implement components from scratch with various renderers that can render different markup languages. The JSF component hierarchy and related objects such as validators, event handlers, and model objects are the focus of this article's following sections:
JSF components
Implement custom validators
Model objects at work
Internationalize Web apps with JSF
Implement custom components
Read the whole series, "A First Look at JavaServer Faces:"
Part 1: Learn how to implement Web-based user interfaces with JSF
Part 2: Explore JavaServer Faces components
JSF components
JSF's component hierarchy separates JSF from other Web application frameworks like Struts. For example, both Struts and JSF have a set of custom JavaServer Pages (JSP) tags that represent HTML components, such as text fields and option lists, but the Struts tags generate HTML directly, whereas the JSF tags create server-side components that generate HTML. At first glance, the JSF approach might not have an obvious advantage until you realize that JSF components can be fitted with renderers that generate markup languages other than HTML. So, you can implement a renderer for your markup language of choice and associate that renderer with an existing JSF component. Also, the rich JSF component hierarchy makes it relatively easy to create custom components like a tree viewer or a query builder.
Figure 2 shows the class diagram for JSF components.
Figure 2. JSF UI components class diagram. Click on thumbnail to view full-size image.
JSF components implement the javax.faces.component.UIComponent interface, which specifies a whopping 46 methods that define the essence of a JSF component; Figure 2 lists only a handful of those methods. Luckily, your components can extend the abstract class javax.faces.component.UIComponentBase , which implements sane defaults for all UIComponent methods except for getComponentType() . That means you can implement a custom component simply by extending UIComponentBase and implementing getComponentType() .
Each JSF component has:
A list of child components
A hashmap of attributes
One or more validators
One or more event handlers
An identifier for an optional renderer
All JSF components are potential containers, by virtue of a child components list maintained by every JSF component. That lets you nest components?even if they contain other components?inside other components. This crucial ability, which is the impetus behind the Composite design pattern, is routinely implemented by object-oriented graphical user interfaces (GUIs), such as Swing or VisualWorks Smalltalk.
JSF components also maintain a list of attributes. Those attributes store component-specific information. For example, you may want to store the URL associated with an image used by a component; so you could store that URL?or the image itself?in the component's list of attributes. Component attributes are stored by name in a hash map.
All JSF components perform three fundamental tasks:
Render the component, typically by generating markup
Handle the component's events
Validate the component's values
JSF components can render themselves or delegate rendering to a renderer. The boolean UIComponent.rendersSelf() method tells the JSF implementation whether or not a component renders itself; if not, the JSF implementation obtains a reference to the component's renderer with the UIComponent.getRendererType() method's help and then calls on the renderer to produce markup for the component.
JSF component event handling can also be managed directly by a component, or components can delegate to an event handler. One or more event handlers can be registered for a component, typically by a component's renderer or the component itself, during the JSF lifecycle's Apply Request Values phase.
Finally, JSF components can have one or more validators that validate input. Those validators, which are usually created by the JSF implementation, are stored by components in an array list.
Now that we have a basic understanding of JSF components, let's look at implementing a custom validator and associating it with a component.
Implement custom validators
In Part 1 , we discussed using built-in validators to validate input for JSF components. If you use JSP for your Web application's views?which is typically the case?you can specify validators for JSF components with a tag, like this:
The code fragment attaches a validator to a text field; you just specify the validator's class name with the tag's className attribute. The validator specified above is a JSF built-in validator that checks to make sure a component's value is not null . As discussed in Part 1, JSF provides a handful of built-in validators, but you can also implement your own validators and associate them with a JSF component. For example, the application shown in Figure 3 uses a custom validator to validate a username.
Figure 3. Use a custom validator. Click on thumbnail to view full-size image.
The JSP page displayed in Figure 3 is listed in Listing 1.
Listing 1
A Simple JavaServer Faces Application
<%@ taglib uri='http://java.sun.com/j2ee/html_basic/' prefix='faces' %>
Please enter your name and password
Name: |
className='com.sabreware.validators.NameValidator'/>
className='javax.faces.validator.LengthValidator'/>
name='javax.faces.validator.LengthValidator.MINIMUM'
value='3'/>
|
|
Password: |
|
Like the code fragment shown at the beginning of this section, the preceding JSP page uses tags to attach validators to a JSF component. In this case, the validators are a custom validator that authenticates a username and a JSF built-in validator that checks to make sure the username is at least three characters long.
The custom validator used in Listing 1 is listed in Listing 2.
Listing 2. WEB-INF/classes/com/sabreware/validators/NameValidator.java
package com.sabreware.validators;
import java.util.Iterator;
import javax.faces.component.UIComponent;
import javax.faces.component.AttributeDescriptor;
import javax.faces.context.FacesContext;
import javax.faces.context.Message;
import javax.faces.context.MessageImpl;
import javax.faces.validator.Validator;
public class NameValidator implements Validator {
public AttributeDescriptor getAttributeDescriptor(String attributeName) {
return null;
}
public Iterator getAttributeNames() {
return null;
}
public void validate(FacesContext context, UIComponent component) {
String name = (String)component.getValue();
if(!"phillip".equalsIgnoreCase(name)) {
context.addMessage(component,
new MessageImpl (Message.SEVERITY_ERROR, "bad username",
"The username " + name + " is invalid"));
}
}
}
The preceding validator implements the javax.faces.validator.Validator interface, which defines the methods listed below:
void validate(FacesContext, UIComponent)
Iterator getAttributeNames(String)
AttributeDescriptor getAttributeDescriptor(String)
The validate() method performs the actual validation for a given component. The other two methods, defined by the Validator interface, are used by tools to discover attributes (and their descriptions) associated with a particular validator. In this case, we don't have any attributes for our validator, so the getAttributeDescriptor() and getAttributeNames() methods simply return null .
A validator's validate() method does nothing if the component's values are valid; if those values are invalid, the validator() method creates messages and adds them to the JSF context. All of that happens during the JSF lifecycle's Process Validations phase. If a component fails validation?meaning messages have been added to the JSF context?the JSF implementation proceeds directly to the Render Response phase; otherwise, the lifecycle proceeds to the Apply Model Values phase. (Refer to Figure 1 for more information about the JSF lifecycle phases.)
Model objects at work
In a Model-View-Controller (MVC) architecture, views, which are typically JSP pages for Java-based Web applications, display values contained in a model. JavaServer Faces makes it easy to connect UI components to fields stored in model objects. As we saw in the last section, if a request's values are all valid, the JSF lifecycle moves from the Process Validations phase to the Apply Model Values phase. During the latter phase, the JSF implement
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: A first look at JavaServer Faces, Part
2
View Tutorial: A first look at JavaServer Faces, Part
2
Related
Tutorials:
To EJB, or not to
EJB?
To EJB, or not to
EJB? |
Rumble in the
jungle: J2EE versus .Net, Part
1
Rumble in the
jungle: J2EE versus .Net, Part
1 |
A first look at JavaServer Faces, Part I
A first look at JavaServer Faces, Part Learn how to implement Web-based user interfaces with JSF |
A first look at JavaServer Faces, Part
2
A first look at JavaServer Faces, Part
2 |
The J2EE 1.4 Tutorial
The J2EE 1.4 Tutorial is a guide to developing enterprise applications for the Java 2 Platform, Enterprise Edition (J2EE) version 1.4. Here we cover all the things you need to know to make the best use of this tutorial. |
JavaServer Faces, redux
JavaServer Faces, redux |
Introduction to JavaServer Faces
This article is meant to acquaint the reader with JavaServer Faces, commonly known as JSF. JSF technology simplifies building the user interface for web applications. It does this by providing a higher-level framework for working with your web app, repres |
Improving JSF by Dumping JSP
Improving JSF by Dumping JSP
After a long wait and high expectations, JavaServer Faces (JSF) 1.0 was finally released on March 11, 2004. JSF introduces an event-driven component model for web application development, similar in spirit and function to t |
Creating JSF Custom Components
Creating JSF Custom Components
This article illustrates how to build custom components for use in web applications based on JavaServer Faces (JSF). While JSF comes with a standard set of components, one of the most-publicized features is the easy additio |
Put JSF to work
Build a real-world Web application with JavaServer Faces, the Spring Framework, and Hibernate
Summary
Building a real-world Web application using JavaServer Faces is not a trivial task. This article shows you how to integrate JSF, the Spring Framewor |
Handling Events in JavaServer Faces, Part 2
Here in part two, Hans implements event handling for parts of the sample application discussed in part one.
|
Handling Events in JavaServer Faces, Part 1
In this excerpt from the book, author Hans Bergsten looks at the JSF event model, using examples to help explain what\'s going on "under the hood." |
JavaServer Faces Technology
JavaServer Faces technology is a server-side user interface component framework for Java technology-based Web applications. |
Integrating Struts, Tiles, and JavaServer Faces
Integrating Struts, Tiles, and JavaServer Faces. Bring the power, flexibility, and manageability of the three technologies together. |
Atricle Struts, JavaServer Faces, and Java Studio Creator:
The Evolution of Web Application Frameworks Sun Microsystems' Craig McClanahan, the creator of the Apache Struts Framework, co-specification lead for JavaServer Faces 1.0, and prime architect for Sun Java Studio Creator's new release, explains all three. |
Chat Transcript: JSP 2.1 Technology and JSF 1.2 Technology
The next release of JavaServer Pages (JSP) technology, JSP 2.1, and the next release of JavaServer Faces (JSF) technology, JSF 1.2, are designed to improve the alignment of these two technologies in the area of expression language, and to enhance their ea |
Struts, JavaServer Faces, and Java Studio Creator:
The Evolution of Web Application Frameworks Sun Microsystems' Craig McClanahan, the creator of the Apache Struts Framework, co-specification lead for JavaServer Faces 1.0, and prime architect for Sun Java Studio Creator's new release, explains all three. |
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java
Open Source Web Frameworks in Java
Struts
Struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP. Struts is maintained as a part of Apache Jakarta project and is open |
Running JavaServer Faces Technology-Based Portlets on Sun Java System Portal Server 6 2005Q1
You can extend the framework based on JavaServer Faces technology and then run a JSR 168-compliant portlet on Sun Java System Portal Server 6. This article describes the setup procedures, offers sample code, and summarizes the known issues. |
JavaServer Faces in Action, Chapter 8
Shows how to build a static Login page with JavaServer Faces and JSP technology by importing the proper tag libraries, and adding HtmlGraphicImage and HtmlOutputText components. |
|
|
|