我是靠谱客的博主 怡然钥匙,这篇文章主要介绍Struts2框架中OGNL表达式的学习,现在分享给大家,希望可以做个参考。

Struts2框架中的表达式OGNL
ognl
1 什么是ognl
ognl表达式:全称为Object-Graph Navigation Language
是struts2默认的表达式语言 用来获取和设置java对象的属性

  1. 使用ognl的好处
    可以方便的操作对象属性的开源表达式语言
    3.使用ognl表达式的5点优势
    1),支持对象方法调用 如xxx.doSomeSpecial();
    2),支持类静态的方法调用和值访问,表达式的格式:@【类全名(包括包路径)】@【方法名 |值名】
    3),支持赋值操作和表达式串联
    4),访问OGNL上下文和actionContext
    5),操作集合对象
    例子部分:

例子1 上下文环境中使用ognl
import java.util.HashMap;
import java.util.Map;

import org.omg.CORBA.Context;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

/上下文环境中使用ognl/
public class Ognl2 {

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static void main(String[] args) { /*创建一个上下文Context对象,它是用保存多个对象一个环境 对象*/ Map<String,Object> context = new HashMap<String,Object>(); Person person1 = new Person(); person1.setName("张三"); Person person2 = new Person(); person2.setName("李四"); Person person3 = new Person(); person3.setName("王五"); /*person4不放到上下文环境中*/ Person person4 = new Person(); person4.setName("赵六"); /*将person1,person2,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); } catch (OgnlException e) { e.printStackTrace(); } }

}
class Person{
private String name;

复制代码
1
2
3
4
5
6
7
8
public String getName() { return name; } public void setName(String name) { this.name = name; }

}

结果分析及总结
对于使用上下文的OGNL,若不指定从哪一个对象中查找”name”属性,则OGNL直接从根对象(root)查找,
若指定查找对象(使用’#’号指定,如#person1),则从指定的对象中查找,若指定对象不在上下文中则
会抛出异常,换句话说就是是#person1.name形式指定查找对象则必须要保证指定对象在上下文环境中。

例子2 使用OGNL调用方法

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

/*
* 使用OGNL调用方法
*/
public class Ognl3 {
public static void main(String[] args){
//ognl提供的一个上下文类,它实现了Map接口
OgnlContext context = new OgnlContext();

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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); //调用静态方法 对于静态方法的调用,需要使用如下格式:@ClassName@method Object min = Ognl.getValue("@java.lang.Math@E",context,context.getRoot()); System.out.println("min(4,10) is:"+min); //调用静态变量 对于静态变量需要使用如下格式:@ClassName@field Object e = Ognl.getValue("@java.lang.Math@E",context,context.getRoot()); System.out.println("E is:"+e); } catch (OgnlException e) { e.printStackTrace(); } }

}
class People{
private String name;

复制代码
1
2
3
4
5
6
7
8
public String getName() { return name; } public void setName(String name) { this.name = name; }

}

结果分析及总结
使用OGNL调用方法十分简单,对于成员方法调用,秩序要给出方法的名称+(),若有参数,
直接写在括号内,与一般调用Java方法一致,对于静态方法的调用,需要使用如下格式:
@ClassName@method,对于静态变量需要使用如下格式:@ClassName@field

例子3 使用OGNL操作集合

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

//使用OGNL操作集合
public class Ognl4 {

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public static void main(String[] args) { OgnlContext context = new OgnlContext(); Classroom classroom=new Classroom(); classroom.getStudents().add("张三"); classroom.getStudents().add("李四"); classroom.getStudents().add("王五"); classroom.getStudents().add("赵六"); classroom.getStudents().add("小明"); Student student=new Student(); student.getContactWays().put("hme:","123"); student.getContactWays().put("com:","112"); student.getContactWays().put("mob:","119"); context.put("classroom",classroom); context.put("student",student); context.setRoot(classroom); try { //获取classroom的students集合 Object collection = Ognl.getValue("students", context,context.getRoot()); System.out.println("students集合:"+collection); //获得classroom的students集合 Object firstStudent = Ognl.getValue("students[0]",context,context.getRoot()); System.out.println("第一个学生是:"+firstStudent); //调用集合的方法 Object size = Ognl.getValue("students.size()",context,context.getRoot()); System.out.println("students collection size is:"+size); 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); System.out.println("-----------------------*_*-----------------------------"); } catch (OgnlException e) { e.printStackTrace(); } }

}
class Classroom{
private List students=new ArrayList();

复制代码
1
2
3
4
5
6
7
8
public List<String> getStudents() { return students; } public void setStudents(List<String> students) { this.students = students; }

}
class Student{
private Map

最后

以上就是怡然钥匙最近收集整理的关于Struts2框架中OGNL表达式的学习的全部内容,更多相关Struts2框架中OGNL表达式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部