Wiki source code of WOOgnl Framework
Last modified by chuckhill on 2012/08/27 13:18
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | == Overview == | ||
| 2 | |||
| 3 | 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>>url:http://commons.apache.org/ognl/||shape="rect"]]. | ||
| 4 | |||
| 5 | 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. | ||
| 6 | |||
| 7 | Here are some examples that demonstrate just a tiny bit of the really cool things you can do: | ||
| 8 | |||
| 9 | * value="~~\"Hello Mr.\" + session.user.firstName"; | ||
| 10 | * value="~~name.length().(#this>100?2*#this:20+#this)"; | ||
| 11 | * value="~~#A=new NSMutableArray(),#A.addObject(name),#A"; | ||
| 12 | |||
| 13 | Here are some examples provided by Max Muller, WOOgnl's original author: | ||
| 14 | |||
| 15 | {{code}}// Calling methods with arguments | ||
| 16 | Repetition1: WORepetition { | ||
| 17 | item = arrayItem; | ||
| 18 | list = "~sort(anArray, \"name\")"; | ||
| 19 | } {{/code}} | ||
| 20 | |||
| 21 | {{code}}// Calling static methods | ||
| 22 | Repetition2: WORepetition { | ||
| 23 | item = arrayItem; | ||
| 24 | list = "~@er.extensions.ERXArrayUtilities@sortedArraySortedWithKey(anArray, \"name\")"; | ||
| 25 | }{{/code}} | ||
| 26 | |||
| 27 | {{code}}// Accessing static ivars | ||
| 28 | String1: WOString { | ||
| 29 | value = "~@ognl.webobjects.WOOgnl@OgnlSpecialCharacters"; | ||
| 30 | }{{/code}} | ||
| 31 | |||
| 32 | {{code}}// Accessing static ivars within inner class | ||
| 33 | String1: WOString { | ||
| 34 | value = "~@ognl.webobjects.WOOgnl$MyInnerClass@OgnlSpecialCharacters"; | ||
| 35 | }{{/code}} | ||
| 36 | |||
| 37 | {{code}}// Use of conditionals, note that every previous value of the . is | ||
| 38 | // pushed into the ivar #this | ||
| 39 | String2: WOString { | ||
| 40 | value = "~name.length().(#this > 100? 2*#this : 20+#this)"; | ||
| 41 | }{{/code}} | ||
| 42 | |||
| 43 | {{code}}// String concat | ||
| 44 | String3: WOString { | ||
| 45 | value = "~\"Hello Max \" + name"; | ||
| 46 | } {{/code}} | ||
| 47 | |||
| 48 | {{code}}// Use of set operator in. can also use in against NSArray and | ||
| 49 | NSSet objects | ||
| 50 | String4: WOString { | ||
| 51 | value = "~name in {\"Main\", \"Something\"} ? \"Yes\" : \"No\""; | ||
| 52 | }{{/code}} | ||
| 53 | |||
| 54 | {{code}} | ||
| 55 | |||
| 56 | // Variable declaration. Note that commas allow multiple actions | ||
| 57 | // per expression. | ||
| 58 | String5: WOString { | ||
| 59 | value = "~#A=new com.webobjects.foundation.NSMutableArray(),#A.addObject(name), #A.addObjectsFromArray(session.languages), #A"; | ||
| 60 | } | ||
| 61 | |||
| 62 | {{/code}} | ||
| 63 | |||
| 64 | === Helper system === | ||
| 65 | |||
| 66 | For more detail, check [[doc:documentation.Home.WOLips Tutorials.WOOGNL Helper Functions.WebHome]]. With these two lines in your properties file and Project WONDER, | ||
| 67 | |||
| 68 | {{code}} | ||
| 69 | |||
| 70 | ognl.helperFunctions=true | ||
| 71 | ognl.inlineBindings=true | ||
| 72 | |||
| 73 | {{/code}} | ||
| 74 | |||
| 75 | You can declare your own helper class like this: | ||
| 76 | |||
| 77 | {{code}} | ||
| 78 | |||
| 79 | public class StringHelper { | ||
| 80 | public String capitalize(String str) { | ||
| 81 | // ... | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | {{/code}} | ||
| 86 | |||
| 87 | And use that in your binding by "piping your data" | ||
| 88 | |||
| 89 | {{code}} | ||
| 90 | |||
| 91 | <wo:str value="$person.name|capitalize" /> | ||
| 92 | |||
| 93 | {{/code}} |