OGNL

一.概述

以下内容摘自Ognl的官网:

  OGNL stands for Object-Graph Navigation Language; it is an expression language for getting and setting properties of Java objects, plus other extras such as list projection and selection and lambda expressions. You use the same expression for both getting and setting the value of a property.

  The Ognl class contains convenience methods for evaluating OGNL expressions. You can do this in two stages, parsing an expression into an internal form and then using that internal form to either set or get the value of a property; or you can do it in a single stage, and get or set a property using the String form of the expression directly.

OGNL的全称叫做对象图导航语言,是一个用于获取与设置Java对象的表达式语言,还附加一些例如集合投影、过滤、Lambda表达式的功能。你可以使用同一个表达式实现属性的赋值或取值。

Ognl的类中包含的很方便的方法实现OGNL表达式的赋值。实现这个功能你需要两步,解析一个表达式使之称为一种内部的形式然后再用这种内部的形式对属性赋值或取值;或者你可以实现这个功能只用一步,直接用字符串来实现属性的取值或者赋值。

二. 代码案例

2.1 定义两个JavaBean

public class Dog {
private String name;
//setter and getter
}
public class Person {
private String name;
private Dog dog;
//setter and getter
}

2.2 测试用例

2.2.1 获取属性

@Test
public void test1(){
Dog dog = new Dog();
dog.setName("wangcai"); Dog dog1 = new Dog();
dog1.setName("wangwang"); Person person = new Person();
person.setName("zhagnsan");
person.setDog(dog1); OgnlContext context = new OgnlContext(); //实例化一个Ognl的上下文
context.put("person", person);
context.put("dog", dog); context.setRoot(person); //将person设置为ongl的根
try {
//使用Ognl.paraseExpression(str)来解析ognl表达式。dog是person这个根对象的属性,可以直接写属性名
Object obj = Ognl.getValue(Ognl.parseExpression("dog.name"), context, context.getRoot());
System.out.println(obj);
System.out.println("================================="); //#person.name获取根对象person的name属性, #person代表的person这个对象,如要拿到某个对象要使用#
obj = Ognl.getValue(Ognl.parseExpression("#person.name"), context, context.getRoot());
System.out.println(obj);
System.out.println("================================="); //#dog表示的是context.put("dog", dog)这个对象
obj = Ognl.getValue("#dog.name", context, context.getRoot());
System.out.println(obj);
System.out.println("=================================="); //此时name表示的是person这个根对象的属性,直接写上属性名即可
obj = Ognl.getValue("name", context, context.getRoot());
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}

输出结果:

wangwang

================================================

zhagnsan

================================================

wangcai

================================================

zhagnsan

2.2.2 方法调用

@Test
public void test2(){
try{
Person person = new Person();
person.setName("zhagnsan"); OgnlContext context = new OgnlContext();
context.put("person", person); context.setRoot(person); //可以直接调用方法,name是根对象person的属性
Object obj = Ognl.getValue("name.toUpperCase().length()", context, context.getRoot());
System.out.println(obj);
System.out.println("=================================");
} catch (Exception e) {
e.printStackTrace();
}
}

输出结果: 8

2.2.3 调用静态方法

@Test
public void test3(){
OgnlContext context = new OgnlContext();
try {
//调用静态方法的语法: @类的全名@静态方法名
Object obj = Ognl.getValue("@java.lang.Integer@toBinaryString(10)", context, context.getRoot());
System.out.println(obj);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:1010

2.2.4 java.lang.Math的处理

@Test
public void test4(){
OgnlContext context = new OgnlContext();
try {
//java.lang.Math为ognl的内置对象,@@即可表示对java.lang.Math的引用
Object obj = Ognl.parseExpression("@@min(10, 4)");
obj = Ognl.getValue(obj, context, context.getRoot());
System.out.println(obj);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:4

2.2.5 List集合的处理

@Test
public void test5(){
OgnlContext context = new OgnlContext();
try {
Object obj = Ognl.getValue("{'aa', 'bb', 'cc'}", context, ontext.getRoot());
System.out.println(obj instanceof List);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:true

@Test
public void test7(){
OgnlContext context = new OgnlContext();
try {
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("welcome");
context.put("list", list); Object obj = Ognl.getValue("#list[1]", context, context. getRoot());
System.out.println(obj);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:world

2.2.6 Map集合

@Test
public void testMap(){
OgnlContext context = new OgnlContext();
try {
/*
Map<String, String> map = new HashMap<String, String>();
map.put("aa", "abc");
map.put("bb", "zz");
map.put("cc", "gg"); context.put("map", map); Object obj = Ognl.getValue("#map['aa']", context, context.getRoot());
*/
Object obj = Ognl.getValue("#{'aa':'zz', 'bb':'cc', 'dd':'xx'}['aa']", context, context.getRoot());
System.out.println(obj);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:zz

2.2.7 过滤

//过滤,语法格式Collection.{? expr},返回的是一个集合
@Test
public void testFilter(){
OgnlContext context = new OgnlContext();
try {
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("welcome");
list.add("helloworld");
context.put("list", list); /*
Object obj = Ognl.getValue("#list.{? #this.length() gt 5}", context, context.getRoot());
System.out.println(obj);
*/
//Object obj = Ognl.getValue("#list.{? #this.length() gt 5}.size()", context, context.getRoot()); //size是伪属性,isEmpty也是伪属性
Object obj = Ognl.getValue("#list.{? #this.length() gt 5}.size", context, context.getRoot());
System.out.println(obj);
System.out.println("==============================");
/**
* collectoin.{^ expr} 获取到过滤后的集合中的第一个元素
*/
obj = Ognl.getValue("#list.{^ #this.length() > 5}", context, context.getRoot());
System.out.println(obj);
System.out.println("=============================="); /**
* collectoin.{$ expr} 获取到过滤后的集合中的最后一个元素
*/
obj = Ognl.getValue("#list.{$ #this.length() > 5}[0]", context, context.getRoot());
System.out.println(obj);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:

2

==============================

[welcome]

==============================

helloworld

2.2.8 投影

/**
* 投影 collection.{expr}, 返回的依然是集合,并且长度不会变化,仅仅只是属性的个数发生的变化。拿数据库来类比:过滤相当于取行的操作(行数的可能会变化),投影相当于数据库的取列的操作。
*/
@Test
public void testProjectoin(){
OgnlContext context = new OgnlContext();
try {
List<Person> list = new ArrayList<Person>();
Person p1 = new Person();
p1.setName("zhangsan"); Person p2 = new Person();
p2.setName("lisi"); Person p3 = new Person();
p3.setName("wangwu");
list.add(p1);
list.add(p2);
list.add(p3);
context.put("list", list); /*//将List中每个Person的name取出来,放到一个集合中
//Object obj = Ognl.getValue("#list.{name}", context, context.getRoot());
Object obj = Ognl.getValue("#list.{#this.name}", context, context.getRoot()); //与上一行代码实现相同的功能
System.out.println(obj);
*/
//当名字长度大于5,就用helloworld来替换,否则保持不变
//Object obj = Ognl.getValue("#list.{#this.name.length() > 5 ? 'helloworld' : #this.name}", context, context.getRoot());
Object obj = Ognl.getValue("#list.{name.length() > 5 ? 'helloworld' : name}", context, context.getRoot()); //与上一行代码实现相同的功能
System.out.println(obj);
} catch (OgnlException e) {
e.printStackTrace();
}
}

输出结果:

[helloworld, lisi, helloworld]

下一篇:Struts2对Ognl的支持

OGNL语言的更多相关文章

  1. java struts2入门学习--OGNL语言基本用法

    一.知识点学习 1.struts2中包含以下6种对象,requestMap,sessionMap,applicationMap,paramtersMap,attr,valueStack; 1)requ ...

  2. java struts2入门学习--防止表单重复提交.OGNL语言学习

    一.知识点回顾 防止表单重复提交核心思想: 客户端和服务器端和写一个token,比较两个token的值相同,则非重复提交;不同,则是重复提交. 1.getSession三种方式比较: request. ...

  3. java struts2入门学习--OGNL语言常用符号和常用标签学习

    一.OGNL常用符号(接上一篇文章): 1.#号 1)<s:property value="#request.username"/> 作用于struts2的域对象,而不 ...

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

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

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

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

  6. struts2学习笔记--OGNL表达式1

    struts2标签库主要使用的是OGNL语言,类似于El表达式,但是强大得多,它是一种操作对象属性的表达式语言,OGNL有自己的优点: 能够访问对象的方法,如list.size(); 能够访问静态属性 ...

  7. el 表达式 和 ognl表达式

    el (expression language) el 基础操作符 el 能够隐含对象(就是可以直接访问的) el 的两种使用方式,第二种好像在jsp中没有什么用,主要用于jsf el能够访问的对象( ...

  8. Struts2对Ognl的支持

                                                      Struts2对Ognl的支持 一. 写作背景 由于工作性质的变化,最近一直在研究struts2,从 ...

  9. Struts2(3) —— 数据处理

    Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:ValueStack一种数据结构,操作数据的方式为 ...

随机推荐

  1. Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. SSM框架学习之高并发秒杀业务--笔记3-- Service层

    上一节中已经包DAO层编写完成了,所谓的DAO层就是所有和数据访问的部分都应该放在这个层里,它负责与数据库打交道.对于一个web项目来说,大概由这几部分组成: 1. 前台的显示层. 2. 分发处理请求 ...

  3. ES6 基础版迭代器

    ES6中引入了generator function* get() { var result1 = yield c; var result2 = yield b; var result3 = yield ...

  4. 必备技能:分清楚DOM的attribute和property

    分清楚DOM的attribute和property,用JQ的时候分清楚attr,和prop方法,网上有很多大神的总结,我就不列举了.

  5. 在Web.config或App.config中的添加自定义配置

    .Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置,所以忍 ...

  6. swiper 内容超出纵向滚动 解决办法

    .swiper-slide { overflow: auto; }   var swiper = new Swiper('.swiper-container', { direction: 'verti ...

  7. JDBC成绩管理系统

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  8. Hadoop 2.2.0学习笔记20131209

    1.下载java 7并安装 [root@server- ~]# rpm -ivh jdk-7u40-linux-x64.rpm Preparing... ####################### ...

  9. 作业七:团队项目——Alpha版本冲刺阶段 001

    今天进展:准备开发环境,安装软件. 今天安排:因为软件过于庞大,所以我们第一天都在按软件,原本计划第一天要设计框架,但因为软件问题.所以我们决定留到第二天.

  10. web前端基础篇⑤

    1.雪碧图技术(精灵图)sprite cpmpass-合并2.兼容性:1.reset充值技术,normalize技术2.加前缀-webkit —mom -ms— -o-3.<!Doctype&g ...