Java Reference
In-Depth Information
Chapter 2
JavaServer Faces and Facelets
The release of Java EE 7 embodies incremental updates for the JavaServer Faces (JSF) API, making it easier to use
and more compliant with modern technology. The JSF web framework originated in the early 2000s, and the web
landscape has changed much since its inception. The incremental releases of JSF 1. x added more features to help
build out the framework, but JSF 1. x was still very much a work in progress that, although it worked fine by itself, was
even better when paired with other third-party frameworks, such as JBoss Seam. When Java EE 6 was released, the
JSF 2.0 framework became more self-sufficient and required no third-party frameworks to be fully functional. When
utilized along with the new EJB infrastructure that was part of Java EE 6, JSF 2.0 worked seamlessly but still had a few
rough edges. The JSF 2.1 release repaired bugs and helped smooth some of the rough edges that remained.
The JSF 2.2 release brings with it even more incremental updates that aim to complete the framework even
further, bringing it into alignment with the next generation of web applications using HTML5. This chapter covers
many of the new features that will be important to a wide range of developers. The updated release brings forth better
integration with HTML5, less XML configuration, better page flow, and more security.
View Actions
In JSF 2.1 and prior, it was difficult to invoke action methods within a managed bean unless they were bound to a
command component. Sometimes it makes sense to invoke a method when the page is loading, after the page has
been fully loaded, and so on. In the past, this was done by using a preRenderView event listener, which causes the
method bound within a managed bean to be invoked before the view is rendered. Utilization of the preRenderView
event listener works, but it does not provide the level of control that is required to invoke a method during different
phases of the view life cycle. The preRenderView event listener also requires developers to programmatically check
the request type and work with the navigation handler.
In the JSF 2.2 release, you can use a new technique to invoke action methods within a managed bean during
specified life-cycle events that occur within the view. A new tag, f:viewAction , can be bound to a view, and it can
be incorporated into the JSF life cycle in both non-JSF (initial) and JSF (postback) requests. To use the tag, it must be
a child of the metadata facet. View parameters can be specified within the metadata facet as well, and they will be
available from within the managed bean when the action method is invoked.
The following simple JSF view demonstrates the use of the viewAction , because it will cause the
viewActionManagedBean.viewActionExample() method to be invoked before the view is built:
<html xmlns=" http://www.w3.org/1999/xhtml "
xmlns:h=" http://xmlns.jcp.org/jsf/html "
xmlns:f=" http://xmlns.jcp.org/jsf/core "
xmlns:ui=" http://xmlns.jcp.org/jsf/facelets " >
<h:head>
<title>Chapter 2 - ViewAction Example</title>
</h:head>
 
Search WWH ::




Custom Search