Jump to content

WebObjects/Project WONDER/Frameworks/WOOgnl

From Wikibooks, open books for an open world

WebObjects WikiBooks pages considered deprecated

[edit | edit source]

Go to http://wiki.objectstyle.org/confluence/display/WO/Project+WONDER-Frameworks-WOOgnl instead.

Overview

[edit | edit source]

OGNL stands for "Object Graph Navigation Language", and it defines an entire family of key-value coding-like abilities. As Jonathan Rentzsch put it in his CAWUG presentation on Project WOnder, "Think: Key-Value Coding on Steroids". You can get more information on the specifics on OGNL at the official OGNL website.

WOOgnl provides a framework that integrates the OGNL syntax into WO's standard binding resolution. By simply including the WOOgnl framework on your build path and preceding your binding value with a "~", it will be interpreted by WOOgnl.

Provided your html markup is well-formed, you can also put ognl expressions in standard html tags if you set the following property:

ognl.parseStandardTags=true

Here are some examples that demonstrate just a tiny bit of the really cool things you can do:

  • value="~\"Hello Mr.\" + session.user.firstName";
  • value="~name.length().(#this>100?2*#this:20+#this)";
  • value="~#A=new NSMutableArray(),#A.addObject(name),#A";

Here are some examples provided by Max Muller, WOOgnl's original author:

 // Calling methods with arguments
 Repetition1: WORepetition {
       item = arrayItem; 
       list = "~sort(anArray, \"name\")"; 
 }
 // Calling static methods
 Repetition2: WORepetition {
       item = arrayItem; 
       list = "~@er.extensions.ERXArrayUtilities@sortedArraySortedWithKey(anArray, \"name\")"; 
 }
 // Accessing static ivars
 String1: WOString {
       value = "~@ognl.webobjects.WOOgnl@OgnlSpecialCharacters";
 }
 // Use of conditionals, note that every previous value of the . is
 // pushed into the ivar #this
 String2: WOString {
       value = "~name.length().(#this > 100? 2*#this : 20+#this)";
 }
 // String concat
 String3: WOString {
       value = "~\"Hello Max \" + name";
 }
 // Use of set operator in.  can also use in against NSArray and
 NSSet objects
 String4: WOString {
       value = "~name in {\"Main\", \"Something\"} ? \"Yes\" : \"No\"";
 }
 // Variable declaration.  Note that commas allow multiple actions
 // per expression.
 String5: WOString {
       value = "~#A=new com.webobjects.foundation.NSMutableArray(),#A.addObject(name), #A.addObjectsFromArray(session.languages), #A";
 }