先转一篇介绍
官网上的资料比较精炼,例子很少,还是写一些测试代码来验证用法:
包装一下:
package org.airlinkmatrix.test.ognl;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
public class OgnlExpression {
private static final OgnlContext DEFAULT_CONTEXT = (OgnlContext)Ognl.createDefaultContext(null);
private Object expression;
private OgnlContext context;
public void setContext(OgnlContext context) {
this.context = context;
}
public OgnlContext getContext() {
if (context==null){
context = DEFAULT_CONTEXT;
}
return context;
}
public OgnlExpression(String expressionString) throws OgnlException
{
super();
expression = Ognl.parseExpression(expressionString);
}
public Object getExpression()
{
return expression;
}
public Object getValue(Object rootObject) throws OgnlException
{
return Ognl.getValue(getExpression(), getContext(), rootObject);
}
public void setValue(Object rootObject, Object value) throws OgnlException
{
Ognl.setValue(getExpression(), getContext(), rootObject, value);
}
}
测试类:
package org.airlinkmatrix.test.ognl;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.airlinkmatrix.test.ognl.OgnlExpression;
import junit.framework.TestCase;
import ognl.OgnlException;
public class OgnlExpressionTest extends TestCase {
class Root{
private Object[] array;
public Object[] getArray() {
return array;
}
public void setArray(Object[] array) {
this.array = array;
}
private Vector vector;
public Vector getVector() {
return vector;
}
public void setVector(Vector vector) {
this.vector = vector;
}
public Root() {
vector = new Vector();
vector.add(new Part("Michael","002"));
vector.add(new Part("Alex","001"));
vector.add(new Part("Joseph","003"));
vector.add(new Part("Alex","004"));
array = new Part[4];
array[0] = new Part("Michael","002");
array[1] = new Part("Alex","001");
array[2] = new Part("Joseph","003");
array[3] = new Part("Alex","004");
}
}
class Part{
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private String name;
public Part(String name,String id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
// return "name='"+name+"',id='"+id+"'";
return name;
}
public boolean equals(Object obj) {
if (!(obj instanceof Part)){
return false;
}
Part anotherPart = (Part)obj;
if (this.name==null||this.id==null){
return false;
}
if (!this.name.equals(anotherPart.getName())){
return false;
}
if (!this.id.equals(anotherPart.getId())){
return false;
}
return true;
}
}
private Root ROOT;
protected void setUp() throws Exception {
super.setUp();
ROOT = new Root();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testGetValue_ROOT_vector() {
try {
OgnlExpression expression = null;
ArrayList valueList = null;
//?号表示匹配所有记录,
expression = new OgnlExpression("vector.{? #this.name == 'Alex' }");
valueList = (ArrayList)expression.getValue(ROOT);
assertEquals(new Part("Alex","001"),valueList.get(0));
assertEquals(new Part("Alex","004"),valueList.get(1));
//^号表示匹配第一条记录
expression = new OgnlExpression("vector.{^ #this.name == 'Alex' }");
valueList = (ArrayList)expression.getValue(ROOT);
assertEquals(new Part("Alex","001"),valueList.get(0));
//$号表示匹配最后一条记录
expression = new OgnlExpression("vector.{$ #this.name == 'Alex' }");
valueList = (ArrayList)expression.getValue(ROOT);
assertEquals(new Part("Alex","004"),valueList.get(0));
//?号匹配多条记录,所以有可能选到第二条记录
expression = new OgnlExpression("vector.{? #this.name == 'Alex' }[1].id");
assertEquals("004",expression.getValue(ROOT));
//可以接着取记录属性
expression = new OgnlExpression("vector.{^ #this.name == 'Alex' }[0].id");
assertEquals("001",expression.getValue(ROOT));
try{
//^号匹配单条记录,第二条记录一定是空的,会报IndexOutOfBoundsException
expression = new OgnlExpression("vector.{^ #this.name == 'Alex' }[1].id");
expression.getValue(ROOT);
fail("expect IndexOutOfBoundsException");
}catch (IndexOutOfBoundsException ex){
}catch (Exception otherEx){
fail("expect IndexOutOfBoundsException");
}
try{
//^号匹配单条记录,如果匹配不到数据,仍然会报IndexOutOfBoundsException
//这个与官网的文档是有出入的,^号并不能避免IndexOutOfBoundsException
expression = new OgnlExpression("vector.{^ #this.name == 'Wrong Name' }[0].id");
expression.getValue(ROOT);
fail("expect IndexOutOfBoundsException");
}catch (IndexOutOfBoundsException ex){
}catch (Exception otherEx){
fail("expect IndexOutOfBoundsException");
}
} catch (OgnlException e) {
e.printStackTrace();
fail("oops!");
}
}
public void testGetValue_ROOT_array() {
try {
OgnlExpression expression = null;
ArrayList valueList = null;
//?号表示匹配所有记录,
expression = new OgnlExpression("array.{? #this.name == 'Alex' }");
valueList = (ArrayList)expression.getValue(ROOT);
assertEquals(new Part("Alex","001"),valueList.get(0));
assertEquals(new Part("Alex","004"),valueList.get(1));
//^号表示匹配第一条记录
expression = new OgnlExpression("array.{^ #this.name == 'Alex' }");
valueList = (ArrayList)expression.getValue(ROOT);
assertEquals(new Part("Alex","001"),valueList.get(0));
//$号表示匹配最后一条记录
expression = new OgnlExpression("array.{$ #this.name == 'Alex' }");
valueList = (ArrayList)expression.getValue(ROOT);
assertEquals(new Part("Alex","004"),valueList.get(0));
//?号匹配多条记录,所以有可能选到第二条记录
expression = new OgnlExpression("array.{? #this.name == 'Alex' }[1].id");
assertEquals("004",expression.getValue(ROOT));
//可以接着取记录属性
expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[0].id");
assertEquals("001",expression.getValue(ROOT));
try{
//^号匹配单条记录,第二条记录一定是空的,会报IndexOutOfBoundsException
expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[1].id");
expression.getValue(ROOT);
fail("expect IndexOutOfBoundsException");
}catch (IndexOutOfBoundsException ex){
}catch (Exception otherEx){
fail("expect IndexOutOfBoundsException");
}
try{
//^号匹配单条记录,如果匹配不到数据,仍然会报IndexOutOfBoundsException
//这个与官网的文档是有出入的,^号并不能避免IndexOutOfBoundsException
expression = new OgnlExpression("array.{^ #this.name == 'Wrong Name' }[0].id");
expression.getValue(ROOT);
fail("expect IndexOutOfBoundsException");
}catch (IndexOutOfBoundsException ex){
}catch (Exception otherEx){
fail("expect IndexOutOfBoundsException");
}
} catch (OgnlException e) {
e.printStackTrace();
fail("oops!");
}
}
public void testSetValue_ROOT_Normal() {
try {
OgnlExpression expression = null;
expression = new OgnlExpression("array.{^ #this.name == 'Alex' }[0].id");
expression.setValue(ROOT,"007");
assertEquals("007",((Part)ROOT.getArray()[1]).getId());
} catch (OgnlException e) {
e.printStackTrace();
fail("oops!");
}
}
}
读取、设置对象图内部的一个值没有多大问题了。但,ognl 组件是否能够配置为设置列表中每一个元素的某字段为同一值呢?
例如,对象图如下:
ROOT
Part
id="1"
Part
id="2"
Part
id="3"
其中ROOT拥有Part的一个列表(java.util.List)
有没有一种写法,能利用ognl表达式一次将上述对象的值改为:
ROOT
Part
id="007"
Part
id= "007"
Part
id= "007"
在ognl语言参考中没有找到。
尝试自己扩展一个吧。