OGNL Hello World in Java

Veera picture Veera · Apr 24, 2009 · Viewed 15.1k times · Source

I need to use OGNL for reading some properties from Java object. OGNL is completely new thing to me. The documentation available for OGNL is OGNL's website is really confusing to me.

So anyone can provide a simple HelloWorld example for using OGNL (or any link to a tutorial is also helpful).

Answer

paweloque picture paweloque · Nov 12, 2009

Try this:

    Dimension d = new Dimension(2,2);

    String expressionString = "width";
    Object expr = Ognl.parseExpression(expressionString);

    OgnlContext ctx = new OgnlContext();
    Object value = Ognl.getValue(expr, ctx, d);

    System.out.println("Value: " + value);