美文网首页JAVAEE
JAVAEE框架学习——Struts2——OGNL与Struts

JAVAEE框架学习——Struts2——OGNL与Struts

作者: So_ProbuING | 来源:发表于2018-06-05 09:39 被阅读0次

    OGNL概述

    OGNL的全称是对象视图导航语言,是一种功能强大的开源表达式语言。使用这种表达式语言,可以通过某种表达式语法,存取JAVA对象的任意属性,调用Java对象的方法。同时能够自动实现必要的类型转换。如果把表达式看作是一个带有语义的字符串,那么OGNL无疑成为了这个语义字符串与Java对象之间共同的桥梁

    OGNL的作用

    Struts2默认的表达式语言就是OGNL,它具有以下特点

    • 支持对象方法调用。objName.methodName()
    • 支持类静态方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名|值名]。
    • 支持赋值操作和表达式串联
    • 访问OGNL上下文和ActionContext
    • 操作集合对象

    OGNL的要素

    表达式

    表达式是整个OGNL的核心,OGNL会根据表达式去对象中取值,所有OGNL操作都是针对表达式解析后进行的。表明了此次OGNL操作要做什么。表达式就是一个带有语法含义的字符串,这个字符串规定了操作的类型和操作的内容。OGNL支持大量的表达式语法,不仅支持这种“链式”对象访问路径还支持在表达式中进行简单计算

    根对象ROOT(对谁操作)

    Root对象可以理解为OGNL的操作对象,表达式规定了做什么,而Root对象则规定了对谁操作。

    Context对象 (在哪进行操作)

    OGNL的取值还需要一个上下文环境。设置了ROOT对象,OGNL可以对Root对象进行取值或写值等操作。上下文环境规定了OGNL的操作在哪里进行。上下文环境Context是一个Map类型的对象,在表达式中访问Context中的对象,需要使用"#"加上对象的名称。"#对象名称" “#”代表要从Context中取值

    OGNL体验

    准备工作

    导包

    struts2的包中包含OGNL的包


    图片.png
       public void fun1() throws OgnlException {
            //创建OGNLContex对象
            //准备ROOT对象
            User rootUser = new User("wx", 27);
            //准备Context
            HashMap<String, User> map = new HashMap<>();
            map.put("user1", new User("jack", 18));
            map.put("user2", new User("rose", 22));
            map.put("user3", new User("tom", 23));
            OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(map);
            // 书写OGNL
            Ognl.getValue("", oc, oc.getRoot());
        }
    

    语法

    访问对象属性

    //访问根用户属性
    String name = (String) Ognl.getValue("name", oc, oc.getRoot());
    //访问上下文环境用户属性
    String user1Name = (String) Ognl.getValue("#user1.name", oc, oc.getValues());
    

    设置对象属性

    //设置根用户属性
    Ognl.getValue("name=jerry", ognlContext, ognlContext.getRoot());
    //设置上下文环境用户属性
    String user1Name = (String) Ognl.getValue("#user1.name='hello',#user1.name", ognlContext, ognlContext.getRoot());
    

    调用对象方法

     Ognl.getValue("setName('测试1')",ognlContext,ognlContext.getRoot());
            String value = (String) Ognl.getValue("getName()", ognlContext, ognlContext.getRoot());
            String user1Name = (String) Ognl.getValue("#user1.setName('用户1'),#user1.getName()", ognlContext, ognlContext.getRoot());
    

    调用静态方法

    public class TestUtils {
        public static Object echo(Object o) {
    
            return o;
        }
    }
    
      OgnlContext oc = new OgnlContext();
            oc.setRoot(rootUser);
            oc.setValues(map);
            // 书写OGNL
            //调用静态方法
            String echoValue = (String) Ognl.getValue("@cn.probuing.utils.TestUtils@echo('hello')", oc, oc.getRoot());
            System.out.println(echoValue);
    

    格式

    @类的全路径名@方法名称(参数列表)
    @类的全路径名@属性名称

    创建对象

       //创建list对象
       Integer listSize = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, rootUser);
      //按索引取值
    String value1 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
    //调用方法取值
    String value2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
    
    String mapName = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
    String value = (String) Ognl.getValue("#{'name':'tom','age':18}.get('name')", oc, oc.getRoot());
    

    Struts2与OGNL结合

    在Struts2中OgnlContext是ValueStack。
    ValueStack是Struts2的一个接口,字面意思为值栈。OgnlValueStack是ValueStack的实现类。客户端发起一个请求Struts2架构会创建一个action实例同时创建一个OgnlValueStack值栈实例。OgnlValueStack贯穿整个Action的生命周期,struts2中使用OGNL将请求Action的参数封装为对象存储到值栈中,并通过OGNL表达式读取值栈中的对象属性值
    在OgnlValueStack中包括两部分,值栈和map(ognl上下文)

    结合原理

    结合原理

    栈原理

    栈原理
    图片.png
    图片.png
    图片.png

    Struts2与OGNL结合的体现

    参数接收

    图片.png
    图片.png
    图片.png
    图片.png

    配置文件

    图片.png

    相关文章

      网友评论

        本文标题:JAVAEE框架学习——Struts2——OGNL与Struts

        本文链接:https://www.haomeiwen.com/subject/hjqxsftx.html