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);
}

最新文章

  1. EF Core 数据库迁移(Migration)
  2. chrome 调试 SASS
  3. web时代变迁及html5与4的区别
  4. jQuery还原select下拉列表和清空input的值,回显下拉列表框的值
  5. LeetCode OJ--Swap Nodes in Pairs
  6. angular 依赖注入
  7. 微软Sql server analysis service数据挖掘技术
  8. UVa 1451 (数形结合 单调栈) Average
  9. HTML5实现刮奖效果
  10. Java学习笔记(三)Java2D组件
  11. Finally! I do understand "flex-basis"
  12. AX_RecordSortedList
  13. poj 1511 正向 反向 构两个图
  14. 2-java内省机制(Introspector)
  15. 关于oracle中varchar2与nvarchar2的一点认识
  16. MYSQL查询优化:数据类型与效率
  17. 医学图像之DICOM格式解析
  18. Java开发之富文本编辑器TinyMCE
  19. 关于Linux路由表的route命令
  20. 『Python题库 - 填空题』151道Python笔试填空题

热门文章

  1. HTML5 Canvas核心技术:图形、动画与游戏开发 PDF扫描版​
  2. cocos学习
  3. WPF 控件库——轮播控件
  4. Android学习笔记 Gallery图库组件的使用
  5. unix网络编程 str_cli epoll 非阻塞版本
  6. NSLocale 本地化信息
  7. 321. Create Maximum Number (c++ ——&gt; lexicographical_compare)
  8. loj#2978. 「THUSCH 2017」杜老师(乱搞)
  9. MyBatis介绍及使用
  10. 2008R2 无法安装 HDP Apache 系列服务解决方案