struts2[3.2]OGNL表达式语句--创建对象lits | map(补充)

本文通过具体示例,介绍了如何使用OGNL表达式创建对象,包括List和Map类型的对象,并展示了如何获取这些对象的属性和大小,适用于熟悉Java语言并对OGNL表达式有兴趣的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建对象~

测试(list):

@Test
	//OGNL创建对象list|map
	public void fun8() throws OgnlException {
		//准备OGNLContext
			//准备Root
			User rootUser1 = new User("tom",18);
			//准备Context
			Map<String,User> context = new HashMap<String,User>();
			context.put("user1", new User("isleiyi",18));
			context.put("user2", new User("Rose",19));
		
		OgnlContext oc = new OgnlContext();
		oc.setRoot(rootUser1);
		oc.setValues(context);
		//书写OGNl
		
		Integer size1 = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
		String name1 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
		String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
		
		System.out.println(size1);
		System.out.println(name1);
		System.out.println(name2);
		
	}

输出:

4
tom
jerry

测试(map):

		Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
		String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
		Integer name4 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
		
		System.out.println(size2);
		System.out.println(name3);
		System.out.println(name4);
		

输出:

2
tom
18

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值