Table Of ContentMastering JavaServer Faces 2.2
Anghel Leonard
Chapter No 1
"Dynamic Access to JSF Application Data
through Expression Language (EL 3.0)"
I n this package, you will find:
The author’s biography
A preview chapter from the book, Chapter no.1 "Dynamic Access to JSF Application
Data through Expression Language (EL 3.0)"
A synopsis of the book’s content
Information on where to buy this book
About the Author
Anghel Leonard is a senior Java developer with more than 13 years of experience in
Java SE, Java EE, and related frameworks. He has written and published more than 50
articles about Java technologies and more than 500 tips and tricks for many websites that
are dedicated to programming. In addition, he has written the following books:
• Tehnologii XML XML în Java, Albastra
• Jboss Tools 3 Developer's Guide, Packt Publishing
• JSF 2.0 Cookbook, Packt Publishing
• JSF 2.0 Cookbook: LITE, Packt Publishing
• Pro Java 7 NIO.2, Apress
• Pro Hibernate and MongoDB, Apress
Currently, Anghel is developing web applications using the latest Java technologies
on the market (EJB 3.0, CDI, Spring, JSF, Struts, Hibernate, and so on). Over the past
two years, he's focused on developing rich Internet applications for geographic
information systems.
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Mastering JavaServer Faces 2.2
This book will cover all the important aspects (Bi g Ticket features) involved in
developing JSF 2.2 applications. It provides clear instructions for getting the most
out of JSF 2.2 and offers many exercises (more than 300 complete applications) to
build impressive JSF-based web applications.
We start off with a chapter about Expression Language (EL) and cover the most
important aspects of EL 2.2 and EL 3.0. We continue with a comprehensive dissertation
about communication in JSF, followed by an exciting chapter about JSF 2.2 scopes. At
this point, we bring into discussion most of the JSF artifacts and configurations. Further,
we start a suite of very interesting topics, such as HTML5 and AJAX. After that we
dissect the JSF view state concept and learn how to deal with this delicate JSF topic.
Furthermore, we will discuss in detail about custom components and composite
components. After this, we will talk about library contracts (themes) of JSF 2.2
resources. Finally, the last chapter will fortify your knowledge about JSF 2.2 Facelets.
What This Book Covers
Chapter 1, Dynamic Access to JSF Application Data through Expression Language (EL
3.0), covers the main aspects of Expression Language (EL). We will cover EL 2.2 and EL
3.0, including new operators, lambda expressions, and collection object support.
Chapter 2, Communication in JSF, represents a dissection of JSF mechanisms used
for ensuring communication between JSF artifacts. Therefore, we will cover context
parameters, request parameters, JSF 2.2 actions on GET requests (view actions),
and more.
Chapter 3, JSF Scopes – Lifespan and Use in Managed Beans Communication, teaches
you to distinguish between the bad and good practices of using JSF and CDI scopes. We
will discuss JSF scopes versus CDI scopes, request, session, view scope (including the
new JSF 2.2 view scope), application, conversation scope, JSF 2.2 flow scope in detail
(Big Ticket feature), and more.
Chapter 4, JSF Configurations Using XML Files and Annotations – Part 1, depicts the
JSF artifact's configuration aspects in a learning-by-example fashion. Configuring JSF
artifacts in the file is pretty straightforward and boring, but if we
take each artifact and exploit its potential in several use cases, then things become much
more interesting.
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
C hapter 5, JSF Configurations Using XML Files and Annotations – Part 2, acts as a
continuation of the previous chapter. Here, we will discuss configuring resource handlers
(JSF 2.2's new context parameter),
configuring flash (JSF 2.2 FlashFactory, FlashWrapper, and flash system events), JSF 2.2
Window ID API, the injection mechanism (which, starting with JSF 2.2, is possible in
most JSF artifacts), and more.
Chapter 6, Working with Tabular Data, pays tribute to the < > tag. Here,
we will focus on the JSF 2.2 API (which supports the
interface in UIData). Moreover, we will learn about table pagination,
deleting/editing/updating table rows, filtering, and styling JSF tables.
Chapter 7, JSF and AJAX, exploits the JSF 2.2 attribute for queue control of
AJAX requests. It discusses how to reset value attributes using JSF 2.2 (input fields can
be updated with AJAX after a validation error), AJAX and JSF 2.2 flow scope, how to
customize AJAX script, and more. This is a classic chapter in almost any JSF book.
Chapter 8, JSF 2.2 – HTML5 and Upload, divides the topic into two parts. The first
part is entirely dedicated to the Big Ticket feature, HTML5, and JSF 2.2 (pass-through
attributes and elements). The second part is dedicated to JSF 2.2's new upload
component, < >.
Chapter 9, JSF State Management, provides a detailed dissertation about the JSF view
state. The headings of this chapter will refer to JSF's saving view state (including JSF 2.2
case insensitivity for state saving method and standardized server state serialization) and
JSF 2.2 stateless view (Big Ticket feature).
Chapter 10, JSF Custom Components, is another example of a classic chapter in any
JSF book. Obviously, the main topics are meant to shape the custom and composite
components creation. We will focus on developing several kinds of components based
on the new JSF 2.2 approach (Facelet's component tag can be declared via annotation).
Chapter 11, JSF 2.2 Resource Library Contracts – Themes, dedicates itself to the new
JSF 2.2 Resource Library Contracts feature (Big Ticket feature). You will learn how to
work with contracts, style JSF tables and UI components using contracts, style contracts
across different kind of devices, and more.
Chapter 12, Facelets Templating, depicts the viral aspects of Facelets templating. We
will focus on the declarative and programmatical aspects of Facelets.
Appendix, The JSF Life Cycle, covers a diagram of the different JSF phases.
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Dynamic Access to
JSF Application Data
through Expression
Language (EL 3.0)
Java Expression Language (EL) is a compact and powerful mechanism that enables
dynamic communication in JSP and JSF-based applications (including development
frameworks based on JSF such as PrimeFaces, ICEfaces, and RichFaces); we embed
expressions in the presentation layer to communicate with the application logic
layer. EL provides bidirectional communication, which means that we can expose
application logic data to the user, but we also can submit user data to be processes.
Generically speaking, EL can be used to populate HTTP requests with user data,
to extract and expose data from HTTP responses, to update HTML DOM, to
conditionally process data, and much more.
Commonly, EL expressions will be present in JSP and JSF pages, but
they can also appear outside, in faces-config.xml, for example.
In this chapter, you will see how to use EL in web pages to communicate with
managed beans, which is the most common case in JSF applications. We will
cover the following topics:
• EL syntax, operators, and reserved words
• EL immediate and deferred evaluation
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Dynamic Access to JSF Application Data through Expr ession Language (EL 3.0)
• EL value and method expressions
• The conditional text in JSF
• Write a custom EL resolver
EL syntax
In this section, you can see an overview of the main aspects of EL 2.2 and 3.0 syntax.
EL supports a handful of operators and reserved words. Each of these are quickly
described in the following section (more details are in the EL specifi cation document
(http://download.oracle.com/otndocs/jcp/el-3_0-fr-eval-spec/index.html)).
EL operators
EL supports the following categories of operators—arithmetic, relational, logical,
conditional, empty and added starting with EL 3.0, string concatenation, assignment
and semicolon operators:
Textuals Description Symbols
A + B Addition +
A - B Subtraction -
A * B Multiplication *
A {div, /} B Arithmetic operator division /, div
A {mod, %} B Arithmetic operator modulo %, mod
A {and, &&} B Logical AND &&, and
A {or, ||} B Logical OR ||, or
{not, !} A Logical opposite !, not
A {lt, <} B Relational less than <, lt
A {gt, >} B Relational greater than >, gt
A {le, <=} B Relational less than or equal to <=, le
A {ge, >=} B Relational greater than or equal to >=, ge
A {eq, ==} B Equal to ==, eq
A {ne, !=} B Not equal to !=, ne
A = B Assignment (EL 3.0) =
A ; B Semicolon (EL 3.0) ;
A += B String concatenation (EL 3.0) +=
A -> B Lambda expression (EL 3.0) ->
[ 8 ]
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Chapter 1
Textuals Description Symbols
empty A Determine whether a value is null or empty
A ? B : C Evaluates B or C, depending on the result of the evaluation ?:
of A. Known as the ternary operator.
Used when writing EL expressions .
Used when writing EL expressions []
EL precedence of operators
Conforming to EL specifi cation, the precedence of operators from the highest to
lowest, left to right is as follows:
• [].
• () (used to change the precedence of operators)
• - (unary) not ! empty
• * / div % mod
• + - (binary)
• +=
• < > <= >= lt gt le ge
• == != eq ne
• && and
• || or
• ? :
• -> (lambda expression)
• =
• ;
EL reserved words
EL defi nes the following reserved words:
• and, or, not, eq, ne, lt, gt, le, ge, true (Boolean literal), false
(Boolean literal), null, instanceof (a Java keyword to do a class
comparison between objects), empty, div, and mod
[ 9 ]
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Dynamic Access to JSF Application Data through Expr ession Language (EL 3.0)
E L immediate and deferred evaluation
EL evaluates expressions as immediate or deferred.
Immediate evaluation returns the result as soon as the page is fi rst rendered. These
kinds of expressions are read-only value expressions and they can be present only in
tags that accept runtime expressions. They are easy to recognize after the ${} notation.
Usually, they are used for arithmetic and logical operations in JSP pages.
Deferred evaluation can return the result at different phases of a page's life cycle
depending on the technology that is using the expression. JSF can evaluate the
expression at different phases of the life cycle (for example, during the rendering
and postback phase), depending on how the expression is being used in the page.
These kind of expressions can be value and method expressions, and they are
marked by the #{} notation.
In Facelets, ${} and #{} act the same.
EL value expressions
Value expressions are probably used the most, and they refer to objects and their
properties and attributes. Such expressions are dynamically used to evaluate results
or set bean properties at runtime. Through value expressions, you can easily access
JavaBeans components, collections, and Java SE enumerated types. Moreover, EL
provides a set of implicit objects that can be used to get attributes from different
scopes and parameter values. Furthermore, you will see how EL deals with each of
these objects.
Value expressions that can read data, but cannot write it are known
as rvalue (${} expressions are always rvalue), while those that can
read and write data are known as lvalue (#{} expressions can be
rvalue and/or lvalue).
Referencing a managed bean
Referencing a managed bean is not exactly a useful example, but it is a good point to
start. Most commonly, your managed bean will look like the following code (in this
case, the bean's class name is PlayersBean):
@ManagedBean
//some scope
[ 10 ]
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Chapter 1
public class PlayersBean{
...
}
Or, in the CDI version, your managed bean will be as follows:
@Named
//some scope
public class PlayersBean{
...
}
Or, with an explicit name, your managed bean will be as follows:
@ManagedBean(name = "myPlayersBean")
//some scope
public class PlayersBean{
...
}
@Named(value = "myPlayersBean")
//some scope
public class PlayersBean{
...
}
Now, for the fi rst two examples, EL refers to the PlayersBean managed bean, like
this—the name is obtained from taking the unqualifi ed class name portion of the
fully qualifi ed class name and converting the fi rst character to lowercase as follows:
#{playersBean}
In addition, for the next two examples, EL uses the explicit name as follows:
#{myPlayersBean}
You should use CDI beans whenever possible since they are
more fl exible than JSF managed beans, and because annotations
from javax.faces.bean will be deprecated in a future JSF
version. Therefore, the CDI ones are recommended.
[ 11 ]
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Dynamic Access to JSF Application Data through Expr ession Language (EL 3.0)
W hen the referenced managed bean cannot be found in any scope, a null value will
be returned.
Downloading the example code
You can download the example code fi les for all Packt books you have
purchased from your account at http://www.packtpub.com. If you
purchased this book elsewhere, you can visit http://www.packtpub.
com/support and register to have the fi les e-mailed directly to you.
Referencing a managed bean's properties
As is commonly known, managed beans usually contain private fi elds, which are
accessible through getter and setter methods as bean properties, and some public
methods that exploits these properties to serve different logic tasks.
EL expressions that can access these properties contain the dot or square brackets
notation, []. For example, let's suppose that the PlayersBean managed bean
contains two fi elds defi ned like the following lines:
private String playerName = "Rafael";
private String playerSurname = "Nadal";
EL can access these fi elds through their getter methods; therefore, you need to defi ne
them as shown in the following code:
public String getPlayerName() {
return playerName;
}
public String getPlayerSurname() {
return playerSurname;
}
Now, an expression that accesses the playerName property can use the dot
notation (.) to refer it, as shown in the following line of code:
#{playersBean.playerName}
Alternatively, this expression can use the square brackets notation, [], as shown in
the following line of code:
#{playersBean['playerName']}
[ 12 ]
For More Information:
www.packtpub.com/mastering-javaserver-faces-2-2/book
Description:JSF 2.0 Cookbook: LITE, Packt Publishing. • Pro Java 7 NIO.2, Apress .. Or, in
the CDI version, your managed bean will be as follows: @Named. //some scope.