OGNL的使用方式
来源:互联网 发布:王仕鹏cba数据 编辑:程序博客网 时间:2024/05/16 07:18
OGNL(Object Graph Navigation language)
OgnlContext:上下文对象,存在一个唯一的叫做根的对象(root),
可以通过程序设定上下文当中的那个对象作为根对象。
在OGNL中,如果表达式没有使用#号,那么OGNL会从根对象中寻找该属性对应的get方法,如果寻找的不是根对象中
的属性,那么则需要以#号开头告诉OGNL,去寻找你所指定的特定对象中的属性。
当使用OGNL调用静态的方法的时候,需要按照如下的语法编写表达式:
@package.classname@methodname(parameters)
对于OGNL来说,java.lang.Math是其默认的类,如果调用java.lang.Math
的静态方法时,无需指定类名。@@min(5,6)
对于OGNL来说:数组与集合一样的,都是通过下表索引来去访问的
构造集合的时候通过{.....}形式来访问。
使用OGNL来处理映射(Map)的语法格式如下:
#{'key1','value1','key2','value2','key3','value3'};
通过key来访问。
eg:System.out.println(Ognl.getValue("#{'one':'value1','two':'value2','three':'value3'}['one']", context,context.getRoot());
过滤(filtering):
collection.{?expression}基本语法格式。
OGNL针对集合提供了一些伪属性(如size,isEmpty),让我们
可以通过属性的方式来调用方法(本质原因在于集合当中的很多的方法并不符合
JavaBean的命名规则),但还可以通过调用方法来实现与伪属性相同的目的。
过滤(filtering),获取集合中的第一个元素collection.{^ expression}
过滤(filtering),获取集合中的最后一个元素collection.{$ expression}
在使用过滤操作时,我们通常都会使用#this,该表达式代表当前正在迭代的
集合中的对象(类比:增强的for循环)。for (Person p:persons){}
投影(projection):基本语法:collection.{expression}
eg:
类比数据库中的投影,过滤是对行的操作,投影式对列的操作。
数据库:select name from emp where age > 50;
其中select name 就相当于投影,而where age > 50就相当于过滤。
------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;import java.util.List;import java.util.Map;import ognl.Ognl;import ognl.OgnlContext;import ognl.OgnlException;import bean.Dog;import bean.Person;public class OgnlTest {public static void main(String[] args) throws Exception {Dog innerDog = new Dog ();innerDog.setName("lisi");Person person = new Person ();person.setName("zhangsan");person.setDog(innerDog);Dog dog = new Dog ();dog.setName("wangcai");OgnlContext context = new OgnlContext ();context.put("person", person);context.put("dog", dog);//将person对象设置成根对象context.setRoot(person);Object object = Ognl.parseExpression("dog.name");System.out.println(object);Object object2 = Ognl.getValue(object, context,context.getRoot());System.out.println(object2);System.out.println("--------------------------");Object object3 = Ognl.parseExpression("#person.name");System.out.println(object3);Object object4 = Ognl.getValue(object3, context,context.getRoot());System.out.println(object4);System.out.println("----------------------------");Object object5 = Ognl.parseExpression("#dog.name");System.out.println(object5);Object object6 = Ognl.getValue(object5, context,context.getRoot());System.out.println(object6);System.out.println("----------------------------------");//调用方法Object object7 = Ognl.parseExpression("name.toUpperCase().length()");System.out.println(object7);Object object8 = Ognl.getValue(object7, context,context.getRoot());System.out.println(object8);System.out.println("----------------------------------");//调用静态方法Object object9 = Ognl.parseExpression("@java.lang.Integer@toBinaryString(10)");System.out.println(object9);Object object10 = Ognl.getValue(object9, context,context.getRoot());System.out.println(object10);System.out.println("----------------------------------");Object object11 = Ognl.parseExpression("@@min(4,6)");System.out.println(object11);Object object12 = Ognl.getValue(object11, context,context.getRoot());System.out.println(object12);System.out.println("----------------------------------");//生成对象Object object13 = Ognl.parseExpression("new java.util.LinkedList()");System.out.println(object13);Object object14 = Ognl.getValue(object13, context,context.getRoot());System.out.println(object14);System.out.println("----------------------------------");//生成list对象Object object15 = Ognl.getValue("{'aa','bb','cc'}[0]", context,context.getRoot());System.out.println(object15);//在OGNL中,数组和列表看成是一样的。System.out.println(object15 instanceof ArrayList);System.out.println("----------------------------------");//访问数组中的元素dog.setFriends(new String[]{"aa","bb","cc"});Object object16 = Ognl.getValue("#dog.friends[1]", context,context.getRoot());System.out.println(object16);System.out.println("----------------------------------");List<String> list = new ArrayList<String> ();list.add("hello");list.add("world");list.add("hello world");context.put("list", list);System.out.println(Ognl.getValue("#list[0]", context,context.getRoot()));System.out.println("------------------------------------");System.out.println(Ognl.getValue("#{'one':'value1','two':'value2','three':'value3'}['one']", context,context.getRoot()));System.out.println("---------------------------------------------------------------------");//System.out.println(Ognl.getValue("#list.{? this.length() > 6}", context,context.getRoot()));List<Person> persons = new ArrayList<Person> ();Person p1 = new Person ();Person p2 = new Person ();Person p3 = new Person ();p1.setName("zhangsan");p2.setName("lisi");p3.setName("wangwu");persons.add(p1);persons.add(p2);persons.add(p3);context.put("persons", persons);//notices:此处的集合值得是List和Array//过滤(filter),collection.{? expression} eg:#this表示当前待操作的元素System.out.println(Ognl.getValue("#persons.{? #this.name.length () > 4}.isEmpty()", context,context.getRoot()));//过滤,获取到集合中的第一个元素,collection.{^ expression}List list2 = (List)Ognl.getValue("#persons.{^ #this.name.length () > 4}", context,context.getRoot());//返回的仍然是集合,只不过集合中只有一个元素System.out.println(list2);//过滤 获得最后一个元素System.out.println(Ognl.getValue("#persons.{$ #this.name.length () > 4}[0].name", context,context.getRoot()));System.out.println("-----------------------------------------------------------");//投影(projection),collection. {expression}System.out.println (Ognl.getValue("#persons.{#this.name.length()<=5?'hello':#this.name}", context,context.getRoot()));System.out.println("------------------------------------------------------------");//result:[zhangsan, hello, wangwu]System.out.println(Ognl.getValue("#persons.{'hello'}", context,context.getRoot()));//result:[hello, hello, hello]}}
- OGNL的使用方式
- ognl的%,#的使用
- ognl的使用
- struts2 OGNL # % $的使用
- OGNL的简单使用
- OGNL表达式的使用
- OGNL表达式的使用
- OGNL表达式的使用
- OGNL表达式的使用
- ognl中#,$,%的使用
- OGNL技术的使用
- OGNL的简单使用
- Ognl表达式的使用
- OGNL特殊符号的使用
- OGNL表达式的使用
- struts2的OGNL的使用
- struts2的OGNL的使用
- .Action中获取Web对象的方式和OGNL表达式的使用
- Shapefile文件
- 二叉树的遍历:递归和非递归实现
- 数组的初始化
- 获取当前脚本绝对路径
- 怎样和脾气急躁的人相处?
- OGNL的使用方式
- C++拷贝构造函数详解
- Linux环境进程间通信(一)
- Hibernate注解,生成主键策略
- CArchive 原理
- 自定义动画之一列中某一图片在表格中依次移动到不同列
- Android教程(十二)-- 使用DisplayMetrics获取手机分辨率
- 二叉树C++ Version
- linux没有 eth0