Ognl表达式的使用

Posted wangjian_an

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

    


以上是关于Ognl表达式的使用的主要内容,如果未能解决你的问题,请参考以下文章

ognl表达式和s标签儿

OGNL与值栈

OGNL表达式

Struts2 OGNL概述

Struts2整合OGNL

OGNL: