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表达式,例如下面的代码片断所示:
<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) { 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"); person person4 = new person(); person4.setname("zhaoliu"); context.put("person1", person1); context.put("person2", person2); context.put("person3", person3); try { object value = ognl.getvalue("name", context, person2); system.out.println("ognl expression \"name\" evaluation is : " + value); object value2 = ognl.getvalue("#person2.name", context, person2); system.out.println("ognl expression \"#person2.name\" evaluation is : " + value2); object value3 = ognl.getvalue("#person1.name", context, person2); system.out.println("ognl expression \"#person1.name\" evaluation is : " + value3); object value4 = ognl.getvalue("name", context, person4); system.out.println("ognl expression \"name\" evaluation is : " + value4); object value5 = ognl.getvalue("#person4.name", context, person4); system.out.println("ognl expression \"person4.name\" evaluation is : " + value5); // 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) { 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); object collection = ognl.getvalue("students", context, context.getroot()); system.out.println("students collection is :" + collection); 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); object createmapcollection = ognl.getvalue("#{'key1':'value1','key2':'value2'}", context, context.getroot()); system.out.println(createmapcollection); } }
总结
以上就是本文关于ognl表达式基本语法与用法详解的全部内容,希望对大家有所帮助,有问题的话可以随时留言,小编会及时回复大家的。感兴趣的朋友可以继续参阅:struts2 通过ognl表达式实现投影 、 struts2 ognl表达式实例详解等。
下一篇: 使用java将动态网页生成静态网页示例