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官方首頁:http://www.ognl.org/OGNL官方文檔 (2.6.9)OGNL Language Guide (2.6.9)附:
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; ??
- ???? } ??
- ??
- ??????
- ????public static String hello() { ??
- ????????return "Hello World"; ??
- ???? }??
posted on 2009-08-12 18:19
jadmin 閱讀(108)
評論(0) 編輯 收藏