• struts OGNL表达式


    OGNLContext对象有两部分构成

      一部分是ROOT:可以放置任何对象作为ROOT

      另外一部分Context:必须是Map形式(键值对)

      

    OGNL表达式操作

    package cn.future.a_ognl;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import ognl.Ognl;
    import ognl.OgnlContext;
    import ognl.OgnlException;
    
    import org.junit.Test;
    
    import cn.future.domain.User;
    
    public class Demo {
        
        @Test
        //取出Root中的值
        public void fun() throws OgnlException{
            //OGNL表达式
            //准备ROOT
            User userRoot = new User("ms",25);
            //准备Context
            Map<String,User> contextMap = new HashMap<String, User>();
            contextMap.put("user1", new User("AAA",10));
            contextMap.put("user2", new User("BBB",11));
            //书写OGNL
            OgnlContext oc = new OgnlContext();
            oc.setRoot(userRoot);
            oc.setValues(contextMap);
            //OGNL取值
            //取root中userRoot对象的name属性
            String name = (String) Ognl.getValue("name", oc, oc.getRoot());
            int age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(age);
        }
        
        @Test
        //取出Context中的值
        public void fun1() throws OgnlException{
            //OGNL表达式
            //准备ROOT
            User userRoot = new User("ms",25);
            //准备Context
            Map<String,User> contextMap = new HashMap<String, User>();
            contextMap.put("user1", new User("AAA",10));
            contextMap.put("user2", new User("BBB",11));
            //书写OGNL
            OgnlContext oc = new OgnlContext();
            oc.setRoot(userRoot);
            oc.setValues(contextMap);
            //OGNL取值
            //取User1对象的name属性
            String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
            int age = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());
            System.out.println(name);
            System.out.println(age);
        }
        
        @Test
        //为属性赋值
        public void fun2() throws OgnlException{
            //OGNL表达式
            //准备ROOT
            User userRoot = new User("ms",25);
            //准备Context
            Map<String,User> contextMap = new HashMap<String, User>();
            contextMap.put("user1", new User("AAA",10));
            contextMap.put("user2", new User("BBB",11));
            //书写OGNL
            OgnlContext oc = new OgnlContext();
            oc.setRoot(userRoot);
            oc.setValues(contextMap);
            //OGNL取值
            //给Roog中userRoot对象的name属性赋值
            Ognl.getValue("name='grf'", oc, oc.getRoot());//赋值 有返回值,返回值是name的值
            String name = (String) Ognl.getValue("name='grf',name", oc, oc.getRoot());//即赋值又取值
            //给Context中user1的name属性赋值
            Ognl.getValue("#user1.name='grf'", oc, oc.getRoot());
        }
        
        @Test
        //为属性赋值(set get)
        public void fun3() throws OgnlException{
            //OGNL表达式
            //准备ROOT
            User userRoot = new User("ms",25);
            //准备Context
            Map<String,User> contextMap = new HashMap<String, User>();
            contextMap.put("user1", new User("AAA",10));
            contextMap.put("user2", new User("BBB",11));
            //书写OGNL
            OgnlContext oc = new OgnlContext();
            oc.setRoot(userRoot);
            oc.setValues(contextMap);
            //OGNL取值
            //给Roog中userRoot对象的name属性赋值
            Ognl.getValue("setName('grf')", oc, oc.getRoot());//赋值 返回值为null
            String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());//即赋值又取值
            //给Context中user1的name属性赋值
            Ognl.getValue("#user1.setName('grf'),#user1.getName()", oc, oc.getRoot());
        }
        
        @Test
        //调用静态方法,或者静态属性
        public void fun4() throws OgnlException{
            //OGNL表达式
            //准备ROOT
            User userRoot = new User("ms",25);
            //准备Context
            Map<String,User> contextMap = new HashMap<String, User>();
            contextMap.put("user1", new User("AAA",10));
            contextMap.put("user2", new User("BBB",11));
            //书写OGNL
            OgnlContext oc = new OgnlContext();
            oc.setRoot(userRoot);
            oc.setValues(contextMap);
            //OGNL取值
            //给Roog中userRoot对象的name属性赋值
            Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());//赋值 返回值为null
            System.out.println(pi);
            
        }
        
        @Test
        //创建集合 list|map
        public void fun5() throws OgnlException{
            //OGNL表达式
            //准备ROOT
            User userRoot = new User("ms",25);
            //准备Context
            Map<String,User> contextMap = new HashMap<String, User>();
            contextMap.put("user1", new User("AAA",10));
            contextMap.put("user2", new User("BBB",11));
            //书写OGNL
            OgnlContext oc = new OgnlContext();
            oc.setRoot(userRoot);
            oc.setValues(contextMap);
            //OGNL取值
            //创建list
            Ognl.getValue("{'aaa','bbb','ccc','ddd'}", oc, oc.getRoot());
            Integer listSize = (Integer) Ognl.getValue("{'aaa','bbb','ccc','ddd'}.size()", oc, oc.getRoot());
            String listName = (String) Ognl.getValue("{'aaa','bbb','ccc','ddd'}[0]", oc, oc.getRoot());
            String listName1 = (String) Ognl.getValue("{'aaa','bbb','ccc','ddd'}.get(1)", oc, oc.getRoot());
    
            //创建map
            Ognl.getValue("#{'name':'ms','age',25}", oc, oc.getRoot());
            Integer mapSize = (Integer) Ognl.getValue("#{'name':'ms','age',25}.size()", oc, oc.getRoot());
            String mapName = (String) Ognl.getValue("#{'name':'ms','age',25}[name]", oc, oc.getRoot());
            Integer mapAge = (Integer) Ognl.getValue("#{'name':'ms','age',25}.get('age')", oc, oc.getRoot());
        }
    }
  • 相关阅读:
    POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)
    防止登录页面出如今frame中
    android--显式跳转和隐式跳转的差别使用方法
    卫星照片
    poj 2586 Y2K Accounting Bug (贪心)
    【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
    【转】在Eclipse中使用PyDev进行Python开发
    【转】eclipse + Pydev 配置Python开发环境
    【转】Python自动化测试 (一) Eclipse+Pydev 搭建开发环境
    【转】Eclipse的启动问题【an error has occurred see the log file】
  • 原文地址:https://www.cnblogs.com/ms-grf/p/7350551.html
Copyright © 2020-2023  润新知