OGNL是Object Graph Navigation Language的缩写,是一种功能非常强大的面向Java的El(El ),与JSP和JSF相比,可用于读取和更新Java对象的属性。
OGNL可以用在以下方面:-用作数据绑定语言,用于将GUI元素(textfield、combobox等)绑定到模型对象
用作数据源语言,用于将数据库表映射到表模型对象
-用作数据绑定语言,用于将web组件绑定到数据模型(WebOGNL、Tapestry、WebWork等)
提供类似于Jakarta Commons BeanUtils提供的功能。 读取Java对象的属性
http://www.Sina.com/http://www.Sina.com /
bool类型: true,false
int类型: 10,0x ABCD等
长型: 100L
浮动类型: 1.0、0.5f等
双精度类型: 0.01D
char类型:“a”、“uFFFF”等
字符串类型:“Hello World! ”
空值
OGNL表达式语法:
例如: 10.01B,相当于java.math.BigDecimal
例如: 100000H,相当于java.math.BigInteger
Java标准类型:
可以在OGNL表达式中使用的运算符基本上与Java的运算符相同,除了可以使用、、==! 除了=、=等运算符外,还可以使用mod、in、not in等
OGNL独自类型:
使用方法: #变量名
示例#this,#user.name
OGNL表达式中能使用的操作符号:
@mypkg.MyClass@myVar
@mypkg.MyClass@myMethod (
变量的引用:
示例: user.address.countryName
对静态方法或变量的访问:
示例: user.getName () )
读取变量值:
new Java.net.URL (' http://localhost/' ) )。
方法调用:
{'green ',' red ',' blue'}
对象的创建:
#{'key1' : 'value1',' key2' : 'value2',' key3' : 'value3'}
对map的引用,例如map.key1
等等。
List表达式例:官方主页:
http://www.ognl.org/
OGNL官方文档(2.6.9)。
语言指南(2.6.9) )。
附件:
Map表达式例:使用案例:
package com.test.ognl; import java.util.HashMap; import java.util.List; import java.util.Map; import junit.framework.TestCase; import ognl.Ognl; import ognl.OgnlContext; publicclassognltestextendstestcase { publicvoidtestgetvalue () throwsexception ) ognlcontextcontext=newognlcontext ); bookbook=newbook('book1 ' ); context.put('book ',book ); finalstringexpression=' book.name '; objectparseexpression=ognl.parse expression (expression ); assertequals('book1 ',ognl.getvalue ) (parseexpression,context ); book.setname(book2 ); assertequals('book2 ',ognl.getvalue ) (parseexpression,context ); } publicvoid testSetValue () throwsexception ) ognlcontextcontext=newognlcontext ); bookbook=newbook('book1 ' ); context.put('book ',book ); finalstringexpression=' book.name '; objectparseexpression=ognl.parse expression (expression ); ognl.setvalue(parseexpression,context,' book2 ' ); assertequals('book2 ',book.getName ); } publicvoidtestcallstaticmethod (throws exception ) ognlcontextcontext=newognlcontext; finalstringexpression=' @ com.test.ognl.book @ test '; objectparseexpression=ognl.parse expression (expression ); assertequals('Helloworld ',ognl.getvalue ) (parseexpression,context ); } publicvoid testArray () throwsexception ) ognlcontextcontext=newognlcontext; finalstringexpression=' new int [ ] { 1,2,3 } '; objectparseexpression=ognl.parse expression (expression ); int[]ret=(int[] ) ognl.getvalue(parseexpression,context ); assertequals(1,ret(0); assertequals(2,ret(1); 资产评估(3,ret(2); } publicvoid testList () throwsexception ) ognlcontextcontext=newognlcontext; finalstringexpression=' { 1,2,3 } '; objectparseexpression=ognl.parse expression (expression ); listret=(list ) ognl.getvalue(parseexpression,context ); 资产质量(new integer (1),ret.get(0) ); 资产质量(new integer (2),ret.get(1) )1); 资产质量(new integer (3),ret.get(2) ); } publicvoidtestmap (throws exception ) ognlcontextcontext=newognlcontext; finalstringexpression=' # {' name' :' book1','price' : 10.2} ' '; objectparseexpression=ognl.parse expression (expression ); Map value=(Map ) ognl.getvalue(parseexpression,context ); assertequals(book1 ),value.get ) ) name ); 资产质量(new integer (10.2 ),value.get ) ' price ); } } class Book { privateint name; publicbook(stringbookname ) { this.name=bookName; } publicint getName () { return name; }publicvoidsetname(intname ) { this.name=name; //teststaticmethodpublicstaticstringhello () { return'Hello World ); }