java ognl表达式_[JavaWeb基础] 029.OGNL表达式介绍
1.OGNL概述
OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性,它旨在提供一个更高的更抽象的层次来对Java对象图进行导航。
OGNL表达式的基本单位是"导航链",一般导航链由如下几个部分组成:
属性名称(property)
方法调用(method invoke)
数组元素
所有的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 context = new HashMap();
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 students = new ArrayList();
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
}
class Student {
private Map contactWays = new HashMap();
public Map getContactWays() {
return contactWays;
}
public void setContactWays(Map 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 friends = new ArrayList();
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 getFriends() {
return friends;
}
public void setFriends(List friends) {
this.friends = friends;
}
@Override
public String toString() {
return "Humen [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
本站文章为宝宝巴士)
java ognl表达式_[JavaWeb基础] 029.OGNL表达式介绍相关推荐
- java第一周_从计算机基础到流程控制语句(if_else)
一.计算机基础 裸机 没有安装任何软件的计算机(无操作系统.无应用软件) 计算机的应用 1.科学计算 科学计算也称数值计算.计算机最开始是为解决科学研究和工程设计中遇到的大量数学问题的数值计算而研制的 ...
- 什么都不懂的学java难不难_零基础转行学java到底难不难
不少零基础的新手想要转行学习java可是不知道难度,南京学码思下面就给大家详细的说一说转行java的难度,希望能帮助到各位想要了解java的同学. 零基础转行学java到底难不难 新手转行学java到 ...
- java 回归遍历_回归基础:代码遍历
java 回归遍历 This article guides you through the basics of regression by showing code and thorough expl ...
- 零基础自学java的难处_零基础自学Java 在学习中要注意哪些问题
如果是零基础自学Java编程,在学习过程中有很多要注意的问题,想要学好学精必然是件难事,并且可能会走弯路浪费很多时间,短时间内是不可能学成参加工作的,想要成为专业的Java程序员并不容易,技术过硬尤为 ...
- 学java 的要点_零基础学Java,掌握Java的基础要点
对于程序员群体来说,了解一定的技巧会对学习专业技能更有帮助,也更有助于在自己的职业发展中处于有利地位,无限互联Java培训专家今天就为大家总结Java程序员入门时需要掌握的基础要点: 掌握静态方法和属 ...
- java培训学费_零基础Java培训大概多少钱
Java培训多少钱,这个问题是我们准备学习Java的小伙伴比较关注的,特别是从零基础开始来学习Java的小伙伴,对这个行业不太了解,为了防止上当受骗,了解清楚还是比较好的,那么零基础Java培训大概多 ...
- java lambda表达式_「JAVA8」- Lambda 表达式
Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性. Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中). 使用 Lambda 表达式可以使代码变的更加 ...
- java 百度爬虫_零基础写Java知乎爬虫之先拿百度首页练练手
上一集中我们说到需要用Java来制作一个知乎爬虫,那么这一次,我们就来研究一下如何使用代码获取到网页的内容. 首先,没有HTML和CSS和JS和AJAX经验的建议先去W3C(点我点我)小小的了解一下. ...
- 学java还是安卓_零基础学安卓好还是学java好
零基础学安卓好还是学java好 关于Java的学习很多同学都有这样或者那样的疑问,比如我是自学Java好呢还是参加Java培训好呢?Java好学吗?安卓和java哪个难学等等.今天我们就先简单的来看两 ...
最新文章
- Windows10搭建ftp服务
- MapReduce计数器
- VxWorks系统BSP配置文件及生成下载
- apollo集群部署_egg框架对接Apollo
- linux 权限管理命令
- 生成内核版本号头文件的方法
- 传奇计算机教室管理软件,联想传奇电子教室(联想电子教室软件)V15.89 官方版
- vue项目-jQuery中Nicescroll滚动条插件的用法
- JavaScript基础--DOM部分01--李南江
- mysql多条语句union_Mysql同时执行多个select语句——union
- 结节性硬化症会一直病变吗?结节性硬化最晚多大发病?
- 初中数学抽象教学的案例_初中数学教学案例分析-初中数学教学案例分析100例...
- 攻破 JAVA NIO 技术壁垒( 下 ) 2017-09-02 ImportNew (点击上方公众号,可快速关注) 来源:朱小厮, blog.csdn.net/u013256816/articl
- SUM分析函数应用缺陷及解决办法
- 龙芯2K1000开发板资料1.2版更新了
- 2021练习题Python的
- 记:在daemon.json中添加“live-restore“: false之后,docker无法启动
- jiegputo matlab转置,matlab实现用免疫克隆算法求二元函数的最优值(附源码)
- Python之路(基本数据类型及操作)
- 数字签名的作用和功能