OGNL表达式

Posted zhangww

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OGNL表达式相关的知识,希望对你有一定的参考价值。

package com.cmcc.log;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OGNL {
    public static void main(String[] args) throws OgnlException{
        OgnlContext ognlContext = new OgnlContext();

        Object root = ognlContext.getRoot();

        Object value = Ognl.getValue("‘helloworld‘.length()", ognlContext, root);  //expression就是方法表达式

        System.out.println(value.toString());

        value = Ognl.getValue("@java.lang.Math@random()", ognlContext, root); //这里注意获取静态方法表达式是固定表达: @类名@方法名
        System.out.println(value.toString());

        User user = new User();
        user.setName("张三");
        ognlContext.setRoot(user);

        value = (String) Ognl.getValue("name", ognlContext, ognlContext.getRoot());
        System.out.println(value);


        ognlContext.put("name","张萨姆");

        value = (String) Ognl.getValue("#name", ognlContext, ognlContext.getRoot());
        System.out.println(value);

        value = (String) Ognl.getValue("name", ognlContext, ognlContext.getRoot());
        System.out.println(value);

        value = (String) Ognl.getValue("#name", ognlContext, new Object());
        System.out.println(value);

        value = (String)ognlContext.get("name");
        System.out.println(value);
    }
}

控制台输出:

10
0.05190743204224668
张三
张萨姆
张三
张萨姆
张萨姆

https://blog.csdn.net/qq_34191426/article/details/101002431

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

Struts2中OGNL表达式的用法

什么是Ognl

ognl表达式和s标签儿

Struts2学习笔记三:OGNL表达式学习Struts2与Ognl表达式的结合原理

Ognl 和 EL 和jstl区别

OGNL与值栈