Supporting OGNL in Tag Libraries
Adding support for JSTL's Expression Language was fairly easy to do in the Display Tag. It was as simple as adding an ExpressionEvaluator and subclassing the ColumnTag and the TableTag. From there, all that was needed was needed a new TLD to reference these new classes.
So the question is - would it be just as easy to support the OGNL Expression Language? Does it have an ExpressionEvaluator I can write with the same simplicity as the JSTL version? If I could figure that out, I think creating OGNL-aware versions of the displaytag and struts-menu would be a piece of cake. All you'd need to do would be to change you're TLD's URI and you'd be off to the races!
Posted by Jason Carreira on February 26, 2004 at 01:04 AM MST #
Posted by James Strachan on February 26, 2004 at 06:16 AM MST #
There are a couple of way to execute an OGNL expression, depending on how much information you want to cache for efficiency. Also OGNL supports "get" and "set" operations; as such OGNL is a binding language rather than just an expression language. Of course it also serves admirably as an expression language for the purpose to which you are putting it.
The simplest way is to use the ognl.Ognl class to evaluate the expression against a root object:
This will parse then evaluate the expression against the given root object.
For greater efficiency you can pre-compile the expression using the parseExpression() method:
For repeated evaluations (such as you might have in a JSP tag that evaluates many times), you definitely want to cache the parsed expression. For your immediate purposes this will be a good test of how well it works within your framework.
This is just the simplest way of using OGNL. There is also a "context" object that allows you to put variable values in scope, and also track the current state of the expression evaluation.
There are many extension points that allow you to control how values are extracted from objects that you will definitely want to investigate:
Please feel free to ask any further questions you have to either me or the OGNL mailing list (see http://www.ognl.org for details)
Posted by Drew Davidson on February 27, 2004 at 04:33 PM MST #