9.2. Using an Expression Language

Problem

You need to parameterize text messages with variables and bean properties.

Solution

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:

${opera.name} 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 = 
    "${opera.name} 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.

Discussion

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 Jakarta Commons JEXL.

Table 9-1. Simple Commons JEXL and JSP 2.0 EL expressions

EL expression

Evaluates to

${true}

true

${1}

The integer 1

${'Hello'}

The String “Hello”

${'blah' == 'blah'}

true

${var == true}

true if the variable named var is a Boolean true

${x.a < 352}

true if x.getA( ) is a number less than 352

${ball.color == 'Green'}

true if ball.getColor( ) equals “Green”

${x == 2 || y == 4}

true if x equals 2 or if y equals 4

${thing.color.name}

Retrieves the value of thing.getColor( ).getName( )

${thing.shapes["circle"]}

Retrieves the object stored under the key “circle” on the Map shapes, which is a bean property of thing

${thing.someList[55]}

Retrieves the object at index 54 of a List, someList, which is a bean property of thing

${empty var}

Evaluates to true if var is null or empty

${!(y < 4)}

true if y is not less than 4

See Also

For more information about JSP 2.0 Expression Language, see Hans Bergsten’s article on http://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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.4.179