在上篇博客,咱们一块儿看了《ognl概念和原理详解》,咱们大约的知道了ognl的基本实现原理和一些基本概念,这节咱们一块儿来学习一下OGNL表达式的基本语法和基本用法,首先咱们一块儿来看一下OGNL中的#、%和$符号。html
一.OGNL中的#、%和$符号java
#、%和$符号在OGNL表达式中常常出现,而这三种符号也是开发者不容易掌握和理解的部分。在这里咱们简单介绍它们的相应用途。数据库
1.#符号的三种用法express
1)访问非根对象属性,例如示例中的#session.msg表达式,因为Struts 2中值栈被视为根对象,因此访问其余非根对象时,须要加#前缀。实际上,#至关于ActionContext. getContext();#session.msg表达式至关于ActionContext.getContext().getSession(). getAttribute("msg") 。数组
2)用于过滤和投影(projecting)集合,如示例中的persons.{?#this.age>20}。session
3) 用来构造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。app
2.%符号框架
%符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。以下面的代码所示:学习
<h3>构造Map</h3>this
<s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />
<p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>
<p>不使用%:<s:url value="#foobar['foo1']" /></p>
<p>使用%:<s:url value="%{#foobar['foo1']}" /></p>
运行界面以下所示。
he value of key "foo1" is bar1
不使用%:#foobar['foo1']
使用%:bar1
3.$符号
$符号主要有两个方面的用途。
1) 在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。
2) 在Struts 2框架的配置文件中引用OGNL表达式,例以下面的代码片段所示:
- <validators>
-
- <field name="intb">
-
- <field-validator type="int">
-
- <param name="min">10</param>
-
- <param name="max">100</param>
-
- <message>BAction-test校验:数字必须为${min}为${max}之间!</message>
-
- </field-validator>
-
- </field>
-
- </validators>
二.咱们一块儿看一下OGNL经常使用表达式:
1. 当使用OGNL调用静态方法的时候,须要按照以下语法编写表达式:
@package.classname@methodname(parameter)
2. 对于OGNL来讲,java.lang.Math是其的默认类,若是调用java.lang.Math的静态方法时,无需指定类的名字,好比:@@min(4, 10);
3. 对于OGNL来讲,数组与集合是同样的,都是经过下标索引来去访问的。
获取List:<s:property value="testList"/><br> 获取List中的某一个元素(能够使用相似于数组中的下标获取List中的内容): <s:property value="testList[0]"/><br> 获取Set:<s:property value="testSet"/><br> 获取Set中的某一个元素(Set因为没有顺序,因此不能使用下标获取数据): <s:property value="testSet[0]"/><br> × 获取Map:<s:property value="testMap"/><br> 获取Map中全部的键:<s:property value="testMap.keys"/><br> 获取Map中全部的值:<s:property value="testMap.values"/><br> 获取Map中的某一个元素(能够使用相似于数组中的下标获取List中的内容): <s:property value="testMap['m1']"/><br> 获取List的大小:<s:property value="testSet.size"/><br> |
4. 使用OGNL来处理映射(Map)的语法格式以下所示:
#{‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘key3’: ‘value3’};
5. 过滤(filtering):collection.{? expression}
6. OGNL针对集合提供了一些伪属性(如size,isEmpty),让咱们能够经过属性的方式来调用方法(本质缘由在于集合当中的不少方法并不符合JavaBean的命名规则),但我么你依然还能够经过调用方法来实现与伪属性相同的目的。
7. 过滤(filtering),获取到集合中的第一个元素:collection.{^ expression}
8. 过滤(filtering),获取到集合中的最后一个元素:collection.{& expression}
9. 在使用过滤操做时,咱们一般都会使用#this,该表达式用于表明当前正在迭代的集合中的对象(联想加强的for循环)
10. 投影(projection):collection.{expression}
11. 过滤与投影之间的差异:类比于数据库中的表,过滤是取行的操做,而投影是取列的操做。 具体举例以下:
利用选择获取List中成绩及格的对象:<s:property value="stus.{?#this.grade>=60}"/><br> 利用选择获取List中成绩及格的对象的username: <s:property value="stus.{?#this.grade>=60}.{username}"/><br> 利用选择获取List中成绩及格的第一个对象的username: <s:property value="stus.{?#this.grade>=60}.{username}[0]"/><br> 利用选择获取List中成绩及格的第一个对象的username: <s:property value="stus.{^#this.grade>=60}.{username}"/><br> 利用选择获取List中成绩及格的最后一个对象的username: <s:property value="stus.{$#this.grade>=60}.{username}"/><br> 利用选择获取List中成绩及格的第一个对象而后求大小: <s:property value="stus.{^#this.grade>=600}.{username}.size"/><br> |
12. 在Struts2中,根对象就是ValueStack。在Struts2的任何流程当中,ValueStack中的最顶层对象必定是Action对象。
13. parameters,#parameters.username
request, #request.username
session, #session.username
application, #application.username
attr, #attr.username
以上几个对象叫作“命名对象”。
14. 访问静态方法或是静态成员变量的改进。
@vs @method
15. 关于Struts2标签库属性值的%与#号的关系:
1). 若是标签的属性值是OGNL表达式,那么无需加上%{}。
2). 若是标签的属性值是字符串类型,那么在字符串当中凡是出现的%{}都会被解析成OGNL表达式,解析完毕后再与其余的字符串进行拼接构造出最后的字符串值。
3). 咱们能够在全部的属性值上加%{},这样若是该属性值是OGNL表达式,那么标签处理类就会将%{}忽略掉。
最后一块儿用代码说话,简单的看一下ognl操做的示例:
1)上下文环境中使用OGNL
- public static void main(String[] args)
- {
- /* 建立一个上下文Context对象,它是用保存多个对象一个环境 对象*/
- Map<String , Object> context = new HashMap<String , Object>();
-
- Person person1 = new Person();
- person1.setName("zhangsan");
-
- Person person2 = new Person();
- person2.setName("lisi");
-
- Person person3 = new Person();
- person3.setName("wangwu");
-
- /* person4不放入到上下文环境中*/
- Person person4 = new Person();
- person4.setName("zhaoliu");
-
- /* 将person一、person二、person3添加到环境中(上下文中)*/
- context.put("person1", person1);
- context.put("person2", person2);
- context.put("person3", person3);
-
- try
- {
- /* 获取根对象的"name"属性值*/
- Object value = Ognl.getValue("name", context, person2);
- System.out.println("ognl expression \"name\" evaluation is : " + value);
-
- /* 获取根对象的"name"属性值*/
- Object value2 = Ognl.getValue("#person2.name", context, person2);
- System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);
-
- /* 获取person1对象的"name"属性值*/
- Object value3 = Ognl.getValue("#person1.name", context, person2);
- System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);
-
- /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中*/
- Object value4 = Ognl.getValue("name", context, person4);
- System.out.println("ognl expression \"name\" evaluation is : " + value4);
-
- /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中*/
- Object value5 = Ognl.getValue("#person4.name", context, person4);
- System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);
-
- /* 获取person4对象的"name"属性,注意person4对象不在上下文中*/
- // Object value6 = Ognl.getValue("#person4.name", context, person2);
- // System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);
-
- }
2)使用OGNL调用方法
- public static void main(String[] args)
- {
- /* OGNL提供的一个上下文类,它实现了Map接口*/
- OgnlContext context = new OgnlContext();
-
- People people1 = new People();
- people1.setName("zhangsan");
-
- People people2 = new People();
- people2.setName("lisi");
-
- People people3 = new People();
- people3.setName("wangwu");
-
- context.put("people1", people1);
- context.put("people2", people2);
- context.put("people3", people3);
-
- context.setRoot(people1);
-
- try
- {
- /* 调用 成员方法*/
- Object value = Ognl.getValue("name.length()", context, context.getRoot());
- System.out.println("people1 name length is :" + value);
-
- Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot());
- System.out.println("people2 name upperCase is :" + upperCase);
-
- Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot());
- System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);
-
- /* 调用静态方法*/
- Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());
- System.out.println("min(4,10) is :" + min);
-
- /* 调用静态变量*/
- Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());
- System.out.println("E is :" + e);
- }
3)使用OGNL操做集合
- public static void main(String[] args) throws Exception
- {
- OgnlContext context = new OgnlContext();
-
- Classroom classroom = new Classroom();
- classroom.getStudents().add("zhangsan");
- classroom.getStudents().add("lisi");
- classroom.getStudents().add("wangwu");
- classroom.getStudents().add("zhaoliu");
- classroom.getStudents().add("qianqi");
-
- Student student = new Student();
- student.getContactWays().put("homeNumber", "110");
- student.getContactWays().put("companyNumber", "119");
- student.getContactWays().put("mobilePhone", "112");
-
- context.put("classroom", classroom);
- context.put("student", student);
- context.setRoot(classroom);
-
- /* 得到classroom的students集合*/
- Object collection = Ognl.getValue("students", context, context.getRoot());
- System.out.println("students collection is :" + collection);
-
- /* 得到classroom的students集合*/
- Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot());
- System.out.println("first student is : " + firstStudent);
-
- /* 调用集合的方法*/
- Object size = Ognl.getValue("students.size()", context, context.getRoot());
- System.out.println("students collection size is :" + size);
-
- System.out.println("--------------------------飘逸的分割线--------------------------");
-
- Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot());
- System.out.println("mapCollection is :" + mapCollection);
-
- Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot());
- System.out.println("the first element of contactWays is :" + firstElement);
-
- System.out.println("--------------------------飘逸的分割线--------------------------");
-
- /* 建立集合*/
- Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot());
- System.out.println(createCollection);
-
- /* 建立Map集合*/
- Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot());
- System.out.println(createMapCollection);
-
- }
- }