05-ognl基本语法
1 基本取值
@Test
//1基础语法演示-基本取值
//取出root中的属性值
public void fun2() throws Exception{
//1 准备OGNLcontext
OgnlContext oc = new OgnlContext();
//2 准备root
User rootUser = new User();
rootUser.setUsername("tom");
rootUser.setAge(18);
//3准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User());
context.put("user2", new User());
//将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个Map作为context部分
oc.setValues(context);
//===============================
//4书写ognl
//获取root用user对象的属性值username & age
String username = (String) Ognl.getValue("username", oc,oc.getRoot());
Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
System.out.println(username);
System.out.println(age);
}
@Test
//1 ognl语法演示-基本取值
//取出context中属性的值
public void fun3() throws Exception{
//1准备ognlcontext
OgnlContext ognlContext = new OgnlContext(); //2准备root
User rootUser = new User("lucy", 24); //3准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("旺财",26));
context.put("user2", new User("小强",21)); //将rootUser作为root部分
ognlContext.setRoot(rootUser);
//将context这个Map作为context部分
ognlContext.setValues(context);
//==========================================
//4书写Ognl
//取出context中键为user1&user2对象的username和age属性
String username1 = (String) Ognl.getValue("#user1.username", ognlContext, ognlContext.getRoot());
Integer age1 = (Integer) Ognl.getValue("#user1.age", ognlContext, ognlContext.getRoot());
String username2 = (String) Ognl.getValue("#user2.username", ognlContext, ognlContext.getRoot());
Integer age2 = (Integer) Ognl.getValue("#user2.age", ognlContext, ognlContext.getRoot());
System.out.println(username1+":"+age1);
System.out.println(username2+":"+age2);
}
2 赋值
//2 ognl基本语法-赋值
@Test
public void fun4() throws Exception{
//准备ognlcontext
OgnlContext oc = new OgnlContext();
//准备root
User rootUser = new User("小强", 15);
//准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("张三", 19));
context.put("user2", new User("李四", 20));
//将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个Map作为context部分
oc.setValues(context);
//======================================
//书写ognl
//将root中的user对象的username设置为tom
String username = (String) Ognl.getValue("username='tom',username", oc, oc.getRoot());
System.out.println(username); //将context中的user1对象的age修改为25;
Integer age = (Integer) Ognl.getValue("#user1.age=25,#user1.age", oc, oc.getRoot());
System.out.println(age); String name2 = (String) Ognl.getValue("#user2.username='jerry',#user2.age=36,#user2.username", oc, oc.getRoot());
System.out.println(name2);
Integer age2 = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
System.out.println(age2);
}
3 调用方法
//3 ognl基本语法-调用方法
@Test
public void fun5() throws Exception{
//准备ognlcontext
OgnlContext oc = new OgnlContext(); //准备root
User rootUser = new User("tom", 28); //准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("jenny", 19));
context.put("user2", new User("lucy", 21)); //将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个map作为context部分
oc.setValues(context);
//================================= //书写ognl
//调用root中user对象的setUsername方法
String username = (String) Ognl.getValue("setUsername('leilei'),username", oc, oc.getRoot());
System.out.println(username);
//调用root中User对象的getName()方法
Integer age = (Integer) Ognl.getValue("getAge()", oc, oc.getRoot());
System.out.println(age); Ognl.getValue("#user1.setUsername('旺财'),#user1.setAge(27)", oc, oc.getRoot());
String name1 = (String) Ognl.getValue("#user1.getUsername()", oc, oc.getRoot());
Integer age1 = (Integer) Ognl.getValue("#user1.getAge()", oc, oc.getRoot());
System.out.println(name1+":"+age1);
}
4 调用静态方法
//4 ognl基本语法--->调用静态方法
@Test
public void fun6() throws Exception{
//准备ognlcontext
OgnlContext oc = new OgnlContext();
//准备root
User rootUser = new User("tom", 18);
//准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("lucy", 15));
context.put("user2", new User("jerry", 15));
//将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个map作为context
oc.setValues(context); //书写ognl
String s = (String) Ognl.getValue("@www.test.utils.EchoUtils@echo('你好')", oc, oc.getRoot());
System.out.println(s); //Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
System.out.println(pi);
}
5 创建对象
//5 ognl基本语法--->创建对象(list,map)
@Test
public void fun7() throws Exception{
//准备ognlcontext
OgnlContext oc = new OgnlContext();
//准备root
User rootUser = new User("tom", 15);
//准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("jeny", 56));
context.put("user2", new User("lucy", 23));
//将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个map作为context
oc.setValues(context); //书写ognl
//创建list对象
Integer size = (Integer)Ognl.getValue("{'tom','jerry','lucy','jack'}.size()", oc, oc.getRoot());
String name = (String)Ognl.getValue("{'tom','jerry','lucy','jack'}[0]", oc, oc.getRoot());
String name1 = (String)Ognl.getValue("{'tom','jerry','lucy','jack'}.get(1)", oc, oc.getRoot());
//System.out.println(size);
//System.out.println(name);
//System.out.println(name1); //创建map对象
Integer mapSize = (Integer)Ognl.getValue("#{'name':'tom','age':35}.size", oc, oc.getRoot());
String mapName = (String)Ognl.getValue("#{'name':'tom','age':35}['name']", oc, oc.getRoot());
Integer mapAge = (Integer)Ognl.getValue("#{'name':'tom','age':35}.get('age')", oc, oc.getRoot());
System.out.println(mapSize);
System.out.println(mapName);
System.out.println(mapAge);
}
6 使用字符串的已有方法
//6 ognl基本语法====>使用字符串的已有方法
@Test
public void fun8() throws Exception{
//准备ognlcontext
OgnlContext oc = new OgnlContext();
//准备root
User rootUser = new User("tom", 15);
//准备context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("jeny", 56));
context.put("user2", new User("lucy", 23));
//将rootUser作为root部分
oc.setRoot(rootUser);
//将context这个map作为context
oc.setValues(context); //书写ognl
Integer sl = (Integer) Ognl.getValue("'we are from china'.length()", oc, oc.getRoot());
String sr = (String) Ognl.getValue("'we are from china'.replace('we','you')", oc, oc.getRoot());
Character sc = (Character) Ognl.getValue("'we are from china'.charAt(0)", oc, oc.getRoot());
System.out.println(sl);
System.out.println(sr);
System.out.println(sc);
}
05-ognl基本语法的更多相关文章
- Ognl 语法基础教程
本文将力求用最简单的语言和实例,介绍一下 OGNL 的语法规则,文章主要内容参考自官方文档http://commons.apache.org/proper/commons-ognl/language- ...
- OGNL表达式介绍
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存 ...
- ognl表达式和s标签
1.ognl表达式: Ognl上下文对象:(他是一个可以存储数据的空间结构,而且在这个结构中包含之前 jsp中的作用域对象) (放在 value stack控件),当前访问的Action这个上下文对象 ...
- Struts 2 OGNL
1.什么是OGNL? 2.Struts 2 OGNL 表达式 ====================== 华丽丽的分割线 ====================== 1.什么是OG ...
- JavaWeb框架SSH_Struts2_(四)----->表达式语言OGNL
1. 表达式语言OGNL OGNL简介 OGNL基本语法 常量 操作符 OGNL表达式 OGNL基础 OGNL上下文 OGNL值栈 OGNL的访问 2. 具体内容 2.1 OGNL简介 OGNL(Ob ...
- 浅谈OGNL表达式
OGNL(Object-Graph Navigation Language):对象视图导航语言 ${user.addr.name}这样的写法就叫对象视图导航 OGNL不仅可以视图导航,支持EL表达式更 ...
- Struts2漏洞利用原理及OGNL机制
Struts2漏洞利用原理及OGNL机制研究 概述 在MVC开发框架中,数据会在MVC各个模块中进行流转.而这种流转,也就会面临一些困境,就是由于数据在不同MVC层次中表现出不同的形式和状态而造成 ...
- JavaWeb框架_Struts2_(四)----->表达式语言OGNL
2. 表达式语言OGNL 2.1 OGNL简介 OGNL(Object-Graph Navigation Language)对象图导航语言的缩写,OGNL是一种表达式语言(Expression L ...
- Ognl 使用实例手册
上一篇博文介绍了ongl的基础语法,接下来进入实际的使用篇,我们将结合一些实际的case,来演示ognl究竟可以支撑到什么地步 在看本文之前,强烈建议先熟悉一下什么是ognl,以及其语法特点,减少阅读 ...
随机推荐
- 【留用】C#的一些好的书籍
浏览博客的时候发现一篇推荐的C#书籍,感觉真的不错,涉略过几本,水平问题,没看的很深入,正在努力,留用了!!! http://www.cnblogs.com/tongming/p/3879752.ht ...
- Java Serializable(序列化)的理解和总结
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object st ...
- Qt信号与槽 如何写2个类,一个发送信号,一个接收并处理
题目: 假设要做2个类,一个类的值提供一个函数SetValue,当这个值发生变化时,假设>10就触发告警调用B的函数; ------------------------------------- ...
- MVC 登陆鉴权
public ActionResult Login(string data) { var _params = JsonConvert.DeserializeAnonymousType(data, ne ...
- <select>标签默认值设置
<td> <label>操作类型:</label> <select id="operation_type" class="com ...
- php代码审计1(php.ini配置)
1.php.ini基本配置-语法 大小写敏感directive = value(指令=值)foo=bar 不等于 FOO=bar 运算符| & - ! 空值的表达方法foo = ;fo ...
- mybatis 学习笔记(二):mybatis SQL注入问题
mybatis 学习笔记(二):mybatis SQL注入问题 SQL 注入攻击 首先了解下概念,什么叫SQL 注入: SQL注入攻击,简称SQL攻击或注入攻击,是发生于应用程序之数据库层的安全漏洞. ...
- 关于C语言中printf函数“输出歧视”的问题
目录 关于C语言中printf函数"输出歧视"的问题 问题描述 探索问题原因 另一种研究方法 问题结论 关于C语言中printf函数"输出歧视"的问题 问题描述 ...
- 跟我一起读postgresql源码(三)——Rewrite(查询重写模块)
上一篇博文我们阅读了postgresql中查询分析模块的源码.查询分析模块对前台送来的命令进行词法分析.语法分析和语义分析后获得对应的查询树(Query).在获得查询树之后,程序开始对查询树进行查询重 ...
- Python3之requests模块
Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...