Ognl表达式的使用

加入jar包
这里写图片描述

package cn.hncu.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlException;

import org.junit.Test;

import cn.hncu.bean.Address;
import cn.hncu.bean.User;

public class Demo {
    @Test
    public void  fun1() throws OgnlException{
        User user=new User();
        user.setName("Tom");
        Address address=new Address();
        address.setCity("长沙");
        user.setAddress(address);
        String name=(String) Ognl.getValue("name", new HashMap<Object,Object>(), user);
        System.out.println(name);
        System.out.println(Ognl.getValue("address.city", new HashMap<Object,Object>(), user));
    }
    @Test
    public void  fun2() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        user.setName("Tom");
        Address address=new Address();
        address.setCity("长沙");
        user.setAddress(address);
        map.put(user, user);
        map.put("name", "Jack");
        System.out.println(Ognl.getValue("name", map, user));
        System.out.println(Ognl.getValue("#name", map, user));
    }
    @Test
    //演示设置值1表达式
    public void  fun3() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        map.put(user, user);
        Ognl.getValue("name='Tom'", map, user);
        Ognl.getValue("#name='Tom2'", map, user);//map不行。只能是给map赋值赋值
        System.out.println(Ognl.getValue("name", map, user));
        System.out.println(Ognl.getValue("#name", map, user));
    }
    @Test
    //演示设置值2setValue
    public void  fun4() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        map.put(user, user);
        Ognl.setValue("name", map, user,"Tom");
        System.out.println(user.getName());
        System.out.println(Ognl.getValue("name", map, user));
        System.out.println(Ognl.getValue("#name", map, user));
    }
    @Test
    //演示方法的调用
    public void  fun5() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        map.put(user, user);
        Ognl.getValue("setName('Jack')", map, user);
        System.out.println(Ognl.getValue("name", map, user));
    }
    @Test
    //演示静态方法的调用(静态方法不在根中)
    public void  fun6() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        System.out.println(Ognl.getValue("@cn.hncu.util.DateUtil@getTime()", map, new User()));
    }
    @Test
    //演示静态方法的调用(静态方法在根中)
    public void  fun7() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        System.out.println(Ognl.getValue("@cn.hncu.util.DateUtil@getTime()", map, new User()));
        //      System.out.println(Ognl.getValue("@cn.hncu.util.DateUtil@getTime("+new Date()+")", map, new User()));不行
        //      System.out.println(Ognl.getValue("@cn.hncu.util.DateUtil@getTime(new Date())", map, new User()));不行
        System.out.println(Ognl.getValue("@cn.hncu.util.DateUtil@getTime(new java.util.Date())", map, new User()));//必须加类名
        System.out.println(Ognl.getValue("@cn.hncu.util.DateUtil@PI", map, new User()));
    }
    @Test
    //演示连接符
    public void  fun8() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        map.put("name", "Tim");
        System.out.println(Ognl.getValue("1+1,1+2,#name", map, user));//只返回最后一个表达式结果
    }
    @Test
    //演示创建对象list,map,user
    public void  fun9() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        System.out.println(Ognl.getValue("{'Tom','Jack','wja'}", map, user));
        System.out.println(Ognl.getValue("#{'name':'Tom','age':20}", map, user));
        System.out.println(Ognl.getValue("new cn.hncu.bean.User()", map, user));
    }
    @Test
    //演示in,和not in
    public void  fun10() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        User user=new User();
        System.out.println(Ognl.getValue("'jack' in {'Tom','Jack','wja'}", map, user));
        System.out.println(Ognl.getValue("'jack' not  in {'Tom','Jack','wja'}", map, user));
        System.out.println(Ognl.getValue("'20' in #{'name':'Tom','age':20}", map, user));//true
        Map m=(Map) Ognl.getValue("#{'name':'Tom','age':'20.6'}", map, user);
        System.out.println(m.get("age").getClass());//根据具体情况去转换

    }
    @Test
    //演示集合的投影
    public void  fun11() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        List<User> list=new ArrayList<User>();
        User user=new User();
        user.setName("Tom");
        User user2=new User();
        user2.setName("Tim");
        list.add(user);
        list.add(user2);
        System.out.println(Ognl.getValue("#this", map, list));//list
        System.out.println(Ognl.getValue("#this.{name}", map, list));//list.name

    }
    @Test
    //演示集合选择过滤
    public void  fun12() throws OgnlException{
        Map<Object,Object> map=new HashMap<Object,Object>();
        List<User> list=new ArrayList<User>();
        User user=new User();
        user.setName("Tom");
        user.setAge(20);
        User user2=new User();
        user2.setName("Tim");
        user2.setAge(2);
        list.add(user);
        list.add(user2);
        System.out.println(Ognl.getValue("#this.{?age>18}", map, list));//list.name

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值