Use Commons JEXL to evaluate an expression containing references
to bean properties. To reference properties of an object, create an
expression using the bean property syntax introduced in Chapter 3. Surround each property reference with
curly braces and a leading $
, as in
the following example:
Common Java Cookbook DocBook XML Content was composed by ${opera.composer} in ${opera.year}.
Use the following code to "merge" an instance of the Opera
bean with the above expression:
import org.apache.commons.jexl.Expression; import org.apache.commons.jexl.ExpressionFactory; import org.apache.commons.jexl.JexlContext; import org.apache.commons.jexl.JexlHelper; Opera opera = new Opera( ); opera.setName("The Magic Flute"); opera.setComposer("Mozart"); opera.setYear(1791); String expr = "Common Java Cookbook DocBook XML Content was composed by ${opera.composer} in " + "${opera.year}."; Expression e = ExpressionFactory.createExpression( expr ); JexlContext jc = JexlHelper.createContext( ); jc.getVars( ).put("opera", opera); String message = (String) e.evaluate(jc); System.out.println( message );
This code puts an instance of the Opera
bean in a JexlContext
and evaluates the expression,
producing the following output:
The Magic Flute was composed by Mozart in 1791.
The previous example creates and populates an instance of the
Opera
bean: opera
. An Expression
object is then created by passing a
String
containing a JEXL expression
to ExpressionFactory.createExpression(
)
. A JexlContext
is created
with a call to JexlHelper.createContext(
)
. This context contains a map-like structure that holds named
variables, and the Opera
object is
added to the JexlContext
under the
name opera
. Once an Expression
and a JexlContext
are created and populated,
expression.evaluate( )
merges the
expression with the context, and a message is generated. A JEXL
expression evaluates to an Object
,
but, in this case, the expression evaluates to a String
object, and the results of the
evaluation are cast to a String
.
This simple example sets the stage for a majority of recipes in
this chapter; most templating engines involve the pattern established by
the previous example. First, there is a template—in this case, a
String
that contains expressions that
are to be replaced by bean properties. Second, a collection of named
variables are put into a context. Lastly, the template is merged with
the variables in the context.
This chapter makes heavy use of expressions in templating engines, and this is a logical time to introduce the concept of the syntax used to create expressions. If you are familiar with JavaServer Pages (JSPs) you may know about a tag library called the JavaServer Pages Standard Tag Library (JSTL), which is a set of standard JSP tag libraries that make creating JSP pages much easier. Along with JSTL came something called EL, which is an expression language you can use in JSTL tags and in JSP 2.0. In general, JEXL can do everything that JSP EL can do, and more. Table 9-1 lists expressions that are valid in both JSP 2.0 EL and Commons JEXL.
Table 9-1. Simple Commons JEXL and JSP 2.0 EL expressions
EL expression |
Evaluates to |
---|---|
|
|
|
The integer |
|
The |
|
|
|
|
|
|
|
|
|
|
|
Retrieves the value of |
|
Retrieves the object stored under the key "circle"
on the |
|
Retrieves the object at index 54 of a |
|
Evaluates to |
|
|
For more information about JSP 2.0 Expression Language, see Hans Bergsten's article on onJava.com called "JSP 2.0: The New Deal, Part 1" (http://www.onjava.com/pub/a/onjava/2003/11/05/jsp.html) or his book JavaServer Pages (O'Reilly).