浅谈OGNL表达式
OGNL(Object-Graph Navigation Language):对象视图导航语言
${user.addr.name}这样的写法就叫对象视图导航
OGNL不仅可以视图导航,支持EL表达式更加丰富的功能
EL表达式取值要从11个内置对象中取:requestScope、sessionScope、applicationScope、pageScope、 pageContext、params、paramValues、header、headerValues、cookie、initParams
OGNL表达式取值要从OGNLContext中取值,在Ognl上下文中有Root部分和Context部分,Root部分放置任何对象作为root都可以,Context部分必须是Map类型
使用OGNL准备工作
导包->struts2的包中已经包含了,所以不需要导入额外的jar包
书写代码
@Test
//准备工作
public void fun1() throws Exception{
//准备ONGL Context
//准备ROOT
User rootUser = new User("tom","18");
//准备Context
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL 在双引号里写OGNL表达式即可
Ognl.getValue("", oc, oc.getRoot());
}
OGNL基本语法演示 取出root的值
@Test
//基本语法演示
//取出oot中的属性值
public void fun2() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
//取出root中user对象的name属性
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
String age = (String) Ognl.getValue("age", oc, oc.getRoot());
System.out.println(name+" "+age);
}
tom 18
OGNL取出Context的值
@Test
//基本语法演示
//取出context中的属性值
public void fun3() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
String age = (String) Ognl.getValue("#user2.age", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
System.out.println(age);
}
jack
rose
22
OGNL赋值语法
@Test
//赋值
public void fun4() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context); Ognl.getValue("name='jerry'", oc, oc.getRoot());
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
//书写OGNL
String name2 = (String) Ognl.getValue("#user1.name='zao',#user1.name", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
}
jerry
zao
OGNL方法调用
@Test
//调用方法
public void fun5() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
Ognl.getValue("setName('lilei')", oc, oc.getRoot());
String name = (String) Ognl.getValue("getName()", oc, oc.getRoot()); String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
}
lilei
lucy
OGNL调用静态方法和静态常量
注意下面表达式
@全类名@方法名('传参')
@全类名@常量名
@@常量名
@Test
public void fun6() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello shijie')", oc, oc.getRoot());
Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
Double pi1 = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
System.out.println(name);
System.out.println(pi);
System.out.println(pi1);
}
hello shijie
3.141592653589793
3.141592653589793
OGNL创建 list map集合
@Test
public void fun7() throws OgnlException{
User rootUser = new User("tom","18");
Map<String, User> context = new HashMap<String,User>();
context.put("user1", new User("jack","18"));
context.put("user2", new User("rose","22"));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
//创建list对象
Integer size = (Integer) Ognl.getValue("{'tom','jerr','jack'}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("{'tom','jerr','jack'}[0]", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("{'tom','jerr','jack'}.get(1)", oc, oc.getRoot());
System.out.println(size);
System.out.println(name);
System.out.println(name2);
//创建map对象
Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
System.out.println(size2);
System.out.println(name3);
System.out.println(age);
}
3
tom
jerr
2
tom
18
浅谈OGNL表达式的更多相关文章
- 浅谈Lambda表达式详解
lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 “成为”.运算符将表达式分为两部分,左边指定 ...
- 浅谈lambda表达式<最通俗易懂的讲解
Java8发布已经有一段时间了,这次发布的改动比较大,很多人将这次改动与Java5的升级相提并论.Java8其中一个很重要的新特性就是lambda表达式,允许我们将行为传到函数中.想想看,在Java8 ...
- Java线上问题排查神器Arthas快速上手与原理浅谈
前言 当你兴冲冲地开始运行自己的Java项目时,你是否遇到过如下问题: 程序在稳定运行了,可是实现的功能点了没反应. 为了修复Bug而上线的新版本,上线后发现Bug依然在,却想不通哪里有问题? 想到可 ...
- Linux特殊符号浅谈
Linux特殊字符浅谈 我们经常跟键盘上面那些特殊符号比如(?.!.~...)打交道,其实在Linux有其独特的含义,大致可以分为三类:Linux特殊符号.通配符.正则表达式. Linux特殊符号又可 ...
- 转:浅谈CSS在前端优化中一些值得注意的关键点
前端优化工作中要考虑的元素多种多样,而合理地使用CSS脚本可以在很大程度上优化页面的加载性能,以下我们就来浅谈CSS在前端优化中一些值得注意的关键点: 当谈到Web的“高性能”时,很多人想到的是页面加 ...
- 转: 浅谈C/C++中的指针和数组(二)
转自:http://www.cnblogs.com/dolphin0520/archive/2011/11/09/2242419.html 浅谈C/C++中的指针和数组(二) 前面已经讨论了指针和数组 ...
- 浅谈w3c标准
#浅谈w3c标准 ##w3c标准是什么 w3c标准包括多个方面,官方是从应用角度分的,相关的文档可以戳[这里](http://www.w3.org/standards/).如果从WEB技术角度,可以分 ...
- 浅谈sql 、linq、lambda 查询语句的区别
浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...
- [C#]6.0新特性浅谈
原文:[C#]6.0新特性浅谈 C#6.0出来也有很长一段时间了,虽然新的特性和语法趋于稳定,但是对于大多数程序猿来说,想在工作中用上C#6.0估计还得等上不短的一段时间.所以现在再来聊一聊新版本带来 ...
随机推荐
- Self Hosting WebServer 的几种方式
写在前面: IIS是Windows平台非常关键的组件,它是微软自带的Web服务器,可以很方便的帮助我们运行起一个网站,WebApi等服务,提供给外部来访问.即使它被很多java或者ruby的同学各种鄙 ...
- sql执行时间过长,请高手指点!
需求:查询出每一位"社工员"通过23门社工课进度100%的数量和23门社工课对应的考试通过的数量. 业务解析: 1.社工员--针对特定学员的一批人.在表USERS_SW_REGIS ...
- SQLServer复习文档1(with C#)
目录: 前言 SQL Server基础准备 1.新建数据库 2.在数据中添加表 3.向表中添加数据 SQL Server与C#基础准备 实例解析 1.C#连接数据库 2.查询特定列数据 3.使用列别名 ...
- Codeforces 817F MEX Queries
题意:对一个维护三种操作:1.将[l..r]中的数全部加入集合中.2.将集合中[l..r]范围内的数删去.3.将集合中在[l..r]中的数删去,并将之前不在集合中的数加入集合 考虑到最近线段树总是写爆 ...
- tolua++实现lua层调用c++技术分析
tolua++技术分析 cocos2dx+lua 前言 一直都使用 cocos2dx + lua 进行游戏开发,用 Lua 开发可以专注于游戏逻辑的实现,另外一方面可以实现热更新:而且 lua 是一个 ...
- Python的集合
1. Python的集合 1.1 集合的定义 在Python中, 集合set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.Python中的集合set类 ...
- WPF水珠效果按钮组
效果图 相关知识 这部分基本就是废话,网上都能找到,我只不过是整理了以下.建议先不看,用到的时候可以回来看看 贝塞尔曲线 先来看两组图,有助于理解什么是贝塞尔曲线(图片取自维基百科,参考链接1) 二次 ...
- java学习笔记—集合之Map集合
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Times } p.p2 { margin: 0.0p ...
- 浅谈 Integer 类
在讲解 Integer 之前,我们先看下面这段代码: public static void main(String[] args) { Integer i = 10; Integer j = 10; ...
- js模拟静态方法
//模拟静态 var Animal = function(name){ this.name = name; Animal.instanceCounter ++; }; Animal.instanceC ...