ognl Api类: OgnlContext对象,底层实现了map接口..因此它是一个map对象
放到ValueStack的list栈中的对象不用使用#号
放到ValueStack的map栈中的对象要使用#号
package cn.itcast.ognl;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.junit.Test;
import cn.itcast.domain.Admin;
public class OgnlDemo1 {
@Test
public void test() throws Exception{
//创建一个OgnlContext对象
OgnlContext context=new OgnlContext();
Admin admin=new Admin(null, null);
admin.setPassword("123");
admin.setUsername("admin");
context.put("admin", admin);<span style="color:#ff0000;">//值栈的map</span>
Object ognl = Ognl.parseExpression("#admin.username");
Object value = Ognl.getValue(ognl, context, context.getRoot());
System.out.println(value);
context.setRoot(admin);/<span style="color:#ff0000;">/放到值栈的list</span>
Object ognl1 = Ognl.parseExpression("admin.username");
Object value1 = Ognl.getValue(ognl, context, context.getRoot());
System.out.println(value1);
}
@Test
public void test1() throws OgnlException{
OgnlContext context=new OgnlContext();
Object ognl=Ognl.parseExpression("@Math@floor(10.2)");
Object value = Ognl.getValue(ognl, context,context.getRoot());
System.out.println(value);
}
}
test():
admin
admin
test1():执行静态方法
10.0