struts2 ognl表达式

推荐阅读:

http://developer.51cto.com/art/201203/322509.htm

 

OGNL:(Object Graphic Navigation Language)对象图导航语言。

 

OgnlContext:ognl上下文对象,实现Map接口。把对象放到OgnlContext中,就可以通过ognl表达式很容易的操纵对象。

 

ROOT:OgnlContext有一个唯一的根对象,访问根对象下的成员变量可以省略#root。


一个简单的示例:

OgnlContext context = new OgnlContext(); 
context.put("person",person); 
//解析表达式为ognl的ASTProperty对象 
Object obj = Ognl.parseExpression("#person.name"); 
//获取表达式的值,本质上就是调用name的set方法 
Object objVal = Ognl.getValue(obj, context, context.getRoot()); 

//设置person为根对象
 context.setRoot(person); 
//会首先到根对象中去查找name 
System.out.println(Ognl.getValue("name", context, context.getRoot()));

 
ognl支持对对象树、成员变量、方法、数组、集合、映射的访问。

 

package ognl;

import java.util.List;
import java.util.Map;

//get和set方法没在此列出
public class People
{
	private String name;
	private String gender;
	private List<String> interest;
	private Address address;
	private Map<String,String> map;	

	public String toString()
	{
		return this.name;
	}
}

class Address
{
	private String homeAddress;
	private String companyAddress;	
}

 

package ognl;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import ognl.Ognl;  
import ognl.OgnlContext;  
import ognl.OgnlException;  
  
public class OgnlTest  
{  
    public static void main(String[] args) throws OgnlException  
    {  
        People people = new People();  
          
        Address address = new Address();  
        address.setHomeAddress("HuBei");  
        address.setCompanyAddress("HangZhou");  
  
        List<String> list = new ArrayList<String>();  
        list.add("basketball");  
        list.add("movie");  
        list.add("music");  
          
        Map<String,String> map = new HashMap<String,String>();    
        map.put("A","a");  
        map.put("B","b");  
        map.put("C","c");  
  
        people.setGender("male");  
        people.setName("lisi");  
        people.setInterest(list);  
        people.setAddress(address);  
        people.setMap(map);  
      
        OgnlContext context = new OgnlContext();  
        context.put("people", people);  
        context.setRoot(people);  
      
        //操作OgnlContext中的对象
        System.out.println(Ognl.getValue("#people.gender", context, context.getRoot()));               //Member  
        System.out.println(Ognl.getValue("#people.address.homeAddress", context, context.getRoot()));  //Object Tree  
        System.out.println(Ognl.getValue("#people.interest[1]", context, context.getRoot()));          //List or Array  
        System.out.println(Ognl.getValue("#people.toString()", context, context.getRoot()));           //Method  
        System.out.println(Ognl.getValue("#people.map.A", context, context.getRoot()));                //Map  
  
        System.out.println("----------------------------");  
          
        //访问类的静态方法和成员变量:@class@member,class默认为java.lang.Math类  
        System.out.println(Ognl.getValue("@@E", context, context.getRoot()));  
        System.out.println(Ognl.getValue("@java.lang.Math@PI", context, context.getRoot()));     //Static Member  
        System.out.println(Ognl.getValue("@java.lang.Integer@toBinaryString(10)", context,context.getRoot())); //Static Method  
          
        System.out.println("----------------------------");  
                  
        //创建对象  
        System.out.println(Ognl.getValue("new Object()", context, context.getRoot()));  
        System.out.println(Ognl.getValue("new String('hello').toUpperCase()", context,  
                context.getRoot()));        
        System.out.println("----------------------------");  
                  
        //创建数组或集合  
        System.out.println(Ognl.getValue("{1,2,3,4}", context,context.getRoot()));    
        System.out.println(Ognl.getValue("{'A','B','C','D'}[2]", context, context.getRoot()));    
          
        //创建映射Map,可用三种方式访问Map中值
        System.out.println(Ognl.getValue("#{'K1':'V1','K2':'V2','K3':'V3'}", context,context.getRoot()));  
        System.out.println(Ognl.getValue("#{'K1':'V1','K2':'V2','K3':'V3'}.K1", context,context.getRoot()));  
        System.out.println(Ognl.getValue("#{'K1':'V1','K2':'V2','K3':'V3'}['K1']", context,context.getRoot()));  
        System.out.println(Ognl.getValue("#{'K1':'V1','K2':'V2','K3':'V3'}.get('K1')", context,context.getRoot()));  
    }  
} 

ognl支持两种操作来返回一个集合的子集:过滤和投影。过滤用于行,投影用于列。

 

过滤(filtering)
collection.{? expression}  返回满足的条件一个子集    
collection.{^ expression}  返回子集的第一个元素,也是一个collection
collection.{$ expression}  返回子集的第二个元素

#persons.{? #this.name.length() > 4}.size()        //返回name长度大于4的person的集合

投影(projection)
collection.{? expression}   返回列的集合
#persons.{name}          //返回所有person中name组成的集合

 

你可能感兴趣的:(struts2)