1.OGNL概述

OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性,它旨在提供一个更高的更抽象的层次来对Java对象图进行导航。

OGNL表达式的基本单位是"导航链",一般导航链由如下几个部分组成:

  1. 属性名称(property)
  2. 方法调用(method invoke)
  3. 数组元素

所有的OGNL表达式都基于当前对象的上下文来完成求值运算,链的前面部分的结果将作为后面求值的上下文。例如:names[0].length()。

2.OGNL操作属性

public class OGNL1 {
public static void main(String[] args) {
/** 创建一个Person对象 **/
Person person = new Person();
person.setName("zhangsan");
try {
/** 从person对象中获取name属性的值 **/
Object value = Ognl.getValue("name", person);
System.out.println(value);
} catch (OgnlException e) {
e.printStackTrace();
}
}
} /** model 实体类 **/
class Person {
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

3.OGNL访问context

public class OGNL1 {
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"); /** 将person1、person2、person3添加到环境中(上下文中) **/
context.put("person1", person1);
context.put("person2", person2);
context.put("person3", person3); try {
/** 获取根对象的"name"属性值 **/
/** 从根对象person2,直接从person2中查找**/
Object value = Ognl.getValue("name", context, person2);
System.out.println("ognl expression \"name\" evaluation is : " + value); /** 获取根对象的"name"属性值 **/
/** 指定#person2,直接从person1中查找**/
Object value2 = Ognl.getValue("#person2.name", context, person2);
System.out.println("ognl expression \"#person2.name\" evaluation is : "+ value2); /** 获取person1对象的"name"属性值 **/
/** 指定#person1,直接从person1中查找**/
Object value3 = Ognl.getValue("#person1.name", context, person2);
System.out.println("ognl expression \"#person1.name\" evaluation is : "+ value3); /** 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 **/
/** 从根对象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();
}
}
}

4.OGNL调用方法

public class OGNL2 {
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);
} catch (OgnlException e) {
e.printStackTrace();
}
}
}

5.OGNL操作集合

public class OGNL3 {
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);"#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);
}
} class Classroom {
private List<String> students = new ArrayList<String>(); public List<String> getStudents() {
return students;
} public void setStudents(List<String> students) {
this.students = students;
}
} class Student {
private Map<String, Object> contactWays = new HashMap<String, Object>(); public Map<String, Object> getContactWays() {
return contactWays;
} public void setContactWays(Map<String, Object> contactWays) {
this.contactWays = contactWays;
}
}

6.OGNL过滤集合

public class OGNL4 {
public static void main(String[] args) throws Exception {
OgnlContext context = new OgnlContext();
Humen humen = new Humen();
humen.setName("qiuyi");
humen.setSex("n");
humen.setAge(22);
humen.getFriends().add(new Humen("zhangsan", "n", 22));
humen.getFriends().add(new Humen("lisi", "f", 21));
humen.getFriends().add(new Humen("wangwu", "n", 23));
humen.getFriends().add(new Humen("zhaoliu", "n", 22));
humen.getFriends().add(new Humen("qianqi", "n", 22));
humen.getFriends().add(new Humen("sunba", "f", 20));
humen.getFriends().add(new Humen("yangqiu", "f", 25));
context.put("humen", humen);
context.setRoot(humen);
/** OGNL过滤集合的语法为:collection.{? expression} **/
Object filterCollection = Ognl.getValue("friends.{? #this.name.length() > 7}", context, context.getRoot());
System.out.println("filterCollection is :" + filterCollection);
System.out.println("--------------------------飘逸的分割线--------------------------");
/** OGNL投影集合的语法为:collection.{expression} **/
Object projectionCollection = Ognl.getValue("friends.{name}", context, context.getRoot());
System.out.println("projectionCollection is :" + projectionCollection);
}
} class Humen {
private String name;
private String sex;
private int age;
private List<Humen> friends = new ArrayList<Humen>(); public Humen() {
} public Humen(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public List<Humen> getFriends() {
return friends;
} public void setFriends(List<Humen> friends) {
this.friends = friends;
} @Override
public String toString() {
return "Humen [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士)

转载自【宝宝巴士SuperDo团队】原文链接: http://www.cnblogs.com/superdo/p/5080281.html

[JavaWeb基础] 029.OGNL表达式介绍的更多相关文章

  1. OGNL表达式介绍

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存 ...

  2. (转)OGNL表达式介绍

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存 ...

  3. [C/C++11语法]_[0基础]_[lamba 表达式介绍]

    场景 lambda 表达式在非常多语言里都有一席之地,由于它的原因,能够在函数里高速定义一个便携的函数,或者在函数參数里直接高速构造和传递. 它能够说是匿名函数对象,一般仅仅适用于某个函数内,仅仅做暂 ...

  4. javaweb基础(29)_EL表达式

    一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...

  5. javaweb基础(10)_HttpServletRequest原理介绍

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  6. JavaWeb框架SSH_Struts2_(四)----->表达式语言OGNL

    1. 表达式语言OGNL OGNL简介 OGNL基本语法 常量 操作符 OGNL表达式 OGNL基础 OGNL上下文 OGNL值栈 OGNL的访问 2. 具体内容 2.1 OGNL简介 OGNL(Ob ...

  7. JavaWeb框架_Struts2_(四)----->表达式语言OGNL

      2. 表达式语言OGNL 2.1 OGNL简介 OGNL(Object-Graph Navigation Language)对象图导航语言的缩写,OGNL是一种表达式语言(Expression L ...

  8. JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总

    一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出.       1,JSTL标签总结: a).JSTL标签有什么用?          JSTL是由JCP(Java Commu ...

  9. JavaWeb基础知识总结

    JavaWeb基础知识总结.   1.web服务器与HTTP协议 Web服务器 l WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. l Internet上供 ...

随机推荐

  1. 在Jetson TX2上捕获、显示摄像头视频

    参考文章:How to Capture and Display Camera Video with Python on Jetson TX2 与参考文章大部分都是相似的,如果不习惯看英文,可以看看我下 ...

  2. P4370 [Code+#4]组合数问题2

    题目要求当\(0\leq a\leq b\leq n\)时,\(k\)个\(\tbinom{b}{a}\)的和的最大值 观察杨辉三角形,可以发现,最大的\(\tbinom{b}{a}\),为\(\tb ...

  3. 洛谷P1771 方程的解

    P1771 方程的解 都知道这个题可以用隔板法做 把这个\(g(x)\)想象为.....\(g(x)\)个苹果? 因为解是正整数,所以给这些"苹果"分组的时候每组最少有一个 然后我 ...

  4. 24-Java-Spring框架(二)

    Spring框架的了解.SpringIOC的部分内容请阅读23-Java-Spring框架(二) 三.Spring Web MVC(Model View Controller) 1.SpringMVC ...

  5. 使用django开发论坛输出调试信息时附加远程客户端IP地址!

    前言 最近使用django开发了个匿名社区(哈士奇社区 4nmb.com),但是有个问题一直困扰我半天,就是如何在django调试信息上输出远程客户端的真实IP地址,在网上找了很多资料也没见人遇到过, ...

  6. checked 完整版全选,单选,反选

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...

  7. msf的rpc和json-rpc,我该选择哪个?

    msf的rpc有两种调用方式,那么我们应该调用哪一个呢? 其中restful接口暂且不谈,这个rest api其实是简单对接了一下msf的后端数据库,这个自己也能读数据库来做,这个以后有时间再谈 首先 ...

  8. MES系统的模型结构和主要功能(二)

    上一节,我们主要说了Mes系统是什么,以及它的特点和难点,本节,再来讨论一下一个合格的MES系统的模型结构和基本功能. 现代工厂的快速发展,对MES系统提出了更高的要求,其必须满足范围广泛的任务要求, ...

  9. puamap是什么意思

    artists map 定义格式:[puamap代号 名] 相关属性: 1.FIGHT 2.SAFE 安全区域 3.DARK 4.NEEDHOLE 配合mapinfo里 x,y -> x1,y1 ...

  10. 【MySQL基础总结】常用函数库

    常用函数库 数学函数 分类及含义 示例 字符串函数 分类及含义 示例 日期时间函数 分类及含义 示例 条件判断函数 分类及含义 示例 系统函数 分类及含义 加密函数 分类及定义 其他常用函数 分类及含 ...