05-ognl基本语法
2018-03-08 09:23
218 查看
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); }
相关文章推荐
- OGNL表达式的基本语法和用法
- OGNL表达式的基本语法和用法------2
- Java程序员从笨鸟到菜鸟之(四十九)细谈struts2(十一)OGNL表达式的基本语法和用法
- .Net_05_事务的基本语法(Sql 语句)
- Java程序员从笨鸟到菜鸟之(四十九)细谈struts2(十一)OGNL表达式的基本语法和用法
- Java程序员从笨鸟到菜鸟之(四十九)细谈struts2(十一)OGNL表达式的基本语法和用法
- OGNL表达式的基本语法和用法 【OGNL中的#、%和$符号】Object-Graph Navigation Language对Java对象图进行导航
- OGNL表达式基本语法与用法详解
- OGNL表达式的基本语法和用法
- OGNL的基本语法
- OGNL表达式的基本语法和用法
- Struts2学习(七)【OGNL基本语法】
- 细谈struts2(十一)OGNL表达式的基本语法和用法
- OGNL表达式的基本语法和用法
- Delphi_07_Delphi_Object_Pascal_基本语法_05_函数参数
- Java程序员从笨鸟到菜鸟之(四十九)细谈struts2(十一)OGNL表达式的基本语法和用法
- OGNL表达式的基本语法和用法
- Strust2第(十一)篇《OGNL表达式的基本语法和用法》
- struts2(十一)OGNL表达式的基本语法和用法
- OGNL表达式的基本语法和用法