JSF 1.2 Components
Java developers and Web designers today need more powerful tools to deliver the richer,
faster, and smoother web experience that users now demand. JavaServer Faces is an
advanced web application framework that includes hundreds of powerful, feature-rich,
Ajax-enabled UI components that provide all of the functionality needed to build web
applications in a Web 2.0 world.
There has never been a better time to learn JSF. The JSF ecosystem is growing fast and
the abundance of JSF components, development tools, industry conferences, and job
opportunities is impressive. Learning JSF can be a challenge, but this book makes it easy
by showing you the most important JSF technologies and concepts that you need to know
to become a JSF professional.
What This Book Covers
Chapter 1: Standard JSF Components introduces you to the JavaServer Faces framework
and the key concepts that you need to understand to build simple JSF applications. You
will learn about Model-View-Controller, managed beans, the JSF expression language,
converters, and validators. You will also discover how to use the standard JSF user
interface components (such as text fields, radio buttons, selection lists, and checkboxes)
to receive text, date/time, numeric, and other types of input from users as well as handle
form submission, render messages, lay out components in a grid, and display a data table.
Chapter 2: Facelets Components introduces the Facelets view definition framework and
compares it to JSP as the view technology for JSF. You will learn about working with
valid XHTML documents and will see examples of how to use the Facelets framework to
create simple and complex composite user interfaces based on Facelets templates. You
will also learn how to use the Facelets component library to display debugging
information, iterate data, include and remove UI components and markup, pass
parameters between Facelets pages, create reusable view elements, and apply advanced
Facelets templating concepts.
Chapter 3: Apache Tomahawk Components covers the Apache Tomahawk component
library and looks at how to use Tomahawk components such as calendars, trees, a file
upload component, and navigation menus to solve common web development tasks. You
will learn how to use Tomahawk components to validate user input, accept date/time
input, upload files, render tree components, create navigation menus, implement user
interface security, display sortable data tables, and use newspaper layouts.
Chapter 4: Apache Trinidad Components discusses the Apache Trinidad framework and
Ajax technology and will introduce you to many of the 100 plus rich user interface
controls in this powerful component library. You will learn how to use color choosers,
pop-up calendars, dynamic trees, data tables, a number spinbox, shuttle components,
navigation menus, layout panels, and more, to implement typical web development use
cases. You will also learn how to use the Apache Trinidad dialog framework to add
dialog windows to your application, how to enable Trinidad's client-side JavaScript
validation, how to create dynamic navigation menus, how to design custom skins and
icons for Trinidad's skinning framework, and how to use Trinidad's partial page rendering
(PPR) Ajax feature to enhance your JSF pages.
Chapter 5: ICEfaces Components introduces the ICEfaces Ajax component library, and
explains many of the important concepts that you need to know in order to develop JSF
applications based on ICEfaces. You will learn how to use many of the more than 50
Ajax-enabled user interface components in the ICEfaces component library, such as how
to add dynamic effects to your pages to enhance input validation, how to use navigation
and context menus, how to work with tree components, how to render dynamic data
tables that support sorting and paging, how to render pie charts and bar graphs, how to
create a tabbed user interface, how to arrange elements using drag-and-drop, how to lay
out components in a grid, and how to work with modal dialogs.
Chapter 6: JBoss Seam Components covers the JBoss Seam framework and introduces
you to the fundamentals of building JSF applications that use the full Java Enterprise
Edition (Java EE) technology stack. You will learn how to configure Seam, how to apply
Seam annotations to Java classes, and how to use Seam JSF controls to bridge the gap
between Enterprise JavaBeans (EJB3) components, the Java Persistence API (JPA), and
the JSF framework. This chapter will show you how to validate user input efficiently
using Seam, JPA, and the Hibernate Validator framework. You will also discover how to
use the Seam tag library and Java API to display validation and success messages, render
required field decorations, display debugging information, use Seam's conversation
management feature to implement robust JSF workflows, and how to combine Seam with
JBoss RichFaces and Ajax4jsf to build next generation JSF applications.
Chapter 7: JBoss RichFaces and Ajax4jsf Components covers the JBoss RichFaces and
Ajax4jsf component libraries. You will learn how to use many advanced RichFaces
components such as in-place editable text, a calendar, an Ajax-based auto-complete
suggestion box, rich panel and menu components, a Google map component, a Microsoft
Virtual Earth component, dynamic data tables with sorting and paging, data grids, a color
picker, a slider component, a number spinner, a picklist control, a rich text editor, and
more. You will also learn how to add Ajax support to non-Ajax JSF components with
Ajax4jsf, and how to perform advanced Ajax tasks such as submitting forms
asynchronously, submitting one component at a time, polling the server, and re-rendering
parts of the page after an Ajax request.
Appendix: Learning JSF: Next Steps introduces JavaServer Faces 2.0 and provides a
summary of the key features in the next generation of the JSF framework. You will
discover how JSF 2.0 emphasizes convention over configuration by learning about the
new JSF annotations to simplify managed bean configuration and reduce XML, the new
JSF resource loading mechanism, the simplified navigation mapping convention, the
integration of Facelets into the core JSF framework, the new "composite" JSF tag library
for defining composite components, and the significantly improved support for Ajax that
is now built-in to the framework. You will also learn about PrimeFaces, a promising new
JSF component library.
Facelets Components
In the previous chapter, we looked at how to use standard JavaServer Faces
components to implement a number of common web development tasks, such as
rendering forms containing simple UI components and accepting and validating
input from users. We also discussed several features and extension points of the JSF
framework, such as the managed beans facility and message bundle support.
Indeed, the JSF framework was designed with extensibility in mind, enabling both
application and framework developers to leverage its infrastructure to build even
more sophisticated technologies on top of an already excellent platform for web
development. One of the most interesting extension points of the JSF framework is
the ViewHandler mechanism.
In this chapter, we will discuss one of the most important technologies in the JSF
ecosystem—the Facelets view definition framework. The Facelets framework
includes a pluggable ViewHandler implementation that provides an alternative to
JavaServer Pages (JSP). Facelets also includes a number of useful JSF tags that can be
used in place of the JavaServer Pages Standard Tag Library (JSTL).
To gain a better appreciation for the role of Facelets in the JSF context, we will begin
with a brief history of Java web development, paying attention to the similarities and
differences between Facelets and JSP. We will also look at how to configure a JSF
application to use Facelets.
The topics we will look at in this chapter include:
- Getting started with Facelets
- Rendering debug information
- Iterating data in a Facelets page
- Removing UI components and markup
- Including UI components and markup
- Passing parameters from one Facelet to another
- Rendering a UI composition
- Rendering a UI component
- Creating a Facelets UI composition template
- Decorating the user interface
- Rendering a UI fragment
A brief history of Java web development
Before we dive into the details of the Facelets framework, let's review a brief history
of web development on the Java platform to understand how Facelets fits into this
broader context.
Before Model-View-Controller (MVC) web frameworks and templating systems such
as Struts, Tiles, Tapestry, JSF, and Facelets, building web applications on the Java
platform involved writing Java Servlets and JavaServer Pages (JSPs).
JSP technology was introduced in 1999 as a competitor to Microsoft's Active
Server Pages (ASP) and PHP. JSP made it easier for Java developers to separate
programming logic from web page markup, and introduced the concept of custom
tag libraries. Developers could now write custom tag classes that could encapsulate
presentation logic, register them in a Tag Library Descriptor (TLD) file, and use
them declaratively in a JSP page simply by adding an import directive at the top of
the page.
The Struts framework was originally launched in 2000 and made extensive use of the
Servlet API, JSP, and the JSP custom tag mechanism. Struts applied the MVC design
pattern to the Servlet API, and introduced a class named the ActionServlet that
acted as the Front Controller of the framework. The Struts ActionServlet handled
all requests matching a particular URL pattern (usually *.do) by delegating request
processing to a number of application-defined action classes implementing the
Command pattern.
The Struts framework also introduced a number of custom tag libraries, which
provided useful functionality that was common to web development. Some of these
libraries included tags for working with JavaBeans, tags for working with HTML
elements, and tags for implementing presentation logic such as data iteration and
conditional rendering.
The popularity of the Struts framework in general and of the Struts custom tag
libraries in particular suggested a widespread need in the Java developer community
for tools to simplify the process of implementing Java web applications. This need
was recognized by the Java Community Process, and in 2002 the JSP Standard
Tag Library (JSTL) was released. It introduced standardized tag libraries for
implementing conditional logic, data iteration, date/time and number formatting,
internationalization, and more.
The JSTL also supported the JSP Expression Language (JSP EL), a compact scripting
language with a simple syntax that provided a less verbose and less error-prone
alternative to JSP scriptlets for dynamic rendering.
Due to the similarities between the JSTL and the Struts custom tag libraries, the
Struts documentation encouraged developers to adopt the JSTL over Struts tags,
whenever there was redundancy between the two libraries.
The Struts framework also supported a templating system known as "Tiles". Tiles
was an open source project that integrated into the Struts framework and had
a plug-in to support the definition of user interface screens based on the Composite
View design pattern.
The Composite View design pattern
Implementing web user interfaces often involves creating and reusing
a number of repeating branding and navigational elements, such as
organizational logos, headers, footers, and navigation menus. An effective
strategy for managing these repeating elements is to use the Composite
View design pattern. This pattern helps us to subdivide our user interface
into smaller pieces, and to create new views by "compositing" these pieces
back together in different ways.
As we will see, Facelets performs a role similar to Tiles in the JSF context, but
includes many other features as well.
When JSF 1.0 was released in 2004, it introduced a number of enhancements
over and above what the Struts framework was currently providing, such as
automatic type conversion of strings to and from other Java data types, direct
binding of user interface components to application domain models, and more.
The Struts framework can be described as an action-based MVC framework due to
its emphasis on action classes and the command pattern, while JSF can be described
as an event-driven, component-based MVC framework due to its emphasis on UI
components and the Observer pattern.
The Command pattern and the Observer pattern
Two popular software design patterns are the Command pattern and the
Observer pattern. The Command pattern emphasizes the use of objects
as executable actions. An object encapsulates a single action, typically by
implementing a callback interface method such as execute(), and is
invoked by a Controller object.
The Observer pattern is common in GUI programming and emphasizes
events and event handlers. Another name for the Observer pattern is
the Publisher/Subscriber model. A UI component such as a button
or hyperlink publishes an event (such as a mouse click), and an event
handler observes user interaction and processes the event.
The advantage of the Observer pattern is that a single object can manage
state (the Model) more easily and can handle multiple events for a single
user interface screen (the View). Also, a component can have multiple
event handlers registered with it. The Observer pattern is also used in
GUI toolkits such as Swing/AWT.
Like Struts, JSF is a highly extensible framework. JSF has a number of important
extension points, such as managed beans, converters, validators, lifecycle phase
listeners, UI components, render kits, and more. In JSF, even the view technology
itself can be swapped out for an alternative ViewHandler implementation.
|