• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JAVA之:OGNL表达式练习

java 搞代码 4年前 (2022-01-09) 28次浏览 已收录 0个评论
文章目录[隐藏]

一、OGNL表达式

  1.简介

  OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航。
  OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能。

  2.使用OGNL准备工作

   2.1导包

  struts2 的包中已经包含了.所以不需要导入额外的jar包

   2.2代码准备

    @Test//准备工作public void fun1() throws Exception{//准备OGNLContext//准备RootUser rootUser = new User("tom",18);//准备ContextMap<String,User> context = new HashMap<String,User>();            context.put("user1", new User("jack",18));            context.put("user2", new User("rose",22));        OgnlContext oc = new OgnlContext();//将rootUser作为root部分        oc.setRoot(rootUser);//将context这个Map作为Context部分        oc.setValues(context);//书写OGNLOgnl.getValue("", oc, oc.getRoot());    }

准备工作

  3.基本语法演示

        //取出root中user对象的name属性String name = (String) Ognl.getValue("name", oc, oc.getRoot());        Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());        System.out.println(name);        System.out.println(age);

取出root中的属性值

        //取出context中键为user1对象的name属性String name = (String) Ognl.getValue(&q<i>本文来源gaodai$ma#com搞$代*码网2</i>uot;#user1.name", oc, oc.getRoot());        String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());        Integer age = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());        System.out.println(name);        System.out.println(name2);        System.out.println(age);

取出context中的属性值

        //将root中的user对象的name属性赋值Ognl.getValue("name='jerry'", oc, oc.getRoot());        String name = (String) Ognl.getValue("name", oc, oc.getRoot());                String name2 = (String) Ognl.getValue("#user1.name='郝强勇',#user1.name", oc, oc.getRoot());        System.out.println(name);        System.out.println(name2);

为属性赋值

        //调用root中user对象的setName方法Ognl.getValue("setName('lilei')", oc, oc.getRoot());        String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());        String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());                System.out.println(name);        System.out.println(name2);

调用方法

        String name = (String) Ognl.getValue("@cn.itheima.a_ognl.HahaUtils@echo('hello 强勇!')", oc, oc.getRoot());//Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());        System.out.println(name);        System.out.println(pi);

调用静态方法

        //创建list对象Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());        String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());        String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());    /*System.out.println(size);        System.out.println(name);        System.out.println(name2);*///创建Map对象Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());        String name3  = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());        Integer age  = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());        System.out.println(size2);        System.out.println(name3);        System.out.println(age);

ognl创建对象-list|map


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JAVA之:OGNL表达式练习
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址