Wiki source code of Project WONDER-Frameworks-WOOgnl
Version 16.1 by yllan on 2009/06/30 02:51
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>>http://www.opensymphony.com/ognl/]]. | ||
| 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}} | ||
| 16 | |||
| 17 | // Calling methods with arguments | ||
| 18 | Repetition1: WORepetition { | ||
| 19 | item = arrayItem; | ||
| 20 | list = "~sort(anArray, \"name\")"; | ||
| 21 | } | ||
| 22 | |||
| 23 | {{/code}} | ||
| 24 | |||
| 25 | {{code}} | ||
| 26 | |||
| 27 | // Calling static methods | ||
| 28 | Repetition2: WORepetition { | ||
| 29 | item = arrayItem; | ||
| 30 | list = "~@er.extensions.ERXArrayUtilities@sortedArraySortedWithKey(anArray, \"name\")"; | ||
| 31 | } | ||
| 32 | |||
| 33 | {{/code}} | ||
| 34 | |||
| 35 | {{code}} | ||
| 36 | |||
| 37 | // Accessing static ivars | ||
| 38 | String1: WOString { | ||
| 39 | value = "~@ognl.webobjects.WOOgnl@OgnlSpecialCharacters"; | ||
| 40 | } | ||
| 41 | |||
| 42 | {{/code}} | ||
| 43 | |||
| 44 | {{code}} | ||
| 45 | |||
| 46 | // Use of conditionals, note that every previous value of the . is | ||
| 47 | // pushed into the ivar #this | ||
| 48 | String2: WOString { | ||
| 49 | value = "~name.length().(#this > 100? 2*#this : 20+#this)"; | ||
| 50 | } | ||
| 51 | |||
| 52 | {{/code}} | ||
| 53 | |||
| 54 | {{code}} | ||
| 55 | |||
| 56 | // String concat | ||
| 57 | String3: WOString { | ||
| 58 | value = "~\"Hello Max \" + name"; | ||
| 59 | } | ||
| 60 | |||
| 61 | {{/code}} | ||
| 62 | |||
| 63 | {{code}} | ||
| 64 | |||
| 65 | // Use of set operator in. can also use in against NSArray and | ||
| 66 | NSSet objects | ||
| 67 | String4: WOString { | ||
| 68 | value = "~name in {\"Main\", \"Something\"} ? \"Yes\" : \"No\""; | ||
| 69 | } | ||
| 70 | |||
| 71 | {{/code}} | ||
| 72 | |||
| 73 | {{code}} | ||
| 74 | |||
| 75 | // Variable declaration. Note that commas allow multiple actions | ||
| 76 | // per expression. | ||
| 77 | String5: WOString { | ||
| 78 | value = "~#A=new com.webobjects.foundation.NSMutableArray(),#A.addObject(name), #A.addObjectsFromArray(session.languages), #A"; | ||
| 79 | } | ||
| 80 | |||
| 81 | {{/code}} | ||
| 82 | |||
| 83 | === Helper system === | ||
| 84 | |||
| 85 | For more detail, check [[http://wiki.objectstyle.org/confluence/display/WOL/WOOGNL+Helper+Functions]]. With these two lines in your properties file and Project WONDER, | ||
| 86 | |||
| 87 | {{code}} | ||
| 88 | |||
| 89 | ognl.helperFunctions=true | ||
| 90 | ognl.inlineBindings=true | ||
| 91 | |||
| 92 | {{/code}} | ||
| 93 | |||
| 94 | You can declare your own helper class like this: | ||
| 95 | |||
| 96 | {{code}} | ||
| 97 | |||
| 98 | public class StringHelper { | ||
| 99 | public String capitalize(String str) { | ||
| 100 | // ... | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | {{/code}} | ||
| 105 | |||
| 106 | And use that in your binding by "piping your data" | ||
| 107 | |||
| 108 | {{code}} | ||
| 109 | |||
| 110 | <wo:str value="$person.name|capitalize" /> | ||
| 111 | |||
| 112 | {{/code}} |