如何进行变量的访问支持嵌套访问,也就是dot操作符来访问变量里的属性,假设我们有一个Foo类:
定义foo类
public class foo {
private int age;
private int sexy;
private String brithday;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
public int getSexy() {
return sexy;
}
public void setSexy(int sexy) {
this.sexy = sexy;
}
}
定义调用方法
public void testClass() {
Expression el = factory.create(" foo.age + 2 * foo.sexy + obj.testAdd(1,2)");
foo f= new foo();
f.setAge(15);
f.setSexy(2);
HashMap<String, Object> context = new HashMap<String, Object>();
context.put("foo", f);
context.put("obj", this);
Object result22 = el.evaluate(context);
System.out.println("result22:"+result22);
}
public static void main(String[] args) {
JSELTest test = new JSELTest();
test.testClass();
}
运行结果:
result22:22
定义foo类
public class foo {
private int age;
private int sexy;
private String brithday;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
public int getSexy() {
return sexy;
}
public void setSexy(int sexy) {
this.sexy = sexy;
}
}
定义调用方法
public void testClass() {
Expression el = factory.create(" foo.age + 2 * foo.sexy + obj.testAdd(1,2)");
foo f= new foo();
f.setAge(15);
f.setSexy(2);
HashMap<String, Object> context = new HashMap<String, Object>();
context.put("foo", f);
context.put("obj", this);
Object result22 = el.evaluate(context);
System.out.println("result22:"+result22);
}
public static void main(String[] args) {
JSELTest test = new JSELTest();
test.testClass();
}
运行结果:
result22:22