您的位置:首页 > 编程语言 > Java开发

Struts标签库之OGNL

2015-07-05 18:11 477 查看
Ognl(Object Graph Navigation Language)对象图导航语言

1. OgnlContext(上下文对象)实现map接口,存在唯一的根对象(root),可以通过程序设定上下文当中的哪个对象作为根对象。

2. 在OGNL中,如果表达式没有使用#号,那么OGNL会从根对象中寻找该属性对应的get方法,如果寻找的不是根对象中的属性,那么则需要以#号开头,告诉OGNL,去寻找你所指定的特定对象中的属性。

3. 当使用OGNL调用静态方法的时候,需要按照如下语法编写表达式:

@package.classname@methodname(parameter)

4. 对于OGNL来说,java.lang.Math是其默认类,如果调用java.lang.Math的静态方法时,无需指定类的名字,比如:@@min(3,6);

5. 对于OGNL来说,数组与集合是一样的,都是通过下标索引来去访问的。构造集合的时候使用{ …}形式。

6. 使用OGNL来处理映射(Map)的语法格式如下所示:

“#{‘key1’:’value1’, ‘key2’:’value2’,’key3’:’value3’}”;

7. OGNL针对集合提供了一些伪属性(如size,isEmpty),让我们可以通过属性的方式来调用方法(本质原因在于集合当中很多方法并不符合JavaBean的命名规则),但我们依然还可以通过调用方法来实现与伪属性相同的目的。

8. 过滤(filtering):collection.{? expression}, 获取到集合中的第一个元素:collection.{^ expression}, 获取到集合中的最后一个元素:collection.{$ expression}

9. 在使用过滤操作时,我们通常都会使用#this,该表达式用于代表当前正在迭代的集合中的对象。

10. 投影(projection):collection.{expression}

11. 过滤与投影之间的差别:类比于数据库中的表,过滤是取行的操作,二投影是取列的操作。

12. 在struct2中有一个称之为值栈的概念(ValueStack)。ValueStack通过N语法与top关键字就可以访问到ValueStack中的任意对象。

13. 在struts2中,根对象就是ValueStack。在Struts2的任何流程当中,ValueStack中的最顶层对象一定是Action对象

14.Struts2中的命名对象

parameters #parameters.username;

request #request.username;

session #session.username;

application #application.username;

attr #attr.username;

15. 关于struts2标签库属性值的%与#号的关系:

- 如果标签的属性值是OGNL表达式,那么无需加上%{}.

- 如果标签的属性值是字符串类型,那么在字符串当中凡是出现%{}的部分都会被解析成OGNL表达式,解析完毕后再与其它字符串进行拼接构造出最终的字符串值.

- 我们可以在所有的属性值上加%{},这样如果该属性值是OGNL表达式,那么标签处理类就会将%{}忽略掉.

实例代码:

Person person = new Person();
person.setName("zhangsan");

Dog dog = new Dog();
dog.setName("wang");

OgnlContext context = new OgnlContext();
context.put("person", person);
context.put("dog", dog);

context.setRoot(person); //设置根对象

Object obj = Ognl.parseExpression("name");
Object obj1 = Ognl.getValue(obj, context, context.getRoot());
System.out.println(obj1);

System.out.println("-----------------");
Object obj2 = Ognl.parseExpression("#person.name");
Object obj3 = Ognl.getValue(obj2, context, context.getRoot());
System.out.println(obj3);

System.out.println("-----------------");
Object obj4 = Ognl.parseExpression("#dog.name");
Object obj5 = Ognl.getValue(obj4, context, context.getRoot());
System.out.println(obj5);

System.out.println("-----------------");
Object obj6 = Ognl.parseExpression("#dog.name.toUpperCase()"); //调用方法
Object obj7 = Ognl.getValue(obj6, context, context.getRoot());
System.out.println(obj7);

System.out.println("-----------------");
Object obj8 = Ognl.parseExpression("@java.lang.Integer@toBinaryString(10)");
Object obj9 = Ognl.getValue(obj8, context, context.getRoot());
System.out.println(obj9);

System.out.println("-----------------");
Object obj10 = Ognl.getValue("@@max(4,8)", context, context.getRoot());
System.out.println(obj10);

System.out.println("-----------------");
Object obj11 = Ognl.getValue("new java.util.LinkedList()", context, context.getRoot());
System.out.println(obj11);

System.out.println("-----------------"); //生成集合
Object obj12 = Ognl.getValue("{'aa','bb','cc'}[1]", context,context.getRoot());
System.out.println(obj12);

System.out.println("-----------------");//生成数组
Object obj13 = Ognl.getValue("new java.lang.String[]{'aa','bb','cc'}[2]", context, context.getRoot());
System.out.println(obj13);

ArrayList<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("chm");

context.put("list", list);

System.out.println("-----------------");
Object obj14 = Ognl.getValue("#list[1]",context, context.getRoot());
System.out.println(obj14);

System.out.println("-----------------");//处理映射(map)
Object obj15 = Ognl.getValue("#{'key1':'value1','key2':'value2','key3':'value3'}['key2']", context , context.getRoot());
System.out.println(obj15);

System.out.println("-----------------");
ArrayList<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);

//过滤(filtering)
Object obj16 = Ognl.getValue("#persons.{? #this.name.length() > 4}[0].name", context, context.getRoot());
System.out.println(obj16);

//获取结果集合中的第一个元素
Object obj17 = Ognl.getValue("#persons.{^ #this.name.length() > 4}[0].name", context, context.getRoot());
System.out.println(obj17);

//获取结果集合中的最后一个元素
Object obj18 = Ognl.getValue("#persons.{$ #this.name.length() > 4}[0].name", context, context.getRoot());
System.out.println(obj18);

//投影(projection)
Object obj19 = Ognl.getValue("#persons.{name}", context, context.getRoot());
System.out.println(obj19);

System.out.println("-----------------");
Object obj20 = Ognl.getValue("#persons.{#this.name.length()>4?'hello':#this.name}", context, context.getRoot());
System.out.println(obj20);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: