struts2的ognl表达式

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);
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值