struts2的ognl表达式

xiaoxiao2021-02-28  89

public class Demo1 { @Test //取值演示 public void fun1() throws Exception{ //准备工作 User u = new User(); //Root部分 u.setName("tom"); Map<String,Object> map = new HashMap<String,Object>();//Context部分 User u2 = new User(); u2.setName("jerry"); map.put("user", u2); //语法 //参数1: 书写OGNL表达式 //参数2: 放入Context部分 //参数3: 放入Root部分 //取出root中的name属性值 String name = (String) Ognl.getValue("name", map, u); System.out.println(name); //取出context中 键为user的user对象中的name属性 String name2 = (String) Ognl.getValue("#user.name", map, u); System.out.println(name2); } @Test //赋值演示 public void fun2() throws Exception{ //准备工作 User u = new User(); //Root部分 u.setName("tom"); Map<String,Object> map = new HashMap<String,Object>();//Context部分 User u2 = new User(); u2.setName("jerry"); map.put("user", u2); //将root中User对象的age属性赋值为18 Ognl.getValue("age=18", map, u); Integer age = (Integer) Ognl.getValue("age", map, u); System.out.println(age); //将Context中user的age属性赋值为16 //Ognl.getValue("#user.age=16", map, u); Integer age2 = (Integer) Ognl.getValue("#user.age=16,#user.age", map, u);//一次执行两个OGNL表达式.表达式之间使用逗号分隔. //多个表达式都会执行,只会返回最后一个表达式结果 System.out.println(age2); } @Test //调用方法 public void fun3() throws Exception{ //准备工作 User u = new User(); //Root部分 u.setName("tom"); Map<String,Object> map = new HashMap<String,Object>();//Context部分 User u2 = new User(); u2.setName("jerry"); map.put("user", u2); //将root中User对象的age属性赋值为18 Integer age= (Integer) Ognl.getValue("setAge(20),age", map, u); System.out.println(age); //将Context中user的age属性赋值为16 Integer age2= (Integer) Ognl.getValue("#user.setAge(16),#user.age", map, u); System.out.println(age2); } @Test //调用静态方法|常量 public void fun4() throws Exception{ //准备工作 User u = new User(); //Root部分 u.setName("tom"); Map<String,Object> map = new HashMap<String,Object>();//Context部分 User u2 = new User(); u2.setName("jerry"); map.put("user", u2); //调用echo静态方法 String str = (String) Ognl.getValue("@cn.itcast.a_ognl.OGNLTool@echo('hello world')", map, u); System.out.println(str); //访问静态属性 String str2 = (String) Ognl.getValue("@cn.itcast.a_ognl.OGNLTool@name", map, u); System.out.println(str2); //访问 Math中的常量(Math是内置静态对象,可以省略完整类名.其他类不行) //Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", map, u); Double pi = (Double) Ognl.getValue("@@PI", map, u); System.out.println(pi); } @Test //创建List|Map public void fun5() throws Exception{ //准备工作 User u = new User(); //Root部分 u.setName("tom"); Map<String,Object> map = new HashMap<String,Object>();//Context部分 User u2 = new User(); u2.setName("jerry"); map.put("user", u2); //创建List对象并将List对象返回.并没有放置到Root|Context中 List list = (List) Ognl.getValue("{'tom','jerry'}", map, u); System.out.println(list); Integer size = (Integer) Ognl.getValue("{'tom','jerry'}.size()", map, u); System.out.println(size); //创建Map对象并将Map返回 Map map2 = (Map) Ognl.getValue("#{'name':'rose','age':16}", map, u); System.out.println(map2); Integer size2 = (Integer) Ognl.getValue("#{'name':'rose','age':16}.size()", map, u); System.out.println(size2); } }
转载请注明原文地址: https://www.6miu.com/read-20453.html

最新回复(0)