OGNL是Object Graph Navigation Language的缩写,与JSP,JSF相比,OGNL是一种功能非常强大的针对Java的表达式语言(EL),它可用来读取和更新Java对象的属性。
OGNL可以用在以下方面:
- 用做数据绑定语言用来绑定GUI元素(textfield, combobox等)到模型对象
- 用做数据源语言用来映射数据库表到表模型对象
- 用做数据绑定语言用来绑定web组件到数据模型(WebOGNL,Tapestry,WebWork等)
- 提供类似Jakarta Commons BeanUtils所提供的功能(读取Java对象的属性)
OGNL表达式语法:
Java标准类型:
bool类型:true,false
int类型:10, 0xABCD等
long类型:100L
float类型:1.0, 0.5F等
double类型:0.01D
char类型:'A', '\uFFFF'等
字符串类型:"Hello World!"
null
OGNL独自类型:
例:10.01B,相当于java.math.BigDecimal
例:100000H,相当于java.math.BigInteger
OGNL表达式中能使用的操作符号:
OGNL表达式中能使用的操作符基本跟Java里的操作符一样,除了能使用 +, -, *, /, ++, --, ==, !=, = 等操作符之外,还能使用 mod, in, not in等
变量的引用:
使用方法:#变量名
例:#this, #user.name
对静态方法或变量的访问:
@mypkg.MyClass@myVar
@mypkg.MyClass@myMethod()
读取变量值:
例:user.address.countryName
方法调用:
例:user.getName()
对象的创建:
new java.net.URL("http://localhost/")
List表达式例:
{"green", "red", "blue"}
Map表达式例:
#{"key1" : "value1", "key2" : "value2", "key3" : "value3"}
对map引用,例:map.key1
附:
OGNL使用例:
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;
public class OgnlTest extends TestCase {
public void testGetValue() throws Exception {
OgnlContext context = new OgnlContext();
Book book = new Book("book1");
context.put("book", book);
final String expression = "book.name";
Object parseExpression = Ognl.parseExpression(expression);
assertEquals("book1", Ognl.getValue(parseExpression, context));
book.setName("book2");
assertEquals("book2", Ognl.getValue(parseExpression, context));
}
public void testSetValue() throws Exception {
OgnlContext context = new OgnlContext();
Book book = new Book("book1");
context.put("book", book);
final String expression = "book.name";
Object parseExpression = Ognl.parseExpression(expression);
Ognl.setValue(parseExpression, context, "book2");
assertEquals("book2", book.getName());
}
public void testCallStaticMethod() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "@com.test.ognl.Book@test()";
Object parseExpression = Ognl.parseExpression(expression);
assertEquals("Hello World", Ognl.getValue(parseExpression, context));
}
public void testArray() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "new int[]{1, 2, 3}";
Object parseExpression = Ognl.parseExpression(expression);
int[] ret = (int[]) Ognl.getValue(parseExpression, context);
assertEquals(1, ret[0]);
assertEquals(2, ret[1]);
assertEquals(3, ret[2]);
}
public void testList() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "{1, 2, 3}";
Object parseExpression = Ognl.parseExpression(expression);
List ret = (List) Ognl.getValue(parseExpression, context);
assertEquals(new Integer(1), ret.get(0));
assertEquals(new Integer(2), ret.get(1));
assertEquals(new Integer(3), ret.get(2));
}
public void testMap() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "#{\"name\" : \"book1\", \"price\" : 10.2}";
Object parseExpression = Ognl.parseExpression(expression);
Map value = (Map) Ognl.getValue(parseExpression, context);
assertEquals("book1", value.get("name"));
assertEquals(new Integer(10.2), value.get("price"));
}
}
class Book {
private int name;
public Book(String bookName) {
this.name = bookName;
}
public int getName() {
return name;
}
public void setName(int Name) {
this.name = name;
}
// test static method
public static String hello() {
return "Hello World";
}
}
OGNL可以用在以下方面:
- 用做数据绑定语言用来绑定GUI元素(textfield, combobox等)到模型对象
- 用做数据源语言用来映射数据库表到表模型对象
- 用做数据绑定语言用来绑定web组件到数据模型(WebOGNL,Tapestry,WebWork等)
- 提供类似Jakarta Commons BeanUtils所提供的功能(读取Java对象的属性)
OGNL表达式语法:
Java标准类型:
bool类型:true,false
int类型:10, 0xABCD等
long类型:100L
float类型:1.0, 0.5F等
double类型:0.01D
char类型:'A', '\uFFFF'等
字符串类型:"Hello World!"
null
OGNL独自类型:
例:10.01B,相当于java.math.BigDecimal
例:100000H,相当于java.math.BigInteger
OGNL表达式中能使用的操作符号:
OGNL表达式中能使用的操作符基本跟Java里的操作符一样,除了能使用 +, -, *, /, ++, --, ==, !=, = 等操作符之外,还能使用 mod, in, not in等
变量的引用:
使用方法:#变量名
例:#this, #user.name
对静态方法或变量的访问:
@mypkg.MyClass@myVar
@mypkg.MyClass@myMethod()
读取变量值:
例:user.address.countryName
方法调用:
例:user.getName()
对象的创建:
new java.net.URL("http://localhost/")
List表达式例:
{"green", "red", "blue"}
Map表达式例:
#{"key1" : "value1", "key2" : "value2", "key3" : "value3"}
对map引用,例:map.key1
附:
OGNL使用例:
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;
public class OgnlTest extends TestCase {
public void testGetValue() throws Exception {
OgnlContext context = new OgnlContext();
Book book = new Book("book1");
context.put("book", book);
final String expression = "book.name";
Object parseExpression = Ognl.parseExpression(expression);
assertEquals("book1", Ognl.getValue(parseExpression, context));
book.setName("book2");
assertEquals("book2", Ognl.getValue(parseExpression, context));
}
public void testSetValue() throws Exception {
OgnlContext context = new OgnlContext();
Book book = new Book("book1");
context.put("book", book);
final String expression = "book.name";
Object parseExpression = Ognl.parseExpression(expression);
Ognl.setValue(parseExpression, context, "book2");
assertEquals("book2", book.getName());
}
public void testCallStaticMethod() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "@com.test.ognl.Book@test()";
Object parseExpression = Ognl.parseExpression(expression);
assertEquals("Hello World", Ognl.getValue(parseExpression, context));
}
public void testArray() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "new int[]{1, 2, 3}";
Object parseExpression = Ognl.parseExpression(expression);
int[] ret = (int[]) Ognl.getValue(parseExpression, context);
assertEquals(1, ret[0]);
assertEquals(2, ret[1]);
assertEquals(3, ret[2]);
}
public void testList() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "{1, 2, 3}";
Object parseExpression = Ognl.parseExpression(expression);
List ret = (List) Ognl.getValue(parseExpression, context);
assertEquals(new Integer(1), ret.get(0));
assertEquals(new Integer(2), ret.get(1));
assertEquals(new Integer(3), ret.get(2));
}
public void testMap() throws Exception {
OgnlContext context = new OgnlContext();
final String expression = "#{\"name\" : \"book1\", \"price\" : 10.2}";
Object parseExpression = Ognl.parseExpression(expression);
Map value = (Map) Ognl.getValue(parseExpression, context);
assertEquals("book1", value.get("name"));
assertEquals(new Integer(10.2), value.get("price"));
}
}
class Book {
private int name;
public Book(String bookName) {
this.name = bookName;
}
public int getName() {
return name;
}
public void setName(int Name) {
this.name = name;
}
// test static method
public static String hello() {
return "Hello World";
}
}