Search from Struts2 Library

Thursday, September 25, 2008

OGNL 5

<--PREVIOUS

Advanced expression language features

As we’ve indicated, OGNL is a full-featured expression language. In fact, its features rival that of some fullfledged programming languages. In this section, we give a brief summary of some of the advanced features that you might use in a pinch. Some of these things are basic features of OGNL, but advanced in the context of Struts 2 usage. Take our terminology with a grain of salt. Also, we’ll make little effort to introduce use cases for these features. We consider their usage to be nonstandard practice. With that said, we also know that these power tools can save the day on those certain occasions that always seem to occur.

LITERALS AND OPERATORS

Like most languages, the OGNL expression language supports a wide array of literals. Table below summarizes these literals.

The only thing out of the ordinary would be the usage of both single and double quotes for string literals. Note, however, that a string literal of a single character must use double quotes, or it’ll be interpreted as a char literal. Table below shows the operators.


As you can see, all the usual suspects are here. This would probably be a good time to note that the OGNL expression language also allows multiple comma-separated expressions to be linked in a single expression. The following snippet demonstrates this process:
user.age = 10, user.name = "chad", user.username

This relatively meaningless example demonstrates an expression that links three subexpressions. As with many languages, each of the first two expressions executes and passes control on to the next expression. The value returned by the last expression is the value returned for the entire expression. Now we’ll see how to invoke methods with OGNL.

CALLING METHODS

One power that many a JSP developer has wished for is the ability to call methods from the expression language. Until recently, this was rare. Actually, even the simplest property reference involves a method call. But those simple property references can invoke methods based on JavaBeans conventions. If the method you want to invoke doesn’t conform to JavaBeans conventions, you’ll probably need the OGNL method invocation syntax to get to it. This can sometimes get you out of a jam. It can also be useful in calling utility methods on helper beans. Table below shows how it works


Note that in this table we assume that a random number generator bean, named utilityBean, has been pushed onto the ValueStack prior to the evaluation of these OGNL expressions. With this bean in place, you can omit the object name in the OGNL expression, because it resolves to the ValueStack by default. First, we invoke the makeRandomNumber() method as you might expect. In the second example, we show that you can even use a full method invocation syntax to access JavaBeans properties, though you don’t have to. The result is no different than when using the simpler property notation.

We should note that these method invocation features of the OGNL expression language are turned off during the incoming phase of Struts 2 data transfer. In other words, when the form input field names are evaluated by the params interceptor, method invocations, as well as some other security-compromising features of the expression language, are completely ignored. Basically, when the params interceptor evaluates OGNL expressions, it’ll only allow them to point to properties onto which it should inject the parameter values. Nothing else is permitted.

ACCESSING STATIC METHODS AND FIELDS

In addition to accessing instance methods and properties, you can also access static methods and fields with the OGNL expression language. There are two ways of doing this. One requires specifying the fully qualified class name, while the other method resolves against the ValueStack. The syntax that takes the full class name is @[fullClassName]@[property or methodCall]. Here are examples of using full class names to access both a static property and a static method:

@manning.utils.Struts2PortfolioConstants@USER
@manning.utils.PortfolioUtilityBean@startImageWrapper()

Besides the @ signs, these are no different than normal property specification or method invocation. As we said, you can forgo specifying the class name if and only if your property or method will resolve on the ValueStack. Here we have the same two examples, but they assume that some object on the ValueStack exposes what they need. The syntax replaces the class name with the vs symbol, which stands for ValueStack:

@vs@USER
@vs@startImageWrapper()

That wraps up our coverage of some of the advanced features of OGNL. You’ll probably find yourself coming back to this quick reference in the future as you butt heads with some odd wall or two. Again, we recommend taking it easy on the OGNL power tools. However, we’re compelled to tell you that OGNL contains even more powerful features than we’ve felt comfortable divulging. For the full details, we refer you directly to the primary OGNL documentation found at www.ognl.org.

<--PREVIOUS

No comments: