struts2 OGNL的用法介绍

在上篇博客,我们一起看了《ognl概念和原理详解》,我们大约的知道了ognl的基本实现原理和一些基本概念,这节我们一起来学习一下OGNL表达式的基本语法和基本用法,首先我们一起来看一下OGNL中的#%$符号

一.OGNL中的#%$符号

      #%$符号在OGNL表达式中经常出现,而这三种符号也是开发者不容易掌握和理解的部分。在这里我们简单介绍它们的相应用途。

1#符号的三种用法

   1访问非根对象属性,例如示例中的#session.msg表达式,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀。实际上,#相当于ActionContext. getContext()#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute("msg") 

   2用于过滤和投影(projecting)集合,如示例中的persons.{?#this.age>20}

   3 用来构造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}

2%符号

      %符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。如下面的代码所示:

<h3>构造Map</h3>

    <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表达式,例如下面的代码片断所示:

[html]  view plain copy print ?
  1. <validators>  
  2.   
  3.     <field name="intb">  
  4.   
  5.             <field-validator type="int">  
  6.   
  7.             <param name="min">10</param>  
  8.   
  9.             <param name="max">100</param>  
  10.   
  11.             <message>BAction-test校验:数字必须为${min}为${max}之间!</message>  
  12.   
  13.         </field-validator>  
  14.   
  15.     </field>  
  16.   
  17. </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

[java]  view plain copy print ?
  1. public static void main(String[] args)  
  2.     {  
  3.         /* 创建一个上下文Context对象,它是用保存多个对象一个环境 对象*/  
  4.         Map<String , Object> context = new HashMap<String , Object>();  
  5.   
  6.         Person person1 = new Person();  
  7.         person1.setName("zhangsan");  
  8.          
  9.         Person person2 = new Person();  
  10.         person2.setName("lisi");  
  11.   
  12.         Person person3 = new Person();  
  13.         person3.setName("wangwu");  
  14.   
  15.         /* person4不放入到上下文环境中*/  
  16.         Person person4 = new Person();  
  17.         person4.setName("zhaoliu");  
  18.   
  19.         /* 将person1、person2、person3添加到环境中(上下文中)*/  
  20.         context.put("person1", person1);  
  21.         context.put("person2", person2);  
  22.         context.put("person3", person3);  
  23.   
  24.         try  
  25.         {  
  26.             /* 获取根对象的"name"属性值*/  
  27.             Object value = Ognl.getValue("name", context, person2);  
  28.             System.out.println("ognl expression \"name\" evaluation is : " + value);  
  29.   
  30.             /* 获取根对象的"name"属性值*/  
  31.             Object value2 = Ognl.getValue("#person2.name", context, person2);  
  32.             System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);  
  33.   
  34.             /* 获取person1对象的"name"属性值*/  
  35.             Object value3 = Ognl.getValue("#person1.name", context, person2);  
  36.             System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);  
  37.   
  38.             /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中*/  
  39.             Object value4 = Ognl.getValue("name", context, person4);  
  40.             System.out.println("ognl expression \"name\" evaluation is : " + value4);  
  41.   
  42.             /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中*/  
  43.             Object value5 = Ognl.getValue("#person4.name", context, person4);  
  44.             System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);  
  45.   
  46.             /* 获取person4对象的"name"属性,注意person4对象不在上下文中*/  
  47.             // Object value6 = Ognl.getValue("#person4.name", context, person2);  
  48.            // System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);  
  49.   
  50.         }  


2使用OGNL调用方法

[java]  view plain copy print ?
  1. public static void main(String[] args)  
  2.     {  
  3.         /* OGNL提供的一个上下文类,它实现了Map接口*/  
  4.         OgnlContext context = new OgnlContext();  
  5.   
  6.         People people1 = new People();  
  7.         people1.setName("zhangsan");  
  8.   
  9.         People people2 = new People();  
  10.         people2.setName("lisi");  
  11.   
  12.         People people3 = new People();  
  13.         people3.setName("wangwu");  
  14.   
  15.         context.put("people1", people1);  
  16.         context.put("people2", people2);  
  17.         context.put("people3", people3);  
  18.          
  19.         context.setRoot(people1);  
  20.   
  21.         try  
  22.         {  
  23.             /* 调用 成员方法*/  
  24.             Object value = Ognl.getValue("name.length()", context, context.getRoot());  
  25.             System.out.println("people1 name length is :" + value);  
  26.              
  27.             Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot());  
  28.             System.out.println("people2 name upperCase is :" + upperCase);  
  29.   
  30.             Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot());  
  31.             System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);  
  32.   
  33.             /* 调用静态方法*/  
  34.             Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());  
  35.             System.out.println("min(4,10) is :" + min);  
  36.   
  37.             /* 调用静态变量*/  
  38.             Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());  
  39.             System.out.println("E is :" + e);  
  40.         }  


3使用OGNL操作集合

[java]  view plain copy print ?
  1. public static void main(String[] args) throws Exception  
  2.     {  
  3.         OgnlContext context = new OgnlContext();  
  4.          
  5.         Classroom classroom = new Classroom();  
  6.         classroom.getStudents().add("zhangsan");  
  7.         classroom.getStudents().add("lisi");  
  8.         classroom.getStudents().add("wangwu");  
  9.         classroom.getStudents().add("zhaoliu");  
  10.         classroom.getStudents().add("qianqi");  
  11.          
  12.         Student student = new Student();  
  13.         student.getContactWays().put("homeNumber""110");  
  14.         student.getContactWays().put("companyNumber""119");  
  15.         student.getContactWays().put("mobilePhone""112");  
  16.          
  17.         context.put("classroom", classroom);  
  18.         context.put("student", student);  
  19.         context.setRoot(classroom);  
  20.   
  21.         /* 获得classroom的students集合*/  
  22.         Object collection = Ognl.getValue("students", context, context.getRoot());  
  23.         System.out.println("students collection is :" + collection);  
  24.   
  25.         /* 获得classroom的students集合*/  
  26.         Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot());  
  27.         System.out.println("first student is : " + firstStudent);  
  28.   
  29.         /* 调用集合的方法*/  
  30.         Object size = Ognl.getValue("students.size()", context, context.getRoot());  
  31.         System.out.println("students collection size is :" + size);  
  32.   
  33.         System.out.println("--------------------------飘逸的分割线--------------------------");  
  34.          
  35.         Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot());  
  36.         System.out.println("mapCollection is :" + mapCollection);  
  37.   
  38.         Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot());  
  39.         System.out.println("the first element of contactWays is :" + firstElement);  
  40.   
  41.         System.out.println("--------------------------飘逸的分割线--------------------------");  
  42.   
  43.         /* 创建集合*/  
  44.         Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot());  
  45.         System.out.println(createCollection);  
  46.   
  47.         /* 创建Map集合*/  
  48.         Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot());  
  49.         System.out.println(createMapCollection);  
  50.   
  51.     }  
  52. }  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值