1. 程式人生 > >struts2[3.2]OGNL表示式語句--建立物件lits | map(補充)

struts2[3.2]OGNL表示式語句--建立物件lits | map(補充)

建立物件~

測試(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