public class OGNL1
{
public static void main(String[] args)
{
/* 创建一个上下文Context对象,它是用保存多个对象一个环境 对象 */
Map<String , Object> context = new HashMap<String , Object>();
Person person1 = new Person();
person1.setName("zhangsan");
Person person2 = new Person();
person2.setName("lisi");
Person person3 = new Person();
person3.setName("wangwu");
/* person4不放入到上下文环境中 */
Person person4 = new Person();
person4.setName("zhaoliu");
/* 将person1、person2、person3添加到环境中(上下文中) */
context.put("person1", person1);
context.put("person2", person2);
context.put("person3", person3);
try
{
/* 获取根对象的"name"属性值 */
Object value = Ognl.getValue("name", context, person2);
System.out.println("ognl expression \"name\" evaluation is : " + value);
/* 获取根对象的"name"属性值 */
Object value2 = Ognl.getValue("#person2.name", context, person2);
System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);
/* 获取person1对象的"name"属性值 */
Object value3 = Ognl.getValue("#person1.name", context, person2);
System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);
/* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 */
Object value4 = Ognl.getValue("name", context, person4);
System.out.println("ognl expression \"name\" evaluation is : " + value4);
/* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 */
Object value5 = Ognl.getValue("#person4.name", context, person4);
System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);
/* 获取person4对象的"name"属性,注意person4对象不在上下文中 */
// Object value6 = Ognl.getValue("#person4.name", context, person2);
// System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);
}
catch (OgnlException e)
{
e.printStackTrace();
}
}
}
class Person
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
ognl expression "name" evaluation is : lisi ognl expression "#person2.name" evaluation is : lisi ognl expression "#person1.name" evaluation is : zhangsan ognl expression "name" evaluation is : zhaoliu ognl.OgnlException: source is null for getProperty(null, "name") at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2296) at ognl.ASTProperty.getValueBody(ASTProperty.java:114) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.ASTChain.getValueBody(ASTChain.java:141) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.Ognl.getValue(Ognl.java:494) at ognl.Ognl.getValue(Ognl.java:596) at ognl.Ognl.getValue(Ognl.java:566) at com.beliefbetrayal.ognl.OGNL1.main(OGNL1.java:53)
public class OGNL2
{
public static void main(String[] args)
{
/* OGNL提供的一个上下文类,它实现了Map接口 */
OgnlContext context = new OgnlContext();
People people1 = new People();
people1.setName("zhangsan");
People people2 = new People();
people2.setName("lisi");
People people3 = new People();
people3.setName("wangwu");
context.put("people1", people1);
context.put("people2", people2);
context.put("people3", people3);
context.setRoot(people1);
try
{
/* 调用 成员方法 */
Object value = Ognl.getValue("name.length()", context, context.getRoot());
System.out.println("people1 name length is :" + value);
Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot());
System.out.println("people2 name upperCase is :" + upperCase);
Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot());
System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);
/* 调用静态方法 */
Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());
System.out.println("min(4,10) is :" + min);
/* 调用静态变量 */
Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());
System.out.println("E is :" + e);
}
catch (OgnlException e)
{
e.printStackTrace();
}
}
}
class People
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
people1 name length is :8 people2 name upperCase is :LISI people1 name.charAt(5) is :s min(4,10) is :4 E is :2.718281828459045
public class OGNL3
{
public static void main(String[] args) throws Exception
{
OgnlContext context = new OgnlContext();
Classroom classroom = new Classroom();
classroom.getStudents().add("zhangsan");
classroom.getStudents().add("lisi");
classroom.getStudents().add("wangwu");
classroom.getStudents().add("zhaoliu");
classroom.getStudents().add("qianqi");
Student student = new Student();
student.getContactWays().put("homeNumber", "110");
student.getContactWays().put("companyNumber", "119");
student.getContactWays().put("mobilePhone", "112");
context.put("classroom", classroom);
context.put("student", student);
context.setRoot(classroom);
/* 获得classroom的students集合 */
Object collection = Ognl.getValue("students", context, context.getRoot());
System.out.println("students collection is :" + collection);
/* 获得classroom的students集合 */
Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot());
System.out.println("first student is : " + firstStudent);
/* 调用集合的方法 */
Object size = Ognl.getValue("students.size()", context, context.getRoot());
System.out.println("students collection size is :" + size);
System.out.println("--------------------------飘逸的分割线--------------------------");
Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot());
System.out.println("mapCollection is :" + mapCollection);
Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot());
System.out.println("the first element of contactWays is :" + firstElement);
System.out.println("--------------------------飘逸的分割线--------------------------");
/* 创建集合 */
Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot());
System.out.println(createCollection);
/* 创建Map集合 */
Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot());
System.out.println(createMapCollection);
}
}
class Classroom
{
private List<String> students = new ArrayList<String>();
public List<String> getStudents()
{
return students;
}
public void setStudents(List<String> students)
{
this.students = students;
}
}
class Student
{
private Map<String , Object> contactWays = new HashMap<String , Object>();
public Map<String , Object> getContactWays()
{
return contactWays;
}
public void setContactWays(Map<String , Object> contactWays)
{
this.contactWays = contactWays;
}
}
students collection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi]
first student is : zhangsan
students collection size is :5
--------------------------飘逸的分割线--------------------------
mapCollection is :{homeNumber=110, mobilePhone=112, companyNumber=119}
the first element of contactWays is :110
--------------------------飘逸的分割线--------------------------
[aa, bb, cc, dd]
{key1=value1, key2=value2}
public class OGNL4
{
public static void main(String[] args) throws Exception
{
OgnlContext context = new OgnlContext();
Humen humen = new Humen();
humen.setName("qiuyi");
humen.setSex("n");
humen.setAge(22);
humen.getFriends().add(new Humen("zhangsan" , "n" , 22));
humen.getFriends().add(new Humen("lisi" , "f" , 21));
humen.getFriends().add(new Humen("wangwu" , "n" , 23));
humen.getFriends().add(new Humen("zhaoliu" , "n" , 22));
humen.getFriends().add(new Humen("qianqi" , "n" , 22));
humen.getFriends().add(new Humen("sunba" , "f" , 20));
humen.getFriends().add(new Humen("yangqiu" , "f" , 25));
context.put("humen", humen);
context.setRoot(humen);
/* OGNL过滤集合的语法为:collection.{? expression} */
Object filterCollection = Ognl.getValue("friends.{? #this.name.length() > 7}", context, context.getRoot());
System.out.println("filterCollection is :" + filterCollection);
System.out.println("--------------------------飘逸的分割线--------------------------");
/* OGNL投影集合的语法为:collection.{expression} */
Object projectionCollection = Ognl.getValue("friends.{name}", context, context.getRoot());
System.out.println("projectionCollection is :" + projectionCollection);
}
}
class Humen
{
private String name;
private String sex;
private int age;
private List<Humen> friends = new ArrayList<Humen>();
public Humen()
{
}
public Humen(String name , String sex , int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public List<Humen> getFriends()
{
return friends;
}
public void setFriends(List<Humen> friends)
{
this.friends = friends;
}
@Override
public String toString()
{
return "Humen [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
filterCollection is :[Humen [name=zhangsan, sex=n, age=22]] --------------------------飘逸的分割线-------------------------- projectionCollection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi, sunba, yangqiu]
<body> username:<s:property value="username"/><br /> -------------------诡异的分割线-------------------<br /> username:<%= ((HelloWorldAction)ActionContext.getContext().getValueStack().peek()).getUsername() %><br /> </body>
username:zhangsan -------------------诡异的分割线------------------- username:zhangsan
<s:property value="user.username"/><br>
<s:property value="user.address.addr"/><br>
<s:property value="user.get()"/><br>
<s:property value="@struts.action.LoginAction@get()"/>
<s:property value="@java.lang.Math@floor(44.56)"/><br>
<s:property value="@@floor(44.56)"/><br>
<s:property value="@java.util.Calendar@getInstance()"/><br>
<s:property value="@struts.vo.Address@TIPS"/><br>
<s:property value="new struts.vo.Student('李晓红' , '美女' , 3 , 25).username"/>
<s:property value="testList"/><br>
<s:property value="testList[0]"/><br>
<s:property value="testSet"/><br>
<s:property value="testSet[0]"/><br> ×
<s:property value="testMap"/><br>
<s:property value="testMap.keys"/><br>
<s:property value="testMap.values"/><br>
<s:property value="testMap['m1']"/><br>
<s:property value="testSet.size"/><br>
<s:property value="stus.{?#this.grade>=60}.{username}"/><br>
<s:property value="stus.{?#this.grade>=60}.{username}[0]"/><br>
<s:property value="stus.{^#this.grade>=60}.{username}"/><br>
<s:property value="stus.{$#this.grade>=60}.{username}"/><br>
<s:property value="stus.{^#this.grade>=600}.{username}.size"/><br>
<s:property value="#f = :[#this==1?1:#this*#f(#this-1)] , #f(4)"/><br>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有