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基本语法的更多相关文章

  1. Ognl 语法基础教程

    本文将力求用最简单的语言和实例,介绍一下 OGNL 的语法规则,文章主要内容参考自官方文档http://commons.apache.org/proper/commons-ognl/language- ...

  2. OGNL表达式介绍

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存 ...

  3. ognl表达式和s标签

    1.ognl表达式: Ognl上下文对象:(他是一个可以存储数据的空间结构,而且在这个结构中包含之前 jsp中的作用域对象) (放在 value stack控件),当前访问的Action这个上下文对象 ...

  4. Struts 2 OGNL

    1.什么是OGNL? 2.Struts 2 OGNL 表达式      ======================  华丽丽的分割线  ======================  1.什么是OG ...

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

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

  6. 浅谈OGNL表达式

    OGNL(Object-Graph Navigation Language):对象视图导航语言 ${user.addr.name}这样的写法就叫对象视图导航 OGNL不仅可以视图导航,支持EL表达式更 ...

  7. Struts2漏洞利用原理及OGNL机制

    Struts2漏洞利用原理及OGNL机制研究   概述 在MVC开发框架中,数据会在MVC各个模块中进行流转.而这种流转,也就会面临一些困境,就是由于数据在不同MVC层次中表现出不同的形式和状态而造成 ...

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

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

  9. Ognl 使用实例手册

    上一篇博文介绍了ongl的基础语法,接下来进入实际的使用篇,我们将结合一些实际的case,来演示ognl究竟可以支撑到什么地步 在看本文之前,强烈建议先熟悉一下什么是ognl,以及其语法特点,减少阅读 ...

随机推荐

  1. Extjs Ext.TreePanel

    TreePanel 简单实例. <link rel="stylesheet" href="Js/ext-4.2/resources/css/ext-all-nept ...

  2. Ubuntu的Unable to locate package无法更新源问题解决方案

    https://blog.csdn.net/long19910605/article/details/47017889/ 问题: 更新源时提示不能联网(does the network require ...

  3. window.document 对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. spring深入了解心得

    spring 主要核心组件 :Core.上下文(Context) .实体(Bean): spring 主要由两大特色:控制反转(IOC).面向对象(AOP): spring中Core主要用于组建Bea ...

  5. windows windows server2003 开机自动挂盘

    windows  windows server2003 开机自动挂盘 方案一: 设置任务计划:开机启动 方案二: 将执行文件放入启动文件夹

  6. 题解 P1434 【滑雪】

    题目链接 此题运用功能强大的 ~~暴力搜索~~ 记忆化搜索才是重点!!! 然而,这是一道经典的DP问题 如果我们用$dis[i][j]$来表示坐标为$(i,j)$时的高度 $cnt[i][j]$ 是我 ...

  7. WKWebView 的使用和封装

    WKWebView 的使用和封装 前言 项目中有个新闻资讯模块展示公司和相关行业的最新动态. 这个部分基本是以展示网页为主,内部可能会有一些 native 和 JS 代码的交互. 因为是新项目,所以决 ...

  8. PowerShell 如何 远程连接【转】

    转自: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://bobzy.blog.51cto.com/2109336/1181249 ...

  9. Docker 安装Hadoop集群

    资源准备:jdk1.8及hadoop2.7.3 链接:https://pan.baidu.com/s/1x8t1t2iY46jKkvNUBHZlGQ 提取码:g1gm 复制这段内容后打开百度网盘手机A ...

  10. DHCP协议及基本实现原理

    DHCP(Dynamic Host Configuration Protocol):动态主机配置协议. DHCP的优缺点 DHCP服务优点:网络管理员可以验证IP地址和其它配置参数,而不用去检查每个主 ...