package com.test.ognl;
import java.util.List;
import ognl.Ognl;
import ognl.OgnlContext;
public class TestOgnl {
public static void main(String []a) throws Exception {
// TestOgnl.testOgnl();
// testOgnlStatic();
testListOgnl();
}
public static void testOgnl() throws Exception {
OgnlContext content = new OgnlContext();
Person p = new Person();
p.setName("生");
content.put("personAlias", p);
String expression = "personAlias.name";
Object obj = Ognl.parseExpression(expression);
System.out.println(Ognl.getValue(obj, content).toString());
}
public static void testOgnlStatic() throws Exception{
Object obj = Ognl.parseExpression("@com.test.ognl.Person@getCity()");
System.out.println(Ognl.getValue(obj, null).toString());
Object expression = Ognl.parseExpression("@com.test.ognl.Person@country");
System.out.println(Ognl.getValue(expression, null).toString());
}
@SuppressWarnings("unchecked")
public static void testListOgnl() throws Exception{
String expression = "{1, 2, 3, 4}";
Object obj = Ognl.parseExpression(expression);
List<Integer> list = (List)Ognl.getValue(obj, null);
for(Integer i : list) {
System.out.println(i);
}
}
}
package com.test.ognl;
public class Person {
private String name;
public static String country ="china";
public Person(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getCity() {
return "tianjin";
}
}