ognl總結

醉面韋陀發表於2010-03-31
	// ***************** root物件的概念 ******************* //
	public void testOgnl_01() throws Exception{
		User user = new User();
		user.setUsername("張三");
		
		//相當於呼叫user.getUsername()方法
		String value = (String)Ognl.getValue("username", user);
		System.out.println(value);
	}
	
	public void testOgnl_02() throws Exception{
		User user = new User();
		Person p = new Person();
		p.setName("張三");
		user.setPerson(p);
		
		//相當於呼叫user.getPerson().getName()方法
		String value = (String)Ognl.getValue("person.name", user);
		System.out.println(value);
	}
	
	public void testOgnl_03() throws Exception{
		User user = new User();
		Person p = new Person();
		p.setName("張三");
		user.setPerson(p);
		
		//可以使用#root來引用根物件,相當於呼叫user.getPerson().getName()方法
		String value = (String)Ognl.getValue("#root.person.name", user);
		System.out.println(value);
	}
	

 

 

// *********************** context的概念 **********************//
	public void testOgnl_04() throws Exception{
		Person p1 = new Person();
		Person p2 = new Person();
		p1.setName("張三");
		p2.setName("李四");
		
		Map context = new HashMap();
		context.put("p1", p1);
		context.put("p2", p2);
		
		String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name", context, new Object());
		System.out.println(value);
	}
	
	public void testOgnl_05() throws Exception{
		Person p1 = new Person();
		Person p2 = new Person();
		p1.setName("張三");
		p2.setName("李四");
		
		Map context = new HashMap();
		context.put("p1", p1);
		context.put("p2", p2);
		
		User root = new User();
		root.setUsername("zhangsan");
		
		String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name + ',' + username", context, root);
		System.out.println(value);
	}

 

 

 

	// ******************* OGNL賦值操作 ************************//
	public void testOgnl_06() throws Exception{
		User user = new User();
		
		//給root物件的屬性賦值,相當於呼叫user.setUsername()
		Ognl.setValue("username", user, "zhangsan");
		
		System.out.println(user.getUsername());
	}
	
	public void testOgnl_07() throws Exception{
		User user = new User();
		
		Map context = new HashMap();
		context.put("u", user);
		
		//給context中的物件屬性賦值,相當於呼叫user.setUsername()
		Ognl.setValue("#u.username",context, new Object(), "zhangsan");
		
		System.out.println(user.getUsername());
	}
	
	public void testOgnl_08() throws Exception{
		User user = new User();
		
		Map context = new HashMap();
		context.put("u", user);
		
		//給context中的物件屬性賦值,相當於呼叫user.setUsername()
		//在表示式中使用=賦值操作符來賦值
		Ognl.getValue("#u.username = '張三'",context, new Object());
		
		System.out.println(user.getUsername());
	}
	
	public void testOgnl_09() throws Exception{
		User user = new User();
		Person p = new Person();
		
		Map context = new HashMap();
		context.put("u", user);
		
		context.put("p", p);
		
		//給context中的物件屬性賦值,相當於呼叫user.setUsername()
		//在表示式中使用=賦值操作符來賦值
		Ognl.getValue("#u.username = '張三',#p.name = '李四'",context, new Object());
		
		System.out.println(user.getUsername()+","+p.getName());
	}
	

 

 

 

	
	//****************** 使用OGNL呼叫物件的方法 **********************//
	public void testOgnl_10() throws Exception{
		User user = new User();
		user.setUsername("張三");
		
		String value = (String)Ognl.getValue("getUsername()", user);
		System.out.println(value);
	}
	
	public void testOgnl_11() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('張三')", user);
		System.out.println(user.getUsername());
	}
	

 

 

 

	// ********************* OGNL中的this表示式 **********************//
	public void testOgnl_14() throws Exception{
		Object root = new Object();
		Map context = new HashMap();
		
		List values = new ArrayList();
		for(int i=0; i<11; i++){
			values.add(i);
		}
		context.put("values", values);
		
		Ognl.getValue("@System@out.println(#values.size.(#this > 10 ? \"大於10\" : '不大於10'))", context, root);
		
	}
	
	public void testOgnl_15() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('ZHANGSAN')", user);
		Ognl.getValue("@System@out.println(#this.username)", user);
	}
	
	public void testOgnl_16() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('ZHANGSAN')", user);
		Ognl.getValue("@System@out.println(username.(#this.toLowerCase()))", user);
	}

 

 

	// ******************* 如何把表示式的解釋結果作為另外一個表示式,OGNL中括號的使用 **********************//
	public void testOgnl_17() throws Exception{
		Object root = new Object();
		Map context = new HashMap();
		User u = new User();
		u.setUsername("張三");
		context.put("u", u);
		context.put("fact", "username");
		
		/**
		 * 1、首先把#fact表示式進行解釋,得到一個值:username
		 * 2、解釋括號中的表示式#u,其結果是user物件
		 * 3、把括號中表示式的結果作為root物件,解釋在第一步中得到的結果(即把第一步的結果看成另外一個表示式) 
		 */
		String value = (String)Ognl.getValue("#fact(#u)", context, root);
		System.out.println(value);
	}
	
	public void testOgnl_18() throws Exception{
		Person person = new Person();
		Map context = new HashMap();
		User u = new User();
		u.setUsername("張三");
		context.put("u", u);
		
		/**
		 * 相當於呼叫person這個根物件的fact方法,並把#u的解釋結果作為引數傳入此方法 
		 */
		String value = (String)Ognl.getValue("fact(#u)", context, person);
		System.out.println(value); //輸出是 "張三,"
	}
	
	public void testOgnl_19() throws Exception{
		Person person = new Person();
		Map context = new HashMap();
		User u = new User();
		u.setUsername("張三");
		context.put("u", u);
		
		/**
		 * 1、先計算表示式fact,得到結果是:username
		 * 2、解釋括號中的表示式#u,其結果是user物件
		 * 3、把括號中表示式的結果作為root物件,解釋在第一步中得到的結果(即把第一步的結果看成另外一個表示式)
		 */
		String value = (String)Ognl.getValue("(fact)(#u)", context, person);
		System.out.println(value); //輸出"張三"
	}
	

 

 

	// ********************* OGNL訪問集合元素 **************************//
	public void testOgnl_20() throws Exception{
		Object root = new Object();
		Map context = new HashMap();
		
		//用OGNL建立List物件
		List listvalue = (List)Ognl.getValue("{123,'kdjfk','oooo'}", context, root);
		context.put("listvalue", listvalue);
		
		//用OGNL建立陣列
		int[] intarray= (int[])Ognl.getValue("new int[]{23,45,67}", context, root);
		context.put("intarray", intarray);
		
		//用OGNL建立Map物件
		Map mapvalue = (Map)Ognl.getValue("#{'listvalue':#listvalue,'intarray':#intarray}", context, root);
		context.put("mapvalue", mapvalue);
		
		Ognl.getValue("@System@out.println(#listvalue[0])", context, root);
		Ognl.getValue("@System@out.println(#intarray[1])", context, root);
		Ognl.getValue("@System@out.println(#mapvalue['intarray'][2])", context, root);
		Ognl.getValue("@System@out.println(#mapvalue.intarray[0])", context, root);
	}