利用ognl 初始化数据(map 套list|map)

public static class MyNullHandler implements NullHandler {

		@SuppressWarnings("rawtypes")
		public Object nullMethodResult(Map context, Object target,
				String methodName, Object[] args) {
			System.err.println("==");
			return null;
		}

		public String getFullExp(Node node) {

			if (node.jjtGetParent() == null)
				return node.toString();
			else
				return getFullExp(node.jjtGetParent());

		}

		@SuppressWarnings({ "rawtypes", "unchecked" })
		public Object nullPropertyValue(Map context, Object target,
				Object property) {

			Object currentObj = null;

			OgnlContext ctx = (OgnlContext) context;
			ASTProperty currentNode = (ASTProperty) ((ASTConst) ctx
					.getCurrentNode()).jjtGetParent();
			String fullExp = getFullExp(currentNode);
			String prefix = "";
			if (isFirstNode(currentNode))
				prefix = currentNode + "[";
			else
				prefix = currentNode.jjtGetParent() + "[";
			if (fullExp.startsWith(prefix))
				currentObj = new MyList();
			else
				currentObj = new HashMap();

			if (target instanceof HashMap) {
				((HashMap) target).put(property, currentObj);
			}

			if (target instanceof List) {
				((List) target).add((Integer) property, currentObj);
			}
			return currentObj;
		}

		class MyList extends ArrayList {
			@Override
			public Object get(int index) {
				try {
					return super.get(index);
				} catch (Exception e) {
					return null;
				}
			}
		}

	}

 

@Test
	public void testAutoCreateMapAndList() {
		try {
			final OgnlExpressionEvaluator ee = new OgnlExpressionEvaluator();
			OgnlRuntime.setNullHandler(Object.class, new MyNullHandler());
			Map<String, Object> root = new HashMap<String, Object>();
			ee.setValue(root, root, "root.bar[0].name", "mike", true);
			ee.setValue(root, root, "root.bar[0].id", "001", true);

			// 手动初始化
			// List list = (List) ((Map) root.get("root")).get("bar");
			// Map m = new HashMap();
			// list.add(1, m);
			// m.put("name", "hzl");
			// m.put("id", "003");

			ee.setValue(root, root, "root.bar[1].name", "lizhs", true);
			ee.setValue(root, root, "root.bar[1].id", "002", true); 
			ee.setValue(root, root, "root.bar[1].dep[0][0].name", "研发部门", true);
			ee.setValue(root, root, "root.bar[1].dep[0][1].name", "测试部门", true);
			ee.setValue(root, root, "root.bar[1].dep[1][0].name", "研发部门2", true);
			ee.setValue(root, root, "root.bar[1].dep[1][1].name", "测试部门2", true);

			// Object findValue = ee.findValue("a.b.c", root,
			// root);
			// System.out.println(findValue);

			System.out.println(root);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

{root={bar=[{id=001, name=mike}, {id=002, name=lizhs, dep=[[{name=研发部门}, {name=测试部门}], [{name=研发部门2}, {name=测试部门2}]]}]}}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值