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,以及其语法特点,减少阅读 ...
随机推荐
- JQuery.validator插件使用
首先给变量validator赋值 var validator =$('#test').validate({validate构造 }); 接着调用 $('#test').valid() 会使用上面的验证 ...
- Lucene的基本概念----转载yufenfei的文章
Lucene的基本概念 Lucene是什么? Lucene是一款高性能.可扩展的信息检索工具库.信息检索是指文档搜索.文档内信息搜索或者文档相关的元数据搜索等操作. 信息检索流程如下: 1. 将即将检 ...
- c# 根据文件夹或文件名返回(文件夹或文件)的完整路径
c# 根据文件夹或文件名返回(文件夹或文件)的完整路径 一.方案一:(使用windows API) 二.方案二:(扫描全盘)
- select chosen 的入门使用
首先要引用 因为肯定要引用jq,所以就没有写啦 <link rel="stylesheet" href="__ROOT__/Public/jschy/chosen_ ...
- 单机,伪分布式,完全分布式-----搭建Hadoop大数据平台
Hadoop大数据——随着计算机技术的发展,互联网的普及,信息的积累已经到了一个非常庞大的地步,信息的增长也在不断的加快.信息更是爆炸性增长,收集,检索,统计这些信息越发困难,必须使用新的技术来解决这 ...
- iOS 开发之 GCD 不同场景使用
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- python基础之内建函数(二)
(7)max() 函数:返回列表.元祖或字符串中最大的元素,注意:字母“大于”数字.小写字母“大于”大写字母(字母排序是根据ASCII码表排的) 例如: >>>num = list ...
- Python3之json模块
概念: 序列化(Serialization):将对象的状态信息转换为可以存储或可以通过网络传输的过程,传输的格式可以是JSON,XML等.反序列化就是从存储区域(JSON,XML)读取反序列化对象的状 ...
- opencv学习笔记(五)----图像的形态学操作
图像的形态学操作有基本的腐蚀和膨胀操作和其余扩展形态学变换操作(高级操作)-----开运算,闭运算,礼帽(顶帽)操作,黑帽操作...(主要也是为了去噪声,改善图像) 形态学操作都是用于处理二值图像(其 ...
- 181114socke编程
一.Socket Families 地址簇 socket.AF_UNIX socket.AF_INET socket.AF_INET6 二.Socker Types socket.SOCK_STREA ...