struts2 ognl语法

/*展示OGNL语法
 * 
 * 
 * 
 */

package com.Ognl.cn;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.Ognl.domain.User;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OgnlDemo {
	private static final String String = null;

	@Test
	public void fun1() throws OgnlException{
		//1.准备OGNLContext
		//准备Root
		User userRoot = new User("张三",18);
	    //准备Context
		Map map = new HashMap();
		map.put("one", new User("李四",20));
		map.put("two", new User("王五",22));
	
		OgnlContext ognl = new OgnlContext();
		ognl.setRoot(userRoot);
		ognl.setValues(map);
		//书写OGNl
		//2.取出root中user对象的name属性                                                                                                                                                                                                                                                                                                                                                                             
		String str = (String)Ognl.getValue("name", ognl, ognl.getRoot());
	//	System.out.println(str);
		
		//取值
		Integer age = (Integer)Ognl.getValue("age", ognl, ognl.getRoot());
		System.out.println(age);
		
		
		//取出Context中的值
		//#代表要从context中取值
		String value = (String)Ognl.getValue("#one.name", ognl, ognl.getRoot());
		//System.out.println(value);
		Integer value1 = (Integer)Ognl.getValue("#two.age", ognl, ognl.getRoot());
		//System.out.println(value1);
//=============================================================================================		
		
		//赋值操作 Root
		//由张三改为Jerry
		Ognl.getValue("name='Jerry'", ognl, ognl.getRoot());
		//取出Root中的值
		String value2 = (String)Ognl.getValue("name", ognl, ognl.getRoot());
		System.out.println(value2);
		Integer value3 = (Integer)Ognl.getValue("age", ognl, ognl.getRoot());
		//System.out.println(value3);
		
		//赋值Context
		Ognl.getValue("#two.name ='Tom'", ognl, ognl.getRoot());
		//取值
		String str5 = (String)Ognl.getValue("#two.name", ognl,  ognl.getRoot());		     
	    //System.out.println(str5);
//=============================================================================================	    
	    //调用对象的方法
	    //没加#,从root中取值
		//取出root中user对象的name属性.
	    String name = (String)Ognl.getValue("getName()", ognl,ognl.getRoot());
	    System.out.println(name);
	    
	    //root中赋值操作
	    Ognl.getValue("setName('lilei')", ognl,ognl.getRoot());
	      
	    Ognl.getValue("#one.setName('lucy')", ognl, ognl.getRoot());
	     
	    String str9 = (String) Ognl.getValue("#one.getName()", ognl, ognl.getRoot());
	    System.out.println(str9);
	
	    String str0 =   (String)Ognl.getValue("#one.getName()", ognl, ognl.getRoot());
	    System.out.println(str0);
	    
//====================================================================================================================
	    //调用静态方法
	    String str11 =(String)Ognl.getValue("@com.Ognl.cn.StaticMethod@fun('123')",ognl,ognl.getRoot());
		System.out.println(str11);
		
		//===========================================================================================================
	//ognl创建对象,list,map 
		
		//创建list对象
		
		 Integer size = (Integer)Ognl.getValue("{'tom','Jerry','jack','rose'}.size()", ognl, ognl.getRoot());
		 System.out.println(size);
		 
		 //取元素
		String strx = (String)Ognl.getValue("{'tom','Jerry','jack','rose'}[0]", ognl, ognl.getRoot());
		//或者
		String stx1 = (String)Ognl.getValue("{'tom','Jerrry','jack','rose'}.get(1)", ognl, ognl.getRoot());
		System.out.println(strx);
		System.out.println(stx1);
		
		
		//===========================================================================================================
		//map对象
		//#{}   表示要创建一个map
		Integer stxm = (Integer)Ognl.getValue("#{'name':'tom','age':18}.size()", ognl, ognl.getRoot());
		System.out.println(stxm);
		
		//获得name键对应的值
		String stxn = (String)Ognl.getValue("#{'name':'tom','age':18}['name']", ognl, ognl.getRoot());
		String stxz = (String)Ognl.getValue("#{'name':'tom','age':18}.get('age')", ognl, ognl.getRoot());
		System.out.println(stxz);
	}
} 
/*OGNL:对象视图导航语言(这种写法就是user.addr.name) 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */

 

你可能感兴趣的:(struts2)