ognl總結

 

 ognl & valuestack 入門

 

 

 ognl 方法總結

 

	// ***************** 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("@[email protected](#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("@[email protected](#this.username)", user);
	}
	
	public void testOgnl_16() throws Exception{
		User user = new User();
		
		Ognl.getValue("setUsername('ZHANGSAN')", user);
		Ognl.getValue("@[email protected](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("@[email protected](#listvalue[0])", context, root);
		Ognl.getValue("@[email protected](#intarray[1])", context, root);
		Ognl.getValue("@[email protected](#mapvalue['intarray'][2])", context, root);
		Ognl.getValue("@[email protected](#mapvalue.intarray[0])", context, root);
	}

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章