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,以及其语法特点,减少阅读 ...
随机推荐
- 关于bootstrap模态框的初始化事件
转:https://blog.csdn.net/u010181136/article/details/77579823
- .Net Core 项目区域请求设置
.net core 和asp.net MVC区域请求有个区别,这里重点记录一下 asp.net MVC 区域请求直接是/区域名称/控制名称/方法名称,其他不需要设置任何东西,而Core 项目这样请求路 ...
- Android学习笔记 - 开始
因为项目需求,要在Android上开发一个证件识别软件,项目时间 9/10- 9/30 工作内容: (1)修改证件识别库 (2)移植证件识别库至Android (3)开发一个Android应用程序 学 ...
- EFCore扩展Select方法(根据实体定制查询语句)
EFCore扩展Select方法(根据实体定制查询语句) 通常用操作数据库的时候查询返回的字段是跟 我们的定义的实体是不一致的,所以往往针对UI或者接口层创建大量的Model, 而且需要手动对应字段 ...
- MySQL事务在MGR中的漫游记—路线图
欢迎访问网易云社区,了解更多网易技术产品运营经验. MGR即MySQL Group Replication,是MySQL官方推出的基于Paxos一致性协议的数据高可靠.服务高可用方案.MGR在20 ...
- 多态的作用-游戏编程展示------新标准c++程序设计
游戏软件的开发最能体现面向对象设计方法的优势.游戏中的人物.道具.建筑物.场景等都是很直观的对象,游戏运行的过程就是这些对象相互作用的过程.每个对象都有自己的属性和方法,不同对象也可能有共同的属性和方 ...
- 《C#多线程编程实战》2.6 ManualResetEventSlim
这个比较好理解的. 正如书上所言,如同一直在打开的大门的屋子,谁要进去,谁就自己的关门,出来的时候在开开. 常用的方法 有三个: Set() //设置为有信号,也就是让等待的线程不用继续等待,唤醒等 ...
- sysbase 笔记
Alter 在已有数据的表中新增一个字段: ALTER table ciecdb.ciec.eci_mansmfile ADD num int default 0;
- 861. Score After Flipping Matrix
We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...
- 连接数据库+注册->登录->抽奖(有关联关系的接口)
注册账号信息需要写入数据库,登录和抽奖时从数据库获取数据 一.连接数据库 my_sql.py: import pymysql class MyDb: def __init__(self,host,pa ...