WebObjects/Project WONDER/Frameworks/WOOgnl
外觀
轉到http://wiki.objectstyle.org/confluence/display/WO/Project+WONDER-Frameworks-WOOgnl。
OGNL 代表“物件圖導航語言”,它定義了一整個系列類似於鍵值編碼的功能。正如 Jonathan Rentzsch 在其關於 Project WOnder 的 CAWUG 演示中所說,“想想:類固醇上的鍵值編碼”。您可以在OGNL 官方網站上獲得有關 OGNL 細節的更多資訊。
WOOgnl 提供了一個框架,該框架將 OGNL 語法整合到 WO 的標準繫結解析中。只需將 WOOgnl 框架包含在您的構建路徑上並在繫結值之前加上“~”,它將由 WOOgnl 解釋。
如果您的 html 標記格式正確,您也可以將 ognl 表示式放在標準 html 標記中,前提是您設定了以下屬性
ognl.parseStandardTags=true
以下是一些示例,展示了您可以使用的一些非常酷的功能
- value="~\"Hello Mr.\" + session.user.firstName";
- value="~name.length().(#this>100?2*#this:20+#this)";
- value="~#A=new NSMutableArray(),#A.addObject(name),#A";
以下是一些由 WOOgnl 的最初作者 Max Muller 提供的示例
// 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";
}